[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [ANN] tamale-1.2 - Erlang-style pattern matching library
- From: David Manura <dm.lua@...>
- Date: Sat, 25 Sep 2010 17:15:34 -0400
On Sat, Sep 25, 2010 at 2:02 PM, Scott Vokes <vokes.s@gmail.com> wrote:
> The new version is tagged as v1.2.1
Here's more test cases:
local tamale = require "tamale"
local M = tamale.matcher
-- test composed patterns (alternation)
local m2 = M { {1, true}, {2, true} }
local m = M{{ {m2, m2}, 3 }}
print(m {1, 2}) --> 3, table
print(m {1, 3}) --> false, "Match failed"
-- test false/nil result (ok?)
print(M {{1}} (1)) --> nil, table
print(M {{1}} (2)) --> false, "Match failed", 2
print(M {{1, false}} (1)) --> false, table
-- test match function values
--FAILS: print(M {{math.sqrt, true}} (math.sqrt) )
local function issqrt(o) return o == math.sqrt end -- alternative
print(M {{issqrt, true}} (math.sqrt) ) --> true, table
-- test match tuples (expression lists)
local function tuple(...) return {n=select('#', ...), ...} end
print(M {{tuple(2,3), true}} (tuple(2,3))) --> true, table
print(M {{tuple(2,3), true}} (tuple(2,3,nil))) --> false, ...
-- test match approximate
local function approx(a) return function(b) return math.abs(a-b) < 1 end end
print(M {{tuple(approx(5), approx(10)), true}} (tuple(5.1, 10.1))) -->
true, table
print(M {{tuple(approx(5), approx(10)), true}} (tuple(5.1, 11.1))) -->
false, ...
> I also added a special variable, V"...", which sets the partial flag
> and captures all subsequent array-portion values. (e.g. { "foo, V"..." }).
Maybe decompose V"..." into Star(V"_"). This generalization allows
{Star(isnumber)} to match an array of numbers.