lua-users home
lua-l archive

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


Hello all, first time posting on Lua List, though I have been reading
many older highly informative posts over the past year.

I just put a functional library on github: https://github.com/davidhollander/fn

I've to factor in most of the discussion on lua-users I've come across
into my decisions. Unlike some other functional libraries such as
lua-functional http://www.samsarin.com/blog/lua-functional/, it
preserves ending nil values of multi-arity arguments by using
"select('#', ...) which I feel is more elegant solution. It does not
override or create iterators. It aims to support manipulation of
ordered data using traditional Lua tables and Lua multi arity
arguments. Those who are concerned about preserving sequences of 'nil'
will gravitate to multiarity arguments\tuples, and those not concerned
are most likely manipulating data and can use standard lua tables in
their normal manner. Methods that expect table instead of a a variable
arity argument end with a "-t" suffix. I feel this is a more natural
solution in practice than turning the table into a new array data
type.

Example: create a "sum" function using partial. Partial creates a
closure that calls Fold Left with an addition function as the first
argument, with a variable arity of additional arguments.

require 'fn'
sum = fn.partial( fn.foldl, function(a, b) return a+b end)
x = sum(1, 2, 3, 4, 5, 6, 7, 8, 9)
print(x)
-- 45


If anyone would like me to create a Rockspec, let me know. I was
getting path assertion errors when trying to add the file as a
'module' using the 'builtin' build type. The library will remain a
single file, but I can try and get the rockspec working again if it
would be useful for someone. If anyone would like to offer feedback
and suggest variants or functions for inclusion (ex. a "range"
function) feel free to do so via mailing list, email, or github.

David Hollander