LPeg as an immutable prefix map

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

LPeg as an immutable prefix map

Soni "They/Them" L.
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.


Reply | Threaded
Open this post in threaded view
|

Re: LPeg as an immutable prefix map

Nagaev Boris
On Sat, Dec 17, 2016 at 9:56 PM, Soni L. <[hidden email]> wrote:

> 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.
>
>

Cool!

Does it compress shared prefixes?

E.g., if you add a million strings of length 500 starting with shared
prefix of length 495, would the bytecode length ~ 500 instructions or
~ 1 million instructions?

--
Best regards,
Boris Nagaev