]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.runtime/scl/Iterator.scl
be7fb90e06e7f7517486275bdfc7614b79a3551c
[simantics/platform.git] / bundles / org.simantics.scl.runtime / scl / Iterator.scl
1 importJava "java.util.Iterator" where
2     data T a
3     
4     hasNext :: T a -> <Proc> Boolean
5     next :: T a -> <Proc> a
6     remove :: T a -> <Proc> ()
7
8 @inline
9 iter :: (a -> <e> b) -> T a -> <Proc,e> ()
10 iter f it = loop ()
11   where
12     loop _ = 
13         if hasNext it
14         then do
15             f (next it)
16             loop ()
17         else ()
18
19 @inline
20 iterB :: (a -> <e> Boolean) -> T a -> <Proc,e> Boolean
21 iterB f it = loop ()
22   where
23     loop _ = 
24         if hasNext it 
25         then if f (next it)
26              then loop ()
27              else False
28         else True
29
30 @inline
31 mapFirst :: (a -> <e> Maybe b) -> T a -> <Proc,e> Maybe b
32 mapFirst f it = loop ()
33   where
34     loop _ = 
35         if hasNext it 
36         then match f (next it) with
37                  Nothing -> loop ()
38                  r -> r
39         else Nothing
40
41 @inline
42 fold :: (a -> b -> <e> a) -> a -> T b -> <Proc,e> a
43 fold f init it = loop init
44   where
45     loop cur =
46         if hasNext it
47         then loop (f cur (next it))
48         else cur
49
50 importJava "java.lang.Iterable" where
51     data Iterable a
52     
53     iterator :: Iterable a -> <Proc> T a