-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfp.lua
71 lines (60 loc) · 1.49 KB
/
fp.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
-- Functional programming functions
-- Module name: fp
-- Started 28 March 2008
-- $Id$
-- Implementations borrowed from various sources on the net and rewritten in Lua.
-- Current functions are:
-- map
-- mapn
-- reduce
-- filter (untested)
fp = {}
-- Applies the function fn to every item in list and returns a new list.
fp.map = function(fn, list)
local result = {}
for i,v in ipairs(list) do
result[i] = fn(v)
end
return result
end
-- Combines an arbitrary number of lists by using the function fn.
-- Lists should be the same length, however only calculates up to the
-- length of the shortest list.
-- Returns a list.
fp.mapn = function(fn, ...)
local result = {}
local i = 1
-- local args = ...
local arg_length = #arg
while true do
local arg_list = fp.map(function(arr) return arr[i] end, arg)
if #arg_list < arg_length then return result end
result[i] = fn(unpack(arg_list))
i = i+1
end
end
-- Takes each item of a list and combines them using the function fn.
-- Returns the result (not a list).
fp.reduce = function(fn, list, init)
local result = init
for i,v in ipairs(list) do
result = fn(result, v)
end
return result
end
-- Calls fn on the list and returns a list with the items that
-- satisfy the function fn.
fp.filter = function(fn, list)
local result = {}
local out
for i,v in ipairs(list) do
local j = 0
out = fn(v)
if out then
result[j] = out
j = j + 1
end
end
return result
end
return fp