This repository has been archived by the owner on Dec 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.lua
74 lines (58 loc) · 1.39 KB
/
main.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
require('util')
require('match')
require('pure')
require('functional')
function fib (n)
if n <= 1 then
return 1
else
return fib(n - 1) + fib(n - 2)
end
end
pure.define('pi', 3.14159)
function degToRad (n)
return n * (pi / 180)
end
print_hello = pure.unsafe(function ()
print('Hello')
end)
print_hello()
print(fib(40))
print(fib(80))
print(degToRad(180))
pass, fail = functional.partition({ 'a', 1, 'b', 2, 'c', 5 }, function (item)
return type(item) == "number"
end)
print(util.table_tostring(pass))
print(util.table_tostring(fail))
print(util.table_tostring(functional.map({ 'a', 'b', 'c'}, function (str)
return str .. 'bar'
end)))
print(util.table_tostring(functional.filter({ 'a', 'b', 'c'}, function (str)
return (string.sub(str, 0, 1) == 'a')
end)))
local str = 'foobar'
local f = functional.curry(string.sub)
local bar = f(str)(4)
print(bar)
local g = functional.uncurry(f)
print(g(bar, 1, 2))
local h = match.define(
{ 1 }, [[return 5]],
{ 2 }, [[return 11]],
{ any }, function (x) return x * 2 end,
{ any, 5 }, function (x) return x / 2 end,
{ any, any },
function (x, y)
return x * y
end
)
print(h(1))
print(h(2))
print(h(3))
print(h(3, 5))
print(h(3, 6))
print(util.table_tostring(functional.flatten({ { 5 }, {{{{ "hello" }}}}, {{ "bar" }} })))
local tbl = { "foo", 2, 3, "bar", 5.5 }
print(tostring(functional.head(tbl)))
print(util.table_tostring(functional.tail(tbl)))