1 import "JavaBuiltin" as Java
3 data Either a b = Left a | Right b
5 data Stream a = Stream (s -> Step a s) s
6 data Step a s = Done | Yield a s | Skip s
8 // stream :: [a] -> Stream a
9 // unstream :: Stream a -> [a]
10 // streamRev :: [a] -> Stream a
11 // unstreamRev :: Stream a -> [a]
13 //toStream :: [a] -> Stream a
14 //toStream l = Stream (\i -> if i >= length l then Done else Yield (get l i) (i+1)) 0
16 filterS :: (a -> Boolean) -> Stream a -> Stream a
17 filterS p (Stream next0 s0) = Stream next s0
19 next s = match next s with
22 Yield a s -> if p a then Yield a s else Skip s
24 mapS :: (a -> b) -> Stream a -> Stream b
25 mapS f (Stream next0 s0) = Stream next s0
27 next s = match next0 s with
30 Yield a s -> Yield (f a) s
32 appendS :: Stream a -> Stream a -> Stream a
33 appendS (Stream next1 s1) (Stream next2 s2) = Stream next (Left s1)
35 next (Left s) = match next1 s with
36 Done -> Skip (Right s2)
37 Skip s -> Skip (Left s)
38 Yield a s -> Yield a (Left s)
39 next (Right s) = match next2 s with
41 Skip s -> Skip (Right s)
42 Yield a s -> Yield a (Right s)
44 decomposeS :: Stream a -> Maybe (a, Stream a)
45 decomposeS (Stream next s0) = loop s0
47 loop s = match next s with
50 Yield a s -> Just (a, Stream next s)
52 returnS :: a -> Stream a
53 returnS x = Stream next True
55 next True = Yield x False
58 isEmptyS :: Stream a -> Boolean
59 isEmptyS (Stream next s0) = loop s0
61 loop s = match next s with
66 foldlS :: (a -> b -> a) -> a -> Stream b -> a
67 foldlS f init (Stream next s0) = go init s0
69 go cur s = match next s with
72 Yield x s -> go (f cur x) s
74 scanlS :: (a -> b -> a) -> a -> Stream b -> Stream a
75 scanlS f init (Stream next0 s0) = Stream next (s0, init)
77 next (s,v) = match next0 s with
80 Yield x s -> Yield v (s, f v x)
82 concatMapS :: (a -> Stream b) -> Stream a -> Stream b
83 concatMapS f (Stream next0 s0) = Stream next (s0, Nothing)
85 next (s, Nothing) = match next0 s with
87 Skip s -> Skip (s, Nothing)
88 Yield x s -> Skip (s, Just (f x))
89 next (s0, Just (Stream next1 s1)) = match next1 s1 with
90 Done -> Skip (s0, Nothing)
91 Skip s -> Skip (s0, Just (Stream next1 s))
92 Yield x s -> Yield x (s0, Just (Stream next1 s))
94 zipWithS :: (a -> b -> c) -> Stream a -> Stream b -> Stream c
95 zipWithS f (Stream next0 s0) (Stream next1 s1) = Stream next (s0, s1, Nothing)
97 next (s0, s1, Nothing) = match next0 s0 with
99 Skip s -> Skip (s, s1, Nothing)
100 Yield x s -> Skip (s, s1, Just x)
101 next (s0, s1, Just x) = match next1 s1 with
103 Skip s -> Skip (s0, s, Just x)
104 Yield y s -> Yield (f x y) (s0, s, Nothing)
106 guardS :: Boolean -> Stream a -> Stream a
107 guardS b (Stream next0 s0) = Stream next (b, s0)
109 next (False, _) = Done
110 next (True, s) = match next0 s with
112 Skip s -> Skip (True, s)
113 Yield x s -> Yield x (True, s)
115 takeS :: Integer -> Stream a -> Stream a
116 takeS count (Stream next0 s0) = Stream next (count, s0)
118 next (count, s) = if count <= 0 then Done
119 else match next0 s with
121 | Skip s -> Skip (count, s)
122 | Yield x s -> Yield x (count, s)
124 repeatS :: a -> Stream a
125 repeatS v = Stream next ()
129 iterateS :: (a -> a) -> a -> Stream a
130 iterateS f v = Stream next v
132 next v = Yield v (f v)
135 main = foldlS Java.iadd (0 :: Integer)
136 (appendS (appendS (returnS (1 :: Integer))
137 (returnS (2 :: Integer))) (returnS (3 :: Integer)))