aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/collection/sequence.lux
blob: 35b9a1102722d0264fe60ce79d3a7dc27196affe (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
(.module:
  [library
   [lux "*"
    [abstract
     [functor {"+" [Functor]}]
     [comonad {"+" [CoMonad]}]]
    [control
     ["//" continuation {"+" [Cont]}]
     ["<>" parser
      ["<.>" code {"+" [Parser]}]]]
    [macro {"+" [with_identifiers]}
     [syntax {"+" [syntax:]}]
     ["." code]]
    [data
     ["." bit]
     [collection
      ["." list ("#\." monad)]]]
    [math
     [number
      ["n" nat]]]]])

(type: .public (Sequence a)
  (Cont [a (Sequence a)]))

(def: .public (iterations step init)
  (All (_ a b)
    (-> (-> a [a b]) a (Sequence b)))
  (let [[next x] (step init)]
    (//.pending [x (iterations step next)])))

(def: .public (repeated x)
  (All (_ a)
    (-> a (Sequence a)))
  (//.pending [x (repeated x)]))

(def: .public (cycle [start next])
  (All (_ a)
    (-> [a (List a)] (Sequence a)))
  (loop [head start
         tail next]
    (//.pending [head (case tail
                        #.End
                        (recur start next)
                        
                        (#.Item head' tail')
                        (recur head' tail'))])))

(template [<name> <return>]
  [(def: .public (<name> sequence)
     (All (_ a) (-> (Sequence a) <return>))
     (let [[head tail] (//.result sequence)]
       <name>))]

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

(def: .public (item idx sequence)
  (All (_ a) (-> Nat (Sequence a) a))
  (let [[head tail] (//.result sequence)]
    (case idx
      0 head
      _ (item (-- idx) tail))))

(template [<taker> <dropper> <pred_type> <pred_test> <pred_step> <post_test>]
  [(def: .public (<taker> pred xs)
     (All (_ a)
       (-> <pred_type> (Sequence a) (List a)))
     (let [[x xs'] (//.result xs)]
       (if (<post_test> <pred_test>)
         (list& x (<taker> <pred_step> xs'))
         (list))))

   (def: .public (<dropper> pred xs)
     (All (_ a)
       (-> <pred_type> (Sequence a) (Sequence a)))
     (let [[x xs'] (//.result xs)]
       (if (<post_test> <pred_test>)
         (<dropper> <pred_step> xs')
         xs)))]
  
  [while until (-> a Bit) (pred x)     pred       |>]
  [first after Nat        (n.= 0 pred) (-- pred) not]
  )

(template [<splitter> <pred_type> <pred_test> <pred_step>]
  [(def: .public (<splitter> pred xs)
     (All (_ a)
       (-> <pred_type> (Sequence a) [(List a) (Sequence a)]))
     (let [[x xs'] (//.result xs)]
       (if <pred_test>
         [(list) xs]
         (let [[tail next] (<splitter> <pred_step> xs')]
           [(#.Item [x tail]) next]))))]
  
  [split_when (-> a Bit) (pred x)     pred]
  [split_at   Nat        (n.= 0 pred) (-- pred)]
  )

(def: .public (only predicate sequence)
  (All (_ a) (-> (-> a Bit) (Sequence a) (Sequence a)))
  (let [[head tail] (//.result sequence)]
    (if (predicate head)
      (//.pending [head (only predicate tail)])
      (only predicate tail))))

(def: .public (partition left? xs)
  (All (_ a) (-> (-> a Bit) (Sequence a) [(Sequence a) (Sequence a)]))
  [(..only left? xs)
   (..only (bit.complement left?) xs)])

(implementation: .public functor
  (Functor Sequence)
  
  (def: (each f fa)
    (let [[head tail] (//.result fa)]
      (//.pending [(f head) (each f tail)]))))

(implementation: .public comonad
  (CoMonad Sequence)
  
  (def: &functor ..functor)
  
  (def: out head)
  
  (def: (disjoint wa)
    (let [[head tail] (//.result wa)]
      (//.pending [wa (disjoint tail)]))))

(syntax: .public (^sequence& [patterns (<code>.form (<>.many <code>.any))
                              body <code>.any
                              branches (<>.some <code>.any)])
  (with_identifiers [g!sequence]
    (let [body+ (` (let [(~+ (|> patterns
                                 (list\each (function (_ pattern)
                                              (list (` [(~ pattern) (~ g!sequence)])
                                                    (` ((~! //.result) (~ g!sequence))))))
                                 list\conjoint))]
                     (~ body)))]
      (in (list& g!sequence body+ branches)))))