aboutsummaryrefslogtreecommitdiffstats
path: root/core/common.milch
blob: 2dc66980ac22249542c608d3fc9065c5c2578aca (plain)
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
184
;; Control flow
;;;;;;;;;;;;;;;;;;;;;;;;;

; function composition
(let . (\[f g x] (f (g x))))

; impure version of .
(let .! (\![f g x] (f (g x))))

(let flow (\[fs]
    (foldr . id (reverse fs))))

(let pipe (\[x fs]
    ((flow fs) x)))

;; Standard functions
;;;;;;;;;;;;;;;;;;;;;;;;;

(let id (\[a] a))

;; Logic
;;;;;;;;;;;;;;;;;;;;;;;;;

(let not (\[b]
    (match b
        true false
        false true)))

(let and (\[a b]
    (match a
        true b
        false (not b))))

(let or (\[a b]
    (match a
        true true
        false b)))

(let xor (\[a b]
    (match a
        true (not b)
        false b)))

;; Math
;;;;;;;;;;;;;;;;;;;;;;;;;

(let inc (+ 1))
(let dec (\[n] (- n 1)))

(let PI 3.141592653589793238)
(let E  2.718281828459045235)

(let mod (\[n k]
    (- n (* k (/ n k)))))

(let even? (\[n]
    (match (mod n 2)
        0 true
        1 false)))

(let odd? (. not even?))

(let leq? (\[a b]
    (or
        (eq? a b)
        (lt? a b))))

(let gt? (\[a] (. not (leq? a))))
(let geq? (\[a] (. not (lt? a))))

(let max (\[a b]
    (match (lt? a b)
        true    b
        false   a)))

(let min (\[a b]
    (match (lt? a b)
        true    a
        false   b)))

;; Vector operations
;;;;;;;;;;;;;;;;;;;;;;;;;

(let sort-by (\[keyf vals]
    (let sorted (sort-by-first
        (map (\[v]
            [(keyf v) v]) vals)))

    (map (. Result/Ok/get-value (at 1)) sorted)))

(let take (\[n xs]
    (match n
        0          []
        otherwise  (prepend (head xs) (take (- n 1) (tail xs))))))

(let drop (\[n xs]
    (match n
        0          xs
        otherwise  (drop (- n 1) (tail xs)))))

; map :: (a -> b) -> [a] -> [b]
(let map (\[f lst]
    (match lst
        []          []
        otherwise   (prepend (f (head lst)) (map f (tail lst))))))

; foldr :: (a -> b -> b) -> b -> [a] -> b
(let foldr (\[f accumulator lst]
    (match lst
        []          accumulator
        otherwise   (f (head lst) (foldr f accumulator (tail lst))))))

; filter :: (a -> Bool) -> [a] -> [a]
(let filter (\[pred lst]
    (match lst
        []          []
        otherwise   (match (pred (head lst))
            true
                (prepend (head lst) (filter pred (tail lst)))
            false
                (filter pred (tail lst))))))

(let _reverse (\[v a]
    (let x (head v))
    (let xs (tail v))
    (let xa (prepend x a))
    (match v
        [] a
        _  (_reverse xs xa))))

(let reverse (\[v]
    (_reverse v [])))

(let _split-by (\[delim acc vals]
    (let v (head vals))
    (let vs (tail vals))

    (match vs
        []          (match v
            delim       [(reverse acc)]
            otherwise   [(prepend v (reverse acc))])
        otherwise   (match v
            delim       (prepend (reverse acc) (_split-by delim [] vs))
            otherwise   (_split-by delim (prepend v acc) vs)))))

; split vector by delimiter
(let split-by (\[delim vals]
    (_split-by delim [] vals)))

(let at! (\![n seq]
    (match seq
        []          (fatal! (fmt "at out of bounds, index: {0}" [n]))
        otherwise   (match n
            0           (head seq)
            otherwise   (at! (- n 1) (tail seq))))))

(let at (\[n seq]
    (match seq
        []          (Result/ex (fmt "at out of bounds, index: {0}" [n]))
        otherwise   (match n
            0           (Result/ok (head seq))
            otherwise   (at (- n 1) (tail seq))))))

(let maximum (\[vals]
    (let v (head vals))
    (let vs (tail vals))

    (match vs
        []          v
        otherwise   (max v (maximum vs)))))

(let minimum (\[vals]
    (let v (head vals))
    (let vs (tail vals))

    (match vs
        []          v
        otherwise   (min v (minimum vs)))))

;; IO
;;;;;;;;;;;;;;;;;;;;;;;;;

(let print-fmt! (\![fstr args]
    (print! (fmt fstr args))))