Friday, October 23, 2009

List Library for Lua

I've updated my List Library for Lua which is inspired by Haskell's Data.List library.

I've had some interesting discussions about possibly specifying the arity of the list functions like for map() and filter(). Other implementations of these list functions take only one list, and any extra arguments are passed to the function called by map().

This lead to me trying an implementation where the 2nd argument to map() was the arity of the function passed in, call it N. The next N arguments are lists to be iterated over, and any arguments beyond that are passed as-is to the function. A quick example can easily illustrate:

function div(x, y) return x / y end

map(div, 2, {99, 21, 6}, {11, 7, 3}) -> {9, 3, 2}

map(div, 1, {99, 21, 6}, 3) -> {33, 7, 2}

Finally though I decided (at least for my own library) that explicitly specifying the arity was too cumbersome. If you need to a more sophisticated used is needed, some kind of wrapper for currying will be needed. Here's the 2nd use of map() above:

map(function(x) return div(x, 3) end, {99, 21, 6}) -> {33, 7, 2}

Obviously this is a little clunky. If you're using MetaLua lambda construct (or the power patch), this is considerably shortened:

map(|x| div(x, 3), {99, 21, 6}) -> {33, 7, 2}

I would really like the lambda construct accepted into mainline Lua.

No comments: