Hi Beta Testers
I have been thinking about adding common functional programming operations to Codea as part of the standard library. Basically map
, reduce
, filter
and so on. I find that I use these more and more in programming, avoiding explicit loops where possible. Not for performance, but for composeability and readability.
I’d like to include an established Lua-based functional library in an update, but wanted to get your thoughts.
(I’m looking specifically at this library http://rtsisyk.github.io/luafun/reference.html)
Some possible examples:
Apply a transform to all points
xform = matrix()
points = { ... }
px = map(function(p)
return xform * p
end, points)
Take the first two touches and convert them to vec2
touches = { ... }
coords = map(function(touch)
return vec2(touch.x, touch.y)
end, take(touches,2))
Remove a touch with a specific ID from a list of touches
touch = ...
touches = filter(function(t)
return touch.id ~= t.id
end, touches)
Draw 10 randomly positioned ellipses
each(function(x, y)
ellipse(x, y, 100)
end, take(10, zip(rands(0,WIDTH), rands(0,HEIGHT))))
There are many more simple composeable functions and generators available, and because the above library treats strings as iterable sequences you can use these functions on strings wherever you would use a table.
I haven’t yet used this particular library, but it seems well thought out and documented.