-
Notifications
You must be signed in to change notification settings - Fork 0
/
Collections.fold
206 lines (139 loc) · 3.35 KB
/
Collections.fold
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
---
-- Flow
---
sum = do
await >>=
| None -> 0
| Some a -> a + sum
end
fold :: (a -> b -> a) -> a -> Consumer b a
fold f z = go z where
go = next >>= | Some a -> go (f acc a)
| None -> return acc
filter pred = do
a <- await
if | pred a -> yield a >> filter pred
| otherwise -> filter pred
filter = find_all
--
-- Haskell prelude version
--
filter :: (a -> Bool) -> [a] -> [a]
filter _pred [] = []
filter pred (x:xs)
| pred x = x : filter pred xs
| otherwise = filter pred xs
filter :: Foldable f => (a -> Bool) -> f a -> f a
filter _pred [] = []
filter pred (x:xs)
| pred x = x : filter pred xs
| otherwise = filter pred xs
filter f [] = []
filter f (x & xs) = if f x then x & filter f xs
else filter f xs
filter =
| pred [] -> []
| pred (x & xs) -> f x ? x & filter f xs : filter f xs
filter =
| pred [] -> []
| pred (x & xs) if pred x -> x & filter f xs
| pred (_ & xs) -> filter f xs
filter pred xs =
case xs
| [] -> []
| (x & xs) if pred x -> x & filter f xs
| (_ & xs) -> filter f xs
-- Using list builder syntax.
filter pred xs = [ x : x <- xs, pred x ]
--
-- Flow's implementation with explicit constructors
--
let rec filter pred =
fun () -> Await (fun a ->
if pred a
then fun () -> Yield (a, filter pred)
else filter pred)
-- Same as original, but with monadic syntax.
filter pred =
await >>= a ->
if pred a
then yield a >> filter pred
else filter pred
filter pred =
x <- await
if | pred x -> yield x >> filter pred
| otherwise -> filter pred
filter pred = [ x : x <- await, pred x ]
---
matrix = [1, 2, 3;
4, 5, 6]
Iter.at(matrix, 1) # => [4, 5, 6]
Iter.at(matrix, 1) |> Enum.at(2) # => 6
matrix # 1 # 2
--
-- List (Singly linked list)
--
-- Lists are polimorphic homogeneous containers.
-- An inductive type definition
type List a = [] | a & List a
-- Here's how a list of integers is created:
[1, 2, 3, 4, 5]
-- The previous syntax is desugared into an inductive chain.
1 & 2 & 3 & 4 & 5 & []
B = ^5
#B == 5
rev B == [5..1]
rev B # 5 == 1
Average ← {(+/ ω) ÷ ρ ω}
Average = A -> fold (+) A / #A
-> ^4
:: [Int] = [1, 2, 3, 4]
-> ^5 ++ ^3
:: [Int] = [1, 2, 3, 4, 5, 1, 2, 3]
-> ^4 \\ 3
:: [Int] = [1, 2, 4]
-> ^5 \\ [1, 4]
:: [Int] = [2, 3, 5]
-> ^5 \\ [2..4]
:: [Int] = [1, 5]
-> 3 in ^5
:: Bool = T
--
-- Dict (Persistent Hash Map)
--
D = {"a" => 2, "b" => 4, "c" => 8}
-> fold (dict, c -> dict # c <- 0), {}, ['a'..'z']
:: {Char => Int} = {
'a' => 0
'b' => 0
'c' => 0
...
'z' => 0
}
['a'..'z']
>> map (=> 0)
>> into {}
(map #(hash-map % 0) (seq "abcdefgh"))
map ['a'..'z']
List.fold ["a".."z"]
init: String.Map.empty
fn: dict ch -> String.Map.add dict 0
--
-- Lists
--
-- Linked Lists
fibs = [1 & 1 & zip fibs (tail fibs) with: (+)]
fibs = 1 & 1 & (zip fibs (tail fibs) with: (+))
--
-- Operations inspired by APL
--
-- https://godoc.org/robpike.io/ivy
-- Find indices in `a` that exists in `b`.
-> a = [10, 20, 30, 40, 50]
-> b = [2, 5, 10, 30]
-> filter ((a in? b) # \x) (^ (# a))
:: [Int] = [0, 2]
-- A ← 10 20 30 40 50
-- B ← 2 5 10 30
-- (A ∊ B) / ⍳⍴A
-- 1 3