[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [ANN] tamale-1.2 - Erlang-style pattern matching library
- From: Scott Vokes <vokes.s@...>
- Date: Sun, 26 Sep 2010 10:04:31 -0400
On Sun, Sep 26, 2010 at 9:45 AM, Henning Diedrich <hd2010@eonblast.com> wrote:
> Find some brilliant, free enlightenment here:
> http://learnyousomeerlang.com/syntax-in-functions#pattern-matching
> and here: http://learnyouahaskell.com/syntax-in-functions#pattern-matching
The pattern matching in tamale is closest to the PM in Erlang's
receive or case clauses (like the "In Case ... of" section in the
Erlang example), since tamale doesn't work at the top
function-definition scope* , and Haskell's static typing makes things
a bit different as well.
It may also help to note that in Erlang syntax, lowercase IDs is
atoms (interned strings / symbols) and uppercase IDs are variables, so
the beach example would look like this:
[[
local function between(key, x, y)
return function(cs)
local v = cs[key]
return v >= x and v <= y
end
end
local beach = tamale.matcher {
{ {"celsius", V"N"}, when=between(20, 45),
"favorable" },
{ {"kelvin", V"N"}, when=between(293, 318),
"scientifically favorable" },
{ {"fahrenheit", V"N"}, when=between(68, 113),
"favorable in the US" },
{ V"_", "avoid beach" },
}
]]
(and it looks this caught a bug in recent changes to the way guards
work with indexing, so I'm adding this to the test suite...)
* which is too bad, it's really nice syntax IMHO, but that would
probably require a LOT of syntactic sugar to make it work in Lua.
Scott