[e-lang] Idea: Match expressions
Kevin Reid
kpreid at attglobal.net
Tue Mar 14 15:01:38 EST 2006
match d :(0..9) { `digit $d` }
expands to:
def _(specimen__1, ejector__2) :any {
def d :(0..9) := (specimen__1, ejector__2)
return thunk { `digit $d` }
}
The purpose of this would be to make it easy to use patterns as
parameters. The equivalent for expressions can be done with function/
thunk expressions; patterns are awkward right now.
Trivial example:
? filter(match term`foo(@{d :int})` {d}, [term`bar(0)`,
> term`foo(1)`,
> term`bar(2)`])
# value: [1]
The purpose of the thunk in the expansion is to allow testing whether
a 'matcher object' matches without actually executing the body; I'm
not sure whether this additional flexibility is worthwhile.
A different possible expansion:
def _ {
to coerce(specimen__1, ejector__2) :any {
def d :(0..9) := (specimen__1, ejector__2)
return `digit $d`
}
}
This makes match expressions produce guards, which could make for
pleaseant reuses, though the concept 'coerce' doesn't really fit here.
There's also the 'run' verb, thunkless version:
def _ {
to run(specimen__1, ejector__2) :any {
def d :(0..9) := (specimen__1, ejector__2)
return `digit $d`
}
}
This protocol can be made from a guard by verb-curry (e`int.coerce`).
A guard could be made from such a matcher by having a utility to
'rename' run methods (e`renameRun("coerce", matcher)` => object which
responds to coerce/* as matcher.run(...)); renameRun could also do
the job of the one-method-object syntax.
--
Kevin Reid <http://homepage.mac.com/kpreid/>
More information about the e-lang
mailing list