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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
local spy = require "luassert.spy"
local match = require "luassert.match"
local _ = require "mason-core.functional"
describe("functional: function", function()
it("curries functions", function()
local function sum(...)
local res = 0
for i = 1, select("#", ...) do
res = res + select(i, ...)
end
return res
end
local arity0 = _.curryN(sum, 0)
local arity1 = _.curryN(sum, 1)
local arity2 = _.curryN(sum, 2)
local arity3 = _.curryN(sum, 3)
assert.equals(0, arity0(42))
assert.equals(42, arity1(42))
assert.equals(3, arity2(1)(2))
assert.equals(3, arity2(1, 2))
assert.equals(6, arity3(1)(2)(3))
assert.equals(6, arity3(1, 2, 3))
-- should discard superfluous args
assert.equals(0, arity1(0, 10, 20, 30))
end)
it("coalesces first non-nil value", function()
assert.equal("Hello World!", _.coalesce(nil, nil, "Hello World!", ""))
end)
it("should compose functions", function()
local function add(x)
return function(y)
return y + x
end
end
local function subtract(x)
return function(y)
return y - x
end
end
local function multiply(x)
return function(y)
return y * x
end
end
local big_maths = _.compose(add(1), subtract(3), multiply(5))
assert.equals(23, big_maths(5))
end)
it("should not allow composing no functions", function()
local e = assert.error(function()
_.compose()
end)
assert.equals("compose requires at least one function", e)
end)
it("should partially apply functions", function()
local funcy = spy.new()
local partially_funcy = _.partial(funcy, "a", "b", "c")
partially_funcy("d", "e", "f")
assert.spy(funcy).was_called_with("a", "b", "c", "d", "e", "f")
end)
it("should partially apply functions with nil arguments", function()
local funcy = spy.new()
local partially_funcy = _.partial(funcy, "a", nil, "c")
partially_funcy("d", nil, "f")
assert.spy(funcy).was_called_with("a", nil, "c", "d", nil, "f")
end)
it("memoizes functions with default cache mechanism", function()
local expensive_function = spy.new(function(s)
return s
end)
local memoized_fn = _.memoize(expensive_function)
assert.equal("key", memoized_fn "key")
assert.equal("key", memoized_fn "key")
assert.equal("new_key", memoized_fn "new_key")
assert.spy(expensive_function).was_called(2)
end)
it("memoizes function with custom cache mechanism", function()
local expensive_function = spy.new(function(arg1, arg2)
return arg1 .. arg2
end)
local memoized_fn = _.memoize(expensive_function, function(arg1, arg2)
return arg1 .. arg2
end)
assert.equal("key1key2", memoized_fn("key1", "key2"))
assert.equal("key1key2", memoized_fn("key1", "key2"))
assert.equal("key1key3", memoized_fn("key1", "key3"))
assert.spy(expensive_function).was_called(2)
end)
it("should evaluate functions lazily", function()
local impl = spy.new(function()
return {}, {}
end)
local lazy_fn = _.lazy(impl)
assert.spy(impl).was_called(0)
local a, b = lazy_fn()
assert.spy(impl).was_called(1)
assert.is_true(match.is_table()(a))
assert.is_true(match.is_table()(b))
local new_a, new_b = lazy_fn()
assert.spy(impl).was_called(1)
assert.is_true(match.is_ref(a)(new_a))
assert.is_true(match.is_ref(b)(new_b))
end)
it("should support nil return values in lazy functions", function()
local lazy_fn = _.lazy(function()
return nil, 2
end)
local a, b = lazy_fn()
assert.is_nil(a)
assert.equal(2, b)
end)
it("should provide identity value", function()
local obj = {}
assert.equals(2, _.identity(2))
assert.equals(obj, _.identity(obj))
end)
it("should always return bound value", function()
local obj = {}
assert.equals(2, _.always(2)())
assert.equals(obj, _.always(obj)())
end)
it("true is true and false is false", function()
assert.is_true(_.T())
assert.is_false(_.F())
end)
end)
|