aboutsummaryrefslogtreecommitdiff
path: root/source/lux/codata/stream.lux
blob: 1d6dd1b5024abb3e1b30b628ed4471cf678af4b7 (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
##   Copyright (c) Eduardo Julian. All rights reserved.
##   The use and distribution terms for this software are covered by the
##   Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
##   which can be found in the file epl-v10.html at the root of this distribution.
##   By using this software in any fashion, you are agreeing to be bound by
##   the terms of this license.
##   You must not remove this notice, or any other, from this software.

(;import lux
         (lux (control (lazy #as L #refer #all)
                       (functor #as F #refer #all)
                       (monad #as M #refer #all)
                       (comonad #as CM #refer #all))
              (meta lux
                    macro
                    syntax)
              (data (list #as l #refer (#only list list& List/Monad)))))

## [Types]
(deftype #export (Stream a)
  (Lazy (, a (Stream a))))

## [Utils]
(def (cycle' x xs init full)
  (All [a]
    (-> a (List a) a (List a) (Stream a)))
  (case xs
    #;Nil            (cycle' init full init full)
    (#;Cons [y xs']) (... [x (cycle' y xs' init full)])))

## [Functions]
(def #export (iterate f x)
  (All [a]
    (-> (-> a a) a (Stream a)))
  (... [x (iterate f (f x))]))

(def #export (repeat x)
  (All [a]
    (-> a (Stream a)))
  (... [x (repeat x)]))

(def #export (cycle xs)
  (All [a]
    (-> (List a) (Maybe (Stream a))))
  (case xs
    #;Nil            #;None
    (#;Cons [x xs']) (#;Some (cycle' x xs' x xs'))))

(do-template [<name> <return> <part>]
  [(def #export (<name> s)
     (All [a] (-> (Stream a) <return>))
     (let [[h t] (! s)]
       <part>))]

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

(def #export (@ idx s)
  (All [a] (-> Int (Stream a) a))
  (let [[h t] (! s)]
    (if (i> idx 0)
      (@ (dec idx) t)
      h)))

(do-template [<taker> <dropper> <splitter> <det-type> <det-test> <det-step>]
  [(def #export (<taker> det xs)
     (All [a]
       (-> <det-type> (Stream a) (List a)))
     (let [[x xs'] (! xs)]
       (if <det-test>
         (list& x (<taker> <det-step> xs'))
         (list))))

   (def #export (<dropper> det xs)
     (All [a]
       (-> <det-type> (Stream a) (Stream a)))
     (let [[x xs'] (! xs)]
       (if <det-test>
         (<dropper> <det-step> xs')
         xs)))

   (def #export (<splitter> det xs)
     (All [a]
       (-> <det-type> (Stream a) (, (List a) (Stream a))))
     (let [[x xs'] (! xs)]
       (if <det-test>
         (let [[tail next] (<splitter> <det-step> xs')]
           [(#;Cons [x tail]) next])
         [(list) xs])))]
  
  [take-while drop-while split-with (-> a Bool) (det x)    det]
  [take       drop       split      Int         (i> det 0) (dec det)]
  )

(def #export (unfold step init)
  (All [a b]
    (-> (-> a (, a b)) a (Stream b)))
  (let [[next x] (step init)]
    (... [x (unfold step next)])))

(def #export (filter p xs)
  (All [a] (-> (-> a Bool) (Stream a) (Stream a)))
  (let [[x xs'] (! xs)]
    (if (p x)
      (... [x (filter p xs')])
      (filter p xs'))))

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

## [Structures]
(defstruct #export Stream/Functor (Functor Stream)
  (def (F;map f fa)
    (let [[h t] (! fa)]
      (... [(f h) (F;map f t)]))))

(defstruct #export Stream/CoMonad (CoMonad Stream)
  (def CM;_functor Stream/Functor)
  (def CM;unwrap head)
  (def (CM;split wa)
    (:: Stream/Functor (F;map repeat wa))))

## [Pattern-matching]
(defsyntax #export (\stream body [patterns' (+^ id^)])
  (do Lux/Monad
    [patterns (map% Lux/Monad macro-expand-1 patterns')
     g!s (gensym "s")
     #let [patterns+ (: (List Syntax)
                        (do List/Monad
                          [pattern (l;reverse patterns)]
                          (list (` [(~ pattern) (~ g!s)]) (` (L;! (~ g!s))))))]]
    (M;wrap (list g!s (` (;let [(~@ patterns+)] (~ body)))))))