Lambda vs. function
Breton Slivka
zen at zenpsycho.com
Mon Oct 27 23:28:13 PDT 2008
>> I would argue it. You can either equivalently consider the fall through to
>> be a jump, or say it behaves as if the code of case 2
>
> "as if the code of case 2 were duplicated into case 1"
>
> - Maciej
I had a go at combining that concept with my object dispatcher
concept, to try and come up with an example of a reasonable transform
of a switch statement to a structure with equivalent function that
uses (mostly) only lambdas and expressions. The idea here, I hope, may
make tail positions more obvious when it comes to switch statements.
so we're transforming this switch statement with fall throughs
switch (switcher) {
case 1:
if (p()) {
f();
g();
break;
}
// fall through
case 2:
while(x) {
if(x==y) {
break;
}
}
case 3:
x += y;
break;
default:
y = x;
break;
}
to this object dispatcher:
var dispatch = {
1: lambda () {
p() ? f(), g() : dispatch[2]();
},
2: lambda () {
while(x) {
x==y ? undefined : dispatch[3]();
}
},
3: lambda () {
x += y;
}
}
//Implementing default case with a tertiary operator
var result = dispatch[switcher] ? dispatch[switcher]() : y=x;
I've used tertiary operators to replace if/else, since the tertiary
operator is an expression form where the tail position is rather
obvious and defined. (i think). If there's a lambdified version of
the if/else statement, or "while", I would be fascinated.
More information about the Es-discuss
mailing list