-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFunctional.ark
More file actions
65 lines (60 loc) · 2.17 KB
/
Functional.ark
File metadata and controls
65 lines (60 loc) · 2.17 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
# @brief Compose function calls
# @param _f the first function
# @param _g the second function
# =begin
# (let foo (fun (a) (* a a)))
# (let bar (fun (b) (+ b b)))
# (let composed (compose foo bar))
# (print (composed 12)) # return value is (12 + 12) * (12 + 12)
# =end
# @author https://github.com/rstefanic
(let compose (fun (_f _g)
(fun (_y &_f &_g) (_f (_g _y)))))
# @brief Take a value as its argument and return a function taking 2 arguments which will call the first function on the value
# @param _x the value
# =begin
# (let val (left 12))
# (val (fun (x) (print x " i am called")) (fun (x) (print x " i am NOT called")))
# =end
# @author https://github.com/SuperFola
(let left (fun (_x)
(fun (_injl _injr &_x) (_injl _x))))
# @brief Take a value as its argument and return a function taking 2 arguments which will call the second function on the value
# @param _y the value
# =begin
# (let val (right 12))
# (val (fun (x) (print x " i am NOT called")) (fun (x) (print x " i am called")))
# =end
# @author https://github.com/SuperFola
(let right (fun (_y)
(fun (_injl _injr &_y) (_injr _y))))
# @brief Flip the arguments of a function
# @param _f the function
# @details Returns a function taking 2 arguments a and b, calling (f b a)
# =begin
# (let foo (fun (a b) (- a b)))
# ((flip foo) 14 12) # will call (foo 12 14) instead of (foo 14 12)
# =end
# @author https://github.com/rstefanic
(let flip (fun (_f)
(fun (_a _b &_f) (_f _b _a))))
# @brief No-op, return the value as-is
# @param _x the value
# =begin
# (let maybeAbs (if shouldNormalizeNegatives math:abs identity))
# (let data (list:map nums maybeAbs))
# =end
# @author https://github.com/SuperFola
(let identity (fun (_x) _x))
# @brief Generic form for functions that need to reuse their arguments
# @param _f function called with two arguments
# @param _g first unary function
# @param _h second unary function
# @details Returns a function taking one argument x, calling (f (g x) (h x))
# =begin
# (let mean (recombine (fun (a b) (/ a b)) list:sum list:size))
# (print (mean [0 1 1 2 3 5 8 13])) # 4.125
# =end
# @author https://github.com/SuperFola
(let recombine (fun (_f _g _h)
(fun (_x &_f &_g &_h) (_f (_g _x) (_h _x)))))