aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/collection/sequence.lux
blob: 2b046fee85218340306e8b71adb8724b1e766ea4 (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
(.module:
  [lux #*
   [abstract
    [functor (#+ Functor)]
    [comonad (#+ CoMonad)]]
   [control
    ["//" continuation (#+ Cont)]
    ["<>" parser
     ["<.>" code (#+ Parser)]]]
   [meta (#+ with-gensyms)]
   [macro
    [syntax (#+ syntax:)]
    ["." code]]
   [data
    ["." bit]
    [number
     ["n" nat]]
    [collection
     ["." list ("#@." monad)]]]])

(type: #export (Sequence a)
  {#.doc "An infinite sequence of values."}
  (Cont [a (Sequence a)]))

(def: #export (iterate f x)
  {#.doc "Create a sequence by applying a function to a value, and to its result, on and on..."}
  (All [a]
    (-> (-> a a) a (Sequence a)))
  (//.pending [x (iterate f (f x))]))

(def: #export (repeat x)
  {#.doc "Repeat a value forever."}
  (All [a]
    (-> a (Sequence a)))
  (//.pending [x (repeat x)]))

(def: #export (cycle [start next])
  {#.doc (doc "Go over the elements of a list forever."
              "The list should not be empty.")}
  (All [a]
    (-> [a (List a)] (Sequence a)))
  (loop [head start
         tail next]
    (//.pending [head (case tail
                        #.Nil
                        (recur start next)
                        
                        (#.Cons head' tail')
                        (recur head' tail'))])))

(template [<name> <return>]
  [(def: #export (<name> sequence)
     (All [a] (-> (Sequence a) <return>))
     (let [[head tail] (//.run sequence)]
       <name>))]

  [head a]
  [tail (Sequence a)]
  )

(def: #export (nth idx sequence)
  (All [a] (-> Nat (Sequence a) a))
  (let [[head tail] (//.run sequence)]
    (case idx
      0 head
      _ (nth (dec idx) tail))))

(template [<taker> <dropper> <splitter> <pred-type> <pred-test> <pred-step>]
  [(def: #export (<taker> pred xs)
     (All [a]
       (-> <pred-type> (Sequence a) (List a)))
     (let [[x xs'] (//.run xs)]
       (if <pred-test>
         (list& x (<taker> <pred-step> xs'))
         (list))))

   (def: #export (<dropper> pred xs)
     (All [a]
       (-> <pred-type> (Sequence a) (Sequence a)))
     (let [[x xs'] (//.run xs)]
       (if <pred-test>
         (<dropper> <pred-step> xs')
         xs)))

   (def: #export (<splitter> pred xs)
     (All [a]
       (-> <pred-type> (Sequence a) [(List a) (Sequence a)]))
     (let [[x xs'] (//.run xs)]
       (if <pred-test>
         (let [[tail next] (<splitter> <pred-step> xs')]
           [(#.Cons [x tail]) next])
         [(list) xs])))]
  
  [take-while drop-while split-while (-> a Bit) (pred x)     pred]
  [take       drop       split       Nat        (n.> 0 pred) (dec pred)]
  )

(def: #export (unfold step init)
  {#.doc "A stateful way of infinitely calculating the values of a sequence."}
  (All [a b]
    (-> (-> a [a b]) a (Sequence b)))
  (let [[next x] (step init)]
    (//.pending [x (unfold step next)])))

(def: #export (filter predicate sequence)
  (All [a] (-> (-> a Bit) (Sequence a) (Sequence a)))
  (let [[head tail] (//.run sequence)]
    (if (predicate head)
      (//.pending [head (filter predicate tail)])
      (filter predicate tail))))

(def: #export (partition left? xs)
  {#.doc (doc "Split a sequence in two based on a predicate."
              "The left side contains all entries for which the predicate is #1."
              "The right side contains all entries for which the predicate is #0.")}
  (All [a] (-> (-> a Bit) (Sequence a) [(Sequence a) (Sequence a)]))
  [(filter left? xs) (filter (bit.complement left?) xs)])

(structure: #export functor
  (Functor Sequence)
  
  (def: (map f fa)
    (let [[head tail] (//.run fa)]
      (//.pending [(f head) (map f tail)]))))

(structure: #export comonad
  (CoMonad Sequence)
  
  (def: &functor ..functor)
  
  (def: unwrap head)
  
  (def: (split wa)
    (let [[head tail] (//.run wa)]
      (//.pending [wa (split tail)]))))

(syntax: #export (^sequence& {patterns (<code>.form (<>.many <code>.any))}
                             body
                             {branches (<>.some <code>.any)})
  {#.doc (doc "Allows destructuring of sequences in pattern-matching expressions."
              "Caveat emptor: Only use it for destructuring, and not for testing values within the sequences."
              (let [(^sequence& x y z _tail) (some-sequence-func +1 +2 +3)]
                (func x y z)))}
  (with-gensyms [g!sequence]
    (let [body+ (` (let [(~+ (list@join (list@map (function (_ pattern)
                                                    (list (` [(~ pattern) (~ g!sequence)])
                                                          (` ((~! //.run) (~ g!sequence)))))
                                                  patterns)))]
                     (~ body)))]
      (wrap (list& g!sequence body+ branches)))))