]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.runtime/scl/Java/Iterator.scl
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.runtime / scl / Java / Iterator.scl
1
2 importJava "java.util.Iterator" where
3     data Iterator a
4     
5     hasNext :: Iterator a -> <Proc> Boolean
6     next :: Iterator a -> <Proc> a
7
8 @inline
9 iter :: (a -> <e> dummy) -> Iterator 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 find :: (a -> <e> Boolean) -> Iterator a -> <Proc,e> Maybe a
21 find f it = loop ()
22   where
23     loop _ = 
24         if hasNext it 
25         then let el = next it
26              in if f el
27                 then Just el
28                 else loop ()
29         else Nothing
30
31 @inline
32 any :: (a -> <e> Boolean) -> Iterator a -> <Proc,e> Boolean
33 any f it = loop ()
34   where
35     loop _ = 
36         if hasNext it 
37         then if f (next it)
38              then True
39              else loop ()
40         else False
41
42 @inline
43 all :: (a -> <e> Boolean) -> Iterator a -> <Proc,e> Boolean
44 all f it = loop ()
45   where
46     loop _ = 
47         if hasNext it 
48         then if f (next it)
49              then loop ()
50              else False
51         else True
52
53 @inline
54 mapFirst :: (a -> <e> Maybe b) -> Iterator a -> <Proc,e> Maybe b
55 mapFirst f it = loop ()
56   where
57     loop _ = 
58         if hasNext it 
59         then match f (next it) with
60                  Nothing -> loop ()
61                  r -> r
62         else Nothing
63
64 @inline
65 foldl :: (a -> b -> <e> a) -> a -> Iterator b -> <Proc,e> a
66 foldl f init it = loop init
67   where
68     loop cur =
69         if hasNext it
70         then loop (f cur (next it))
71         else cur
72
73 @inline
74 foldl1 :: (a -> a -> <e> a) -> Iterator a -> <Proc,e> a
75 foldl1 f it = foldl f (next it) it