]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.runtime/scl/IterN.scl
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.scl.runtime / scl / IterN.scl
1 include "Prelude"
2
3 @inline
4 iterN :: (Integer -> <e> b) -> Integer -> <e> ()
5 iterN f n = loop 0
6   where
7     loop i = if i==n
8              then ()
9              else do { f i ; loop (i+1) }
10
11 @inline
12 anyN :: (Integer -> <e> Boolean) -> Integer -> <e> Boolean
13 anyN f n = loop 0
14   where
15     loop i = if i==n
16              then False
17              else f i || loop (i+1)
18
19 @inline
20 allN :: (Integer -> <e> Boolean) -> Integer -> <e> Boolean
21 allN f n = loop 0
22   where
23     loop i = if i==n
24              then True
25              else f i && loop (i+1)
26
27 @inline
28 mapFirstN :: (Integer -> <e> Maybe b) -> Integer -> <e> Maybe b
29 mapFirstN f n = loop 0
30   where
31     loop i = if i==n
32              then Nothing
33              else match f i with
34                  r @ (Just _) -> r
35                  Nothing -> loop (i+1)
36
37 @inline
38 concatMapN :: (Integer -> <e> [b]) -> Integer -> <e> [b]
39 concatMapN f n = sum (mapN f n)
40
41 @inline
42 mapN :: (Integer -> <e> b) -> Integer -> <e> [b]
43 mapN f n = build (\empty cons -> 
44     let loop i accum = if i==n
45                        then accum
46                        else loop (i+1) (cons accum (f i)) 
47     in  loop 0 empty)
48
49 @inline
50 filterN :: (Integer -> <e> Boolean) -> Integer -> <e> [Integer]
51 filterN f n = build (\empty cons -> 
52     let loop i accum = if i==n
53                        then accum
54                        else loop (i+1) (
55                            if f i 
56                            then cons accum i
57                            else accum) 
58     in  loop 0 empty)
59
60 @inline
61 mapMaybeN :: (Integer -> <e> Maybe b) -> Integer -> <e> [b]
62 mapMaybeN f n = build (\empty cons -> 
63     let loop i accum = if i==n
64                        then accum
65                        else loop (i+1) (match f i with
66                            Just v -> cons accum v
67                            Nothing -> accum) 
68     in  loop 0 empty)
69
70 @inline
71 foldlN :: (a -> Integer -> <e> a) -> a -> Integer -> <e> a
72 foldlN f initial n = loop initial 0
73   where
74     loop cur i = if i==n
75                  then cur
76                  else loop (f cur i) (i+1)