lua-users home
lua-l archive

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


On Wed, 12 Apr 2023 at 17:41, Andrew Gierth <andrew@tao11.riddles.org.uk> wrote:
>>>>> "Sean" == Sean Conner <sean@conman.org> writes:

 >> > (Cc"a" * Cc"b" / f):Cg"c" :ptmatch("ccc")
 >> {"c"="ab"}
 >>
 >> How do I modify the ".../f" above to make it put the first result of f
 >> into :Cg"c" and the second result into :Cg"d"?

 Sean> If I understand your question correctly, for

 Sean>  Cc'a' * Cc'b' / f

 Sean> you want the first result of f() to go into a field named 'c',
 Sean> and the second result of f() to go into a field named 'd'. Yes,
 Sean> that isn't going to work.

Well... sorta.

((Cc"a" * Cc"b" / f):Cg"c" * (Cb"c" / 2):Cg"d"):ptmatch("ccc");

This puts the list of captures returned by f into named capture "c", and
then extracts the second one into "d" by using a back capture. When Ct()
is applied to the result, only the first value of "c" is used, so the
result is { c="ab", d="ba" }.

Note, though, that in my tests (on 5.3) this ended up evaluating f()
twice (the back captures are intentionally unspecified as to whether
they re-evaluate or reuse a previous evaluation). So this maybe isn't
going to work well if f() is costly or impure.

--
Andrew.

Whoa!!!

That's brilliant! I didn't know that a back capture could store
several values! Many thanks! =) =) =)
  Cheers,
    Eduardo Ochs
    http://anggtwu.net/luaforth.html