]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.runtime/scl/Iterator.scl
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.scl.runtime / scl / Iterator.scl
1 import "JavaBuiltin" as Java
2
3 importJava "java.util.Iterator" where
4     data T a
5     
6     hasNext :: T a -> <Proc> Boolean
7     next :: T a -> <Proc> a
8     remove :: T a -> <Proc> ()
9
10 @inline
11 iter :: (a -> <e> b) -> T a -> <Proc,e> ()
12 iter f it = loop ()
13   where
14     loop _ = 
15         if hasNext it
16         then do
17             f (next it)
18             loop ()
19         else ()
20
21 @inline
22 iterI :: (Integer -> a -> <e> b) -> T a -> <Proc,e> ()
23 iterI f it = loop 0
24   where
25     loop i = 
26         if hasNext it
27         then do
28             f i (next it)
29             loop (Java.iadd i 1)
30         else ()
31         
32 @inline
33 iterB :: (a -> <e> Boolean) -> T a -> <Proc,e> Boolean
34 iterB f it = loop ()
35   where
36     loop _ = 
37         if hasNext it 
38         then if f (next it)
39              then loop ()
40              else False
41         else True
42
43 @inline
44 filter :: (a -> <e> Boolean) -> T a -> <Proc,e> ()
45 filter f it = loop ()
46   where
47     loop _ = 
48         if hasNext it
49         then do
50             if f (next it)
51             then ()
52             else remove it
53             loop ()
54         else ()
55
56 @inline
57 mapFirst :: (a -> <e> Maybe b) -> T a -> <Proc,e> Maybe b
58 mapFirst f it = loop ()
59   where
60     loop _ = 
61         if hasNext it 
62         then match f (next it) with
63                  Nothing -> loop ()
64                  r -> r
65         else Nothing
66
67 @inline
68 fold :: (a -> b -> <e> a) -> a -> T b -> <Proc,e> a
69 fold f init it = loop init
70   where
71     loop cur =
72         if hasNext it
73         then loop (f cur (next it))
74         else cur
75
76 importJava "java.lang.Iterable" where
77     data Iterable a
78     
79     iterator :: Iterable a -> <Proc> T a