On Oct 7, 10:36 am, "BartC" <b...@freeuk.com> wrote: > i=16 > x = {1 : fna(), 2 : fnb(), 3 : fnc()}.get(i, "None Of The Above") > print x > > Other than efficiency concerns, sometimes you don't want the extra > side-effects. > > Probably there are workarounds here too, but I suspect the syntax won't be > quite as pert as the Algol68-style example: > > x = (i | "Zero", "One", "Two" | "None of the above") # 0-based
/* ¢ Algol68 case clause is 1-based. ¢ */ However Algol68's CASE ~ IN ~,~,~,~ OUT ~ ESAC clause does mean that the case items of the clause are not evaluated unless selected. Here are some comparisons with python: $ cat py_case.py #!/usr/bin/env python # -*- coding: utf-8 -*- # This procedure evaluates all the months lengths _before_ building the dict. # In that case of month length, they are constants. def days_in_month0(year, month): return {1:31, 2:{True:29,False:28}[year%4==0 and year%100!=0 or year%400==0], 3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31 }.get(month,None) # This procedure is closer - in behaviour - to that of the algol68 case clause. # Keypoint being that individual month length code is not evaluated unless called() def days_in_month(year, month): return {1:lambda:31, 2:lambda:{True:lambda:29,False:lambda:28}[year%4==0 and year%100! =0 or year%400==0](), 3:lambda:31,4:lambda:30,5:lambda:31,6:lambda:30,7:lambda: 31,8:lambda:31,9:lambda:30,10:lambda:31,11:lambda:30,12:lambda:31 }.get(month,"None of the above")() for year in 1899,1900,1901,1999,2000,2001: print "%4d=%2d"%(year,days_in_month0(year, 2)), """ Compare with Algol 68: Brief choice clause example: PROC days in month = (INT year, month)UNION(INT,STRING): (month|31, (year%×4=0 ∧ year%×100≠0 ∨ year%×400=0|29|28), 31,30,31,30,31,31,30,31,30,31 |"None of the above" # or EMPTY # ); BOLD choice clause example: PROC days in month = (INT year, month)UNION(INT,STRING): CASE month IN 31, IF year MOD 4 EQ 0 AND year MOD 100 NE 0 OR year MOD 400 EQ 0 THEN 29 ELSE 28 FI, 31,30,31,30,31,31,30,31,30,31 OUT "None of the above" # or EMPTY # ESAC; []INT years = (1899,1900,1901,1999,2000,2001); FOR key TO UPB years DO INT year=years[key]; printf(($4d,"="2d" "$, year, days in month(year, 2))) OD """ $ python py_case.py 1899=28 1900=28 1901=28 1999=28 2000=29 2001=28 There are also the compound IF and CASE conditional clauses: IF condition1 THEN statements ELIF condition2 THEN statements [ ELSE statements ] FI "brief" form if IF clause: ( condition1 | statements |: condition2 | statements | statements ) CASE switch1 IN statements, statements,... OUSE switch2 IN statements, statements,... [ OUT statements ] ESAC "brief" form of CASE statement: ( switch1 | statements,statements,... |: switch2 | statements,statements,... | statements ) Enjoy NevilleDNZ -- To download Linux's Algol68 Compiler, Interpreter & Runtime: * http://sourceforge.net/projects/algol68/files -- http://mail.python.org/mailman/listinfo/python-list