aboutsummaryrefslogtreecommitdiff
path: root/source/lux/data/list.lux
blob: b2049d419ddf466140fad5f2e26ea2a9382d0439 (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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
##  Copyright (c) Eduardo Julian. All rights reserved.
##  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
##  If a copy of the MPL was not distributed with this file,
##  You can obtain one at http://mozilla.org/MPL/2.0/.

(;import lux
         (lux (control (monoid #as m #refer #all)
                       (functor #as F #refer #all)
                       (monad #as M #refer #all)
                       (eq #as E)
                       (ord #as O)
                       (fold #as f))
              (data (number (int #open ("i" Int/Number Int/Ord)))
                    bool)
              meta/macro))

## [Types]
## (deftype (List a)
##   (| #Nil
##      (#Cons (, a (List a)))))

## [Functions]
(def #export (foldL f init xs)
  (All [a b]
    (-> (-> a b a) a (List b) a)) 
  (case xs
    #;Nil
    init

    (#;Cons [x xs'])
    (foldL f (f init x) xs')))

(def #export (foldR f init xs)
  (All [a b]
    (-> (-> b a a) a (List b) a)) 
  (case xs
    #;Nil
    init

    (#;Cons [x xs'])
    (f x (foldR f init xs'))))

(defstruct #export List/Fold (f;Fold List)
  (def (foldL f init xs)
    (case xs
      #;Nil
      init

      (#;Cons [x xs'])
      (foldL f (f init x) xs')))
  
  (def (foldR f init xs)
    (case xs
      #;Nil
      init

      (#;Cons [x xs'])
      (f x (foldR f init xs')))))

(def #export (fold mon xs)
  (All [a]
    (-> (m;Monoid a) (List a) a))
  (using mon
    (foldL ++ unit xs)))

(def #export (reverse xs)
  (All [a]
    (-> (List a) (List a)))
  (foldL (lambda [tail head] (#;Cons [head tail]))
         #;Nil
         xs))

(def #export (filter p xs)
  (All [a]
    (-> (-> a Bool) (List a) (List a)))
  (case xs
    #;Nil
    #;Nil
    
    (#;Cons [x xs'])
    (if (p x)
      (#;Cons [x (filter p xs')])
      (filter p xs'))))

(def #export (partition p xs)
  (All [a] (-> (-> a Bool) (List a) (, (List a) (List a))))
  [(filter p xs) (filter (complement p) xs)])

(def #export (as-pairs xs)
  (All [a] (-> (List a) (List (, a a))))
  (case xs
    (\ (#;Cons [x1 (#;Cons [x2 xs'])]))
    (#;Cons [[x1 x2] (as-pairs xs')])

    _
    #;Nil))

(do-template [<name> <then> <else>]
  [(def #export (<name> n xs)
     (All [a]
       (-> Int (List a) (List a)))
     (if (i> n 0)
       (case xs
         #;Nil
         #;Nil
         
         (#;Cons [x xs'])
         <then>)
       <else>))]
  
  [take (#;Cons [x (take (i+ -1 n) xs')]) #;Nil]
  [drop (drop (i+ -1 n) xs') xs]
  )

(do-template [<name> <then> <else>]
  [(def #export (<name> p xs)
     (All [a]
       (-> (-> a Bool) (List a) (List a)))
     (case xs
       #;Nil
       #;Nil
       
       (#;Cons [x xs'])
       (if (p x)
         <then>
         <else>)))]

  [take-while (#;Cons [x (take-while p xs')]) #;Nil]
  [drop-while (drop-while p xs') xs]
  )

(def #export (split n xs)
  (All [a]
    (-> Int (List a) (, (List a) (List a))))
  (if (i> n 0)
    (case xs
      #;Nil
      [#;Nil #;Nil]
      
      (#;Cons [x xs'])
      (let [[tail rest] (split (i+ -1 n) xs')]
        [(#;Cons [x tail]) rest]))
    [#;Nil xs]))

(def (split-with' p ys xs)
  (All [a]
    (-> (-> a Bool) (List a) (List a) (, (List a) (List a))))
  (case xs
    #;Nil
    [ys xs]

    (#;Cons [x xs'])
    (if (p x)
      (split-with' p (#;Cons [x ys]) xs')
      [ys xs])))

(def #export (split-with p xs)
  (All [a]
    (-> (-> a Bool) (List a) (, (List a) (List a))))
  (let [[ys' xs'] (split-with' p #;Nil xs)]
    [(reverse ys') xs']))

(def #export (repeat n x)
  (All [a]
    (-> Int a (List a)))
  (if (i> n 0)
    (#;Cons [x (repeat (i+ -1 n) x)])
    #;Nil))

(def #export (iterate f x)
  (All [a]
    (-> (-> a (Maybe a)) a (List a)))
  (case (f x)
    (#;Some x')
    (#;Cons [x (iterate f x')])

    #;None
    (#;Cons [x #;Nil])))

(def #export (some f xs)
  (All [a b]
    (-> (-> a (Maybe b)) (List a) (Maybe b)))
  (case xs
    #;Nil
    #;None

    (#;Cons [x xs'])
    (case (f x)
      #;None
      (some f xs')

      (#;Some y)
      (#;Some y))))

(def #export (interpose sep xs)
  (All [a]
    (-> a (List a) (List a)))
  (case xs
    #;Nil
    xs

    (#;Cons [x #;Nil])
    xs

    (#;Cons [x xs'])
    (#;Cons [x (#;Cons [sep (interpose sep xs')])])))

(def #export (size list)
  (-> List Int)
  (foldL (lambda [acc _] (i+ 1 acc)) 0 list))

(do-template [<name> <init> <op>]
  [(def #export (<name> p xs)
     (All [a]
       (-> (-> a Bool) (List a) Bool))
     (foldL (lambda [_1 _2] (<op> _1 (p _2))) <init> xs))]

  [every? true  and]
  [any?   false or])

(def #export (@ i xs)
  (All [a]
    (-> Int (List a) (Maybe a)))
  (case xs
    #;Nil
    #;None

    (#;Cons [x xs'])
    (if (i= 0 i)
      (#;Some x)
      (@ (i+ -1 i) xs'))))

## [Syntax]
(defmacro #export (@list xs state)
  (#;Right state (#;Cons (foldL (: (-> AST AST AST)
                                   (lambda [tail head] (` (#;Cons (~ head) (~ tail)))))
                                (: AST (` #;Nil))
                                (reverse xs))
                         #;Nil)))

(defmacro #export (@list& xs state)
  (case (reverse xs)
    (#;Cons last init)
    (#;Right state (@list (foldL (: (-> AST AST AST)
                                    (lambda [tail head] (` (#;Cons (~ head) (~ tail)))))
                                 last
                                 init)))

    _
    (#;Left "Wrong syntax for @list&")))

## (defmacro #export (zip tokens state)
##   (if (i> (size tokens) 0)
##     (using List/Functor
##       (let [indices (range 0 (i+ 1 (size tokens)))
##             vars+lists (map (lambda [idx]
##                               (let [base (text:++ "_" idx)]
##                                 [[["" -1 -1] (#SymbolS "" base)]
##                                  [["" -1 -1] (#SymbolS "" (text:++ base "s"))]]))
##                             indices)
##             pattern (` [(~@ (map (lambda [[v vs]] (` (#;Cons (~ v) (~ vs))))
##                                  vars+lists))])
##             g!step [["" -1 -1] (#SymbolS "" "\tstep\t")]
##             g!arg [["" -1 -1] (#SymbolS "" "\targ\t")]
##             g!blank [["" -1 -1] (#SymbolS "" "\t_\t")]
##             code (` ((lambda (~ g!step) [(~ g!arg)]
##                         (case (~ g!arg)
##                           (~ pattern)
##                           (#;Cons [(~@ vars)] ((~ g!step) [(~ (map second vars))]))

##                           (~ g!blank)
##                           #;Nil))
##                      [(~@ tokens)]))]
##         (#;Right state (@list code))))
##     (#;Left "Can't zip no lists.")))

## (defmacro #export (zip-with tokens state)
##   (case tokens
##     (@list& _f tokens)
##     (case _f
##       [_ (#;SymbolS _)]
##       (if (i> (size tokens) 0)
##         (using List/Functor
##           (let [indices (range 0 (i+ 1 (size tokens)))
##                 vars+lists (map (lambda [idx]
##                                   (let [base (text:++ "_" idx)]
##                                     [[["" -1 -1] (#SymbolS "" base)]
##                                      [["" -1 -1] (#SymbolS "" (text:++ base "s"))]]))
##                                 indices)
##                 pattern (` [(~@ (map (lambda [[v vs]] (` (#;Cons (~ v) (~ vs))))
##                                      vars+lists))])
##                 g!step [["" -1 -1] (#SymbolS "" "\tstep\t")]
##                 g!arg [["" -1 -1] (#SymbolS "" "\targ\t")]
##                 g!blank [["" -1 -1] (#SymbolS "" "\t_\t")]
##                 code (` ((lambda (~ g!step) [(~ g!arg)]
##                             (case (~ g!arg)
##                               (~ pattern)
##                               (#;Cons ((~ _f) (~@ vars)) ((~ g!step) [(~ (map second vars))]))

##                               (~ g!blank)
##                               #;Nil))
##                          [(~@ tokens)]))]
##             (#;Right state (@list code))))
##         (#;Left "Can't zip-with no lists."))
      
##       _
##       (let [g!temp [["" -1 -1] (#SymbolS "" "\ttemp\t")]]
##         (#;Right state (@list (` (let [(~ g!temp) (~ _f)]
##                                    (;;zip-with (~@ (@list& g!temp tokens)))))))))

##     _
##     (#;Left "Wrong syntax for zip-with")))

## [Structures]
## (defstruct #export (List/Eq eq) (All [a] (-> (Eq a) (Eq (List a))))
##   (def (= xs ys)
##     (case [xs ys]
##       [#;Nil #;Nil]
##       true

##       [(#;Cons x xs') (#;Cons y ys')]
##       (and (:: eq (E;= x y))
##            (= xs' ys'))

##       [_ _]
##       false
##       )))

(defstruct #export List/Monoid (All [a]
                                 (Monoid (List a)))
  (def unit #;Nil)
  (def (++ xs ys)
    (case xs
      #;Nil          ys
      (#;Cons x xs') (#;Cons x (++ xs' ys)))))

(defstruct #export List/Functor (Functor List)
  (def (map f ma)
    (case ma
      #;Nil          #;Nil
      (#;Cons a ma') (#;Cons (f a) (map f ma')))))

(defstruct #export List/Monad (Monad List)
  (def _functor List/Functor)

  (def (wrap a)
    (#;Cons a #;Nil))

  (def (join mma)
    (using List/Monoid
      (foldL ++ unit mma))))

## [Functions]
(def #export (sort ord xs)
  (All [a] (-> (O;Ord a) (List a) (List a)))
  (case xs
    #;Nil
    #;Nil
    
    (#;Cons x xs')
    (using ord
      (let [pre (filter (>= x) xs')
            post (filter (< x) xs')
            ++ (:: List/Monoid m;++)]
        ($ ++ (sort ord pre) (@list x) (sort ord post))))))