aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/collection/stream.lux
blob: d62f1f23c07d0d14f3dc6e3e52e8c13653b7f20e (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
(.using
 [library
  [lux (.except pattern only)
   [abstract
    [functor (.only Functor)]
    [comonad (.only CoMonad)]]
   [control
    ["//" continuation (.only Cont)]
    ["<>" parser (.only)
     ["<[0]>" code (.only Parser)]]]
   [data
    ["[0]" bit]
    [collection
     ["[0]" list ("[1]#[0]" monad)]]]
   [macro (.only with_symbols)
    [syntax (.only syntax:)]
    ["[0]" code]]
   [math
    [number
     ["n" nat]]]]])

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

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

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

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

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

  [head a]
  [tail (Stream a)]
  )

(def: .public (item idx stream)
  (All (_ a) (-> Nat (Stream a) a))
  (let [[head tail] (//.result stream)]
    (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> (Stream a) (List a)))
     (let [[x xs'] (//.result xs)]
       (if (<post_test> <pred_test>)
         (partial_list x (<taker> <pred_step> xs'))
         (list))))

   (def: .public (<dropper> pred xs)
     (All (_ a)
       (-> <pred_type> (Stream a) (Stream 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> (Stream a) [(List a) (Stream 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 stream)
  (All (_ a) (-> (-> a Bit) (Stream a) (Stream a)))
  (let [[head tail] (//.result stream)]
    (if (predicate head)
      (//.pending [head (only predicate tail)])
      (only predicate tail))))

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

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

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

(syntax: .public (pattern [patterns (<code>.form (<>.many <code>.any))
                           body <code>.any
                           branches (<>.some <code>.any)])
  (with_symbols [g!stream]
    (let [body+ (` (let [(~+ (|> patterns
                                 (list#each (function (_ pattern)
                                              (list (` [(~ pattern) (~ g!stream)])
                                                    (` ((~! //.result) (~ g!stream))))))
                                 list#conjoint))]
                     (~ body)))]
      (in (partial_list g!stream body+ branches)))))