blob: a25a19b5f4025af6a38a98fa735eebec68f03979 (
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
|
## 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 (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))
(number (int #open ("i" Int/Number Int/Ord)))
bool)
(codata (lazy #as L #refer #all))))
## [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 x' xs') (... [x (cycle' x' 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)
(@ (i+ -1 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) (i+ -1 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 (map f fa)
(let [[h t] (! fa)]
(... [(f h) (map f t)]))))
(defstruct #export Stream/CoMonad (CoMonad Stream)
(def _functor Stream/Functor)
(def unwrap head)
(def (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 AST)
(do List/Monad
[pattern (l;reverse patterns)]
(: (List AST) (@list (` [(~ pattern) (~ g!s)]) (` (L;! (~ g!s)))))))]]
(wrap (@list g!s (` (;let [(~@ patterns+)] (~ body)))))))
|