Skip to content

Commit 24878df

Browse files
committed
ports more looping constructs from Eff
1 parent 127e168 commit 24878df

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

src/Control/Monad/ST.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Control.Monad.ST (module Internal) where
22

3-
import Control.Monad.ST.Internal (ST, kind Region, run, while) as Internal
3+
import Control.Monad.ST.Internal (ST, kind Region, run, while, for, foreach) as Internal

src/Control/Monad/ST/Internal.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,28 @@ exports["while"] = function (f) {
3636
};
3737
};
3838

39+
exports["for"] = function (lo) {
40+
return function (hi) {
41+
return function (f) {
42+
return function () {
43+
for (var i = lo; i < hi; i++) {
44+
f(i)();
45+
}
46+
};
47+
};
48+
};
49+
};
50+
51+
exports.foreach = function (as) {
52+
return function (f) {
53+
return function () {
54+
for (var i = 0, l = as.length; i < l; i++) {
55+
f(as[i])();
56+
}
57+
};
58+
};
59+
};
60+
3961
exports.new = function (val) {
4062
return function () {
4163
return { value: val };

src/Control/Monad/ST/Internal.purs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ foreign import run :: forall a. (forall r. ST r a) -> a
7373
-- | computation ends.
7474
foreign import while :: forall r a. ST r Boolean -> ST r a -> ST r Unit
7575

76+
-- | Loop over a consecutive collection of numbers
77+
-- |
78+
-- | `ST.for lo hi f` runs the computation returned by the function `f` for each
79+
-- | of the inputs between `lo` (inclusive) and `hi` (exclusive).
80+
foreign import for :: forall r a. Int -> Int -> (Int -> ST r a) -> ST r Unit
81+
82+
-- | Loop over an array of values.
83+
-- |
84+
-- | `ST.foreach xs f` runs the computation returned by the function `f` for each
85+
-- | of the inputs `xs`.
86+
foreign import foreach :: forall r a. Array a -> (a -> ST r Unit) -> ST r Unit
87+
7688
-- | The type `STRef r a` represents a mutable reference holding a value of
7789
-- | type `a`, which can be used with the `ST r` effect.
7890
foreign import data STRef :: Region -> Type -> Type

0 commit comments

Comments
 (0)