aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHisham Muhammad <hisham@gobolinux.org>2018-07-17 15:59:23 -0300
committerHisham Muhammad <hisham@gobolinux.org>2018-07-18 11:24:07 -0300
commit7754f6a90d99fd6108b16d7a6771cfd6c0739544 (patch)
treecc916a6e381239f706ea5782789127ab9f931287 /src
parente5e0e691445b81be7dad2f8eb2d73c558ebbf921 (diff)
downloadluarocks-7754f6a90d99fd6108b16d7a6771cfd6c0739544.tar.gz
luarocks-7754f6a90d99fd6108b16d7a6771cfd6c0739544.tar.bz2
luarocks-7754f6a90d99fd6108b16d7a6771cfd6c0739544.zip
fun: add fun.partial and fun.flip
Diffstat (limited to 'src')
-rw-r--r--src/luarocks/fun.lua37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/luarocks/fun.lua b/src/luarocks/fun.lua
index dfdf36ed..b1740833 100644
--- a/src/luarocks/fun.lua
+++ b/src/luarocks/fun.lua
@@ -2,6 +2,8 @@
2--- A set of basic functional utilities 2--- A set of basic functional utilities
3local fun = {} 3local fun = {}
4 4
5local unpack = table.unpack or unpack
6
5function fun.concat(xs, ys) 7function fun.concat(xs, ys)
6 local rs = {} 8 local rs = {}
7 local n = #xs 9 local n = #xs
@@ -63,4 +65,39 @@ function fun.sort_in(t, f)
63 return t 65 return t
64end 66end
65 67
68function fun.flip(f)
69 return function(a, b)
70 return f(b, a)
71 end
72end
73
74function fun.partial(f, ...)
75 local n = select("#", ...)
76 if n == 1 then
77 local a = ...
78 return function(...)
79 return f(a, ...)
80 end
81 elseif n == 2 then
82 local a, b = ...
83 return function(...)
84 return f(a, b, ...)
85 end
86 else
87 local pargs = { n = n, ... }
88 return function(...)
89 local m = select("#", ...)
90 local fargs = { ... }
91 local args = {}
92 for i = 1, n do
93 args[i] = pargs[i]
94 end
95 for i = 1, m do
96 args[i+n] = fargs[i]
97 end
98 return f(table.unpack(args, 1, n+m))
99 end
100 end
101end
102
66return fun 103return fun