lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


You can use LPeg as an immutable prefix map (copypasted from REPL):

$ lua
Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
> lpeg=require"lpeg"
> a = lpeg.P"hello"
> b = lpeg.P"world"
> av = lpeg.Cc({v=1})
> bv = lpeg.Cc({v=2})
> function add(t, k, v) --> t
>> return k * v + t
>> end
> t = lpeg.P(false)
> t = add(t, a, av)
> t = add(t, b, bv)
> print(lpeg.match(t, "hello world").v)
1
> print(lpeg.match(t, "world hello").v)
2
> print(lpeg.match(t, "nil"))
nil
> nav = lpeg.Cc({v=3})
> t = add(t, a, nav) -- replace a value (this is why the `add` function uses `k * v + t` not `t + k * v`)
> print(lpeg.match(t, "hello world").v)
3
>

Cool, eh?

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.