/
Functional.lua
183 lines (141 loc) · 4.02 KB
/
Functional.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
--[[
Carbon for Lua
#class Functional
#description {
Provides an interface for functional programming.
*This module may be slated for significant changes in the future.*
}
]]
local Carbon = (...)
local Operators = Carbon.Operators
local Functional = {}
--[[#method {
class public @tuple Functional.Unpack(@table object)
required object: The table to unpack.
Unpacks the table into a tuple. The same as `unpack` and `table.unpack`.
**DEPRECATED** in 1.1: Use Carbon.Unpack (added in 1.0.1)
}]]
Functional.Unpack = Carbon.Deprecated(unpack or table.unpack, "Functional.Unpack")
--[[#method {
class public @bool Functional.AllTuple(...)
required ...: The values to check for truthiness.
Returns whether all the values in the tuple are truthy.
}]]
function Functional.AllTuple(...)
for i = 1, select("#", ...) do
if (not select(i, ...)) then
return false
end
end
return true
end
--[[#method {
class public @bool Functional.All(@table objects)
required objects: The values to check for truthiness.
Returns whether all the values in the table are truthy.
}]]
function Functional.All(t)
for key, value in ipairs(t) do
if (not value) then
return false
end
end
return true
end
--[[#method {
class public @bool Functional.AnyTuple(...)
required ...: The values to check for truthiness.
Returns whether any one of the values in the tuple is truthy.
}]]
function Functional.AnyTuple(...)
for i = 1, select("#", ...) do
if (select(i, ...)) then
return true
end
end
return false
end
--[[#method {
class public @bool Functional.Any(@table objects)
required objects: The values to check for truthiness.
Returns whether any one of the values in the table is truthy.
}]]
function Functional.Any(t)
for key, value in ipairs(t) do
if (value) then
return true
end
end
return false
end
--[[#method {
class public @userdata Functional.ViewReverse(@list list)
required list: The list to create a view for.
Creates a view that returns all values of the list reversed.
}]]
function Functional.ViewReverse(list)
local view = newproxy(true)
getmetatable(view).__index = function(self, key)
return list[#self - key + 1]
end
getmetatable(view).__newindex = function(self, key, value)
list[#self - key + 1] = value
end
return view
end
--[[#method {
class public @list Functional.Range(number a, number b)
required a: The number to start at.
required b: The number to end at.
Creates a range in the form of a @list.
}]]
function Functional.Range(a, b)
local out = {}
for i = a, b, Operators.Sign(b - a) do
table.insert(out, i)
end
return out
end
--[[#method {
class public T Functional.Reduce(@function T method(T total, T current), @list list)
required method: The method to use for reduction. Accepts the total and the current value, returns the new total.
required list: The list of values to reduce over.
Performs a reduction over the list with the given function.
}]]
function Functional.Reduce(method, list)
local total = list[1]
for i = 2, #list do
total = method(total, list[i])
end
return total
end
--[[#method {
class public @list Functional.Map(@function @any method(@any value), @list list)
required method: The method to use for mapping. Takes in a value and returns a transformed version of it.
required list: The list of values to map over.
Uses a function to perform a mapping of values.
This method (erroneously) modified the list before 1.1.
}]]
function Functional.Map(method, list)
local out = {}
for key, value in ipairs(list) do
out[key] = method(value)
end
return out
end
--[[#method {
class public @list Functional.Filter(@function @bool method(@any value), @list list)
required method: The method to use for filtering. Takes in a valid and returns whether it should stay.
required list: The list of values to filter over.
Uses a function to filter a list of values.
}]]
function Functional.Filter(method, list)
local out = {}
for key, value in ipairs(list) do
if (method[value]) then
table.insert(out, value)
end
end
return out
end
return Functional