-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsequence.lua
More file actions
70 lines (55 loc) · 1.74 KB
/
Copy pathsequence.lua
File metadata and controls
70 lines (55 loc) · 1.74 KB
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
--
-- sequence - simple oop wrapper on ordered tables, with method chaining
--
require("common")
-------------------------------------------------------------------------------
heading("seamless")
-- everything in table and tablex work basically out of the box
-- here is the stack and queue section from the table example
-- translated, with comments stripped - looking at both
-- might help to see how things map
local t = sequence{1, 2, 3, 4, 5}
print_var("t", t)
print("front", t:front())
print("back", t:back())
v = t:pop()
print("popped", v)
print_var("after pop", t)
t:push(v)
print_var("after push", t)
v = t:shift()
print("shifted", v)
print_var("after shift", t)
t:push(v)
print_var("after push", t)
t:unshift(t:pop())
print_var("replace at front", t)
-------------------------------------------------------------------------------
heading("functional and type preserving")
-- anything sequential returned from a sequence method is also a sequence
-- allows method chaining for a sequentially readable functional approach
-- not for use in some hot inner loop, but leads to very readable code
local doubled_and_squared = t:map(function(v)
return v * 2
end):map(function(v)
return v * v
end)
print_var("doubled, squared", doubled_and_squared)
local min, max = doubled_and_squared:minmax()
print("min, max", min, max)
--can be used to upgrade some existing table transparently too
--(generate data here, but it could come from anywhere and look like anything)
local preexisting = functional.generate(10, function()
return {length = love.math.random() * 100}
end)
--find the mean length
local mean_length =
--convert
sequence(preexisting)
--map values
:map(function(v)
return v.length
end)
--get the mean
:mean()
print("mean length", mean_length)