~nasser/8fl

8fl/seq.fnl -rw-r--r-- 4.5 KiB
2934f9e8Ramsey Nasser Use negative track numbers for fx columns a month ago
                                                                                
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
203
204
205
206
207
208
209
210
211
212
213
;; TODO figure out macro modules
(require-macros :core-macros)

(fn is-seq-sugar? [x]
    (string.match x ":$"))

;; TODO should (iter :foo) -> (forever :foo) ?
(fn iter [xs]
  "turns xs, expected to be either a function or an array, into an iterator.
  
   an iterator is a function that, when invoked, returns the next element in the
   sequence"
  (match (type xs)
         :function xs
         :table (resumable
                  (each [_ x (ipairs xs)]
                    (coroutine.yield x)))
         :string (if (is-seq-sugar? xs)
                     (string.gmatch (string.sub xs 1 -2) ".")
                     (resumable (coroutine.yield xs)))
         _ (resumable (coroutine.yield xs))))

(fn vec [xs]
  "turns xs, expected to be either a function or an array, into an vector.
   
   a vector is a lua array. this function is named to match clojure."
  (match (type xs)
    :table xs
    _
    (let [ret []]
      (each [x (iter xs)]
            (table.insert ret x))
      ret)))

(fn first [xs]
  (match (type xs)
    :function (xs)
    :table (. xs 1)
    _ ((iter xs))))

(fn last [xs]
  (var val nil)
  (each [x (iter xs)]
    (set val x))
  val)

(fn nth [xs n]
  (var n n)
  (each [x (iter xs)]
    (set n (- n 1))
    (if (= n 0)
      (lua "return x")))
  nil)

(fn range [from to step]
  (resumable
    (var from* (if to from 1))
    (var to* (+ 1 (if to to from)))
    (var step* (if step step 1))
    (while (< from* to*)
      (coroutine.yield from*)
      (set from* (+ from* step*)))))

(fn range* [from to step]
  (resumable
    (var from* (if to from 1))
    (var to* (+ 1 (if to to from)))
    (var step* (if step step 2))
    (while (< from* to*)
      (coroutine.yield from*)
      (set from* (* from* step*)))))

(fn map [xs f]
  (resumable
    (each [x (iter xs)]
      (coroutine.yield (f x)))))

(fn map-cat [xs f]
  (resumable
   (each [x (iter xs)]
     (each [y (iter (f x))]
       (coroutine.yield y)))))

(fn mapv [xs f]
  (-> xs (map f) vec))

(fn map-if [xs pred f]
  (resumable
   (each [x (iter xs)]
     (if (pred x)
       (coroutine.yield (f x)) 
       (coroutine.yield x)))))

(fn filter [xs f]
  (resumable
    (each [x (iter xs)]
      (when (f x)
        (coroutine.yield x)))))

(fn remove [xs f]
  (resumable
    (each [x (iter xs)]
      (when (not (f x))
        (coroutine.yield x)))))

(fn keep [xs f]
  (resumable
   (each [x (iter xs)]
     (let [x* (f x)]
       (when x*
         (coroutine.yield x*))))))

(fn take [xs n]
  (resumable
    (var n n)
    (each [x (iter xs)]
      (coroutine.yield x)
      (set n (- n 1))
      (when (= n 0)
        (lua :return)))))

(fn drop [xs n]
  (resumable
   (var n n)
   (each [x (iter xs)]
     (if (> n 0)
       (set n (- n 1))
       (coroutine.yield x)))
   (coroutine.yield nil)))

(fn drop-while [xs f]
  (resumable
   (var dropping true)
   (each [x (iter xs)]
     (if dropping
       (set dropping (f x)))
     (if (not dropping)
       (coroutine.yield x)))))

(fn cycle [xs]
  (local cache [])
  (coroutine.wrap
    #(do
      (each [x (iter xs)]
        (table.insert cache x)
        (coroutine.yield x))
      (while true
        (each [i x (ipairs cache)]
          (coroutine.yield x))))))

(fn interleave [...]
  (local coros [...])
  (each [i c (ipairs coros)]
    (tset coros i (iter c)))
  (resumable
    (while true
      (each [_ c (ipairs coros)]
        (match (c)
            nil (lua :return)
            v (coroutine.yield v))))))

(fn forever [v]
  (coroutine.wrap
    #(while true
      (coroutine.yield v))))

(fn repeat [xs n]
  (resumable
    (var n n)
    (while (> n 0)
      (each [x (iter xs)]
        (coroutine.yield x))
      (set n (- n 1)))))

(fn concat [...]
  (local vals [...])
  (resumable
    (each [_ xs (ipairs vals)]
      (each [x (iter xs)]
        (coroutine.yield x)))))

(fn reduce [xs f init]
  (var ret init)
  (each [x (iter xs)]
    (set ret (f ret x)))
  ret)

(fn reverse [xs]
  (resumable
    (let [v (vec xs)
          len (# v)]
      (for [i 0 len]
        (coroutine.yield (. v (- len i)))))))

(fn distinct [xs]
  (resumable
   (var vals {})
   (each [x (iter xs)]
    (when (not (. vals x))
      (tset vals x true)
      (coroutine.yield x)))))

;; TODO maybe move to patterns?
(fn linger [xs n]
  (let [n (cycle (or n 1))]
    (resumable
      (each [x (iter xs)]
        (for [_ 1 (n)]
          (coroutine.yield x))))))

{: iter : vec : first : range : range* : map : mapv : filter : take : drop : cycle
 : interleave : forever : repeat : concat : reduce : reverse : map-if : keep
 : distinct   : map-cat : drop-while : last : nth : linger }