blob: 1bfd192926a7e6496fb6f5a5b3fd10328dd4ffaf (
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
|
## 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))))
## Types
(deftype #export (Stream a)
(Lazy (, a (Stream a))))
## Functions
(def #export (iterate f x)
(All [a]
(-> (-> a a) a (Stream a)))
(... [x (iterate f (f x))]))
## (def #export (take n xs)
## (All [a]
## (-> Int (Stream a) (List a)))
## (if (int:> n 0)
## (let [[x xs'] (! xs)]
## (list& x (take (dec n) xs')))
## (list)))
## (def #export (drop n xs)
## (All [a]
## (-> Int (Stream a) (Stream a)))
## (if (int:> n 0)
## (drop (dec n) (get@ 1 (! xs)))
## xs))
## Pattern-matching
## (defmacro #export (\stream tokens)
## (case tokens
## (\ (list& body patterns'))
## (do Lux:Monad
## [patterns (map% Lux:Monad M;macro-expand-1 patterns')
## g!s (M;gensym "s")
## #let [patterns+ (do List:Monad
## [pattern (reverse patterns)]
## (list (` [(~ pattern) (~ g!s)]) (` (L;! (~ g!s)))))]]
## (wrap (list g!s
## (` (;let [(~@ patterns+)]
## (~ body))))))
## _
## "Wrong syntax for \stream"))
## (defsyntax #export (\stream body [patterns' (+$ id$)])
## (do Lux:Monad
## [patterns (map% Lux:Monad M;macro-expand-1 patterns')
## g!s (M;gensym "s")
## #let [patterns+ (do List:Monad
## [pattern (reverse patterns)]
## (list (` [(~ pattern) (~ g!s)]) (` (L;! (~ g!s)))))]]
## (wrap (list g!s
## (` (;let [(~@ patterns+)]
## (~ body)))))))
|