]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.runtime/scl/Iterator.scl
(refs #7259) Added Iterator.filter and MSet.filterInPlace
[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 filter :: (a -> <e> Boolean) -> T a -> <Proc,e> ()
32 filter f it = loop ()
33   where
34     loop _ = 
35         if hasNext it
36         then do
37             if f (next it)
38             then ()
39             else remove it
40             loop ()
41         else ()
42
43 @inline
44 mapFirst :: (a -> <e> Maybe b) -> T a -> <Proc,e> Maybe b
45 mapFirst f it = loop ()
46   where
47     loop _ = 
48         if hasNext it 
49         then match f (next it) with
50                  Nothing -> loop ()
51                  r -> r
52         else Nothing
53
54 @inline
55 fold :: (a -> b -> <e> a) -> a -> T b -> <Proc,e> a
56 fold f init it = loop init
57   where
58     loop cur =
59         if hasNext it
60         then loop (f cur (next it))
61         else cur
62
63 importJava "java.lang.Iterable" where
64     data Iterable a
65     
66     iterator :: Iterable a -> <Proc> T a