forked from purescript/purescript-effect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEffect.purs
More file actions
76 lines (59 loc) · 2.54 KB
/
Copy pathEffect.purs
File metadata and controls
76 lines (59 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
-- | This module provides the `Effect` type, which is used to represent
-- | _native_ effects. The `Effect` type provides a typed API for effectful
-- | computations, while at the same time generating efficient JavaScript.
module Effect
( Effect
, untilE
, whileE
, forE
, foreachE
) where
import Prelude
import Control.Apply (lift2)
-- | A native effect. The type parameter denotes the return type of running the
-- | effect, that is, an `Effect Int` is a possibly-effectful computation which
-- | eventually produces a value of the type `Int` when it finishes.
foreign import data Effect :: Type -> Type
type role Effect representational
instance functorEffect :: Functor Effect where
map = liftA1
instance applyEffect :: Apply Effect where
apply = ap
instance applicativeEffect :: Applicative Effect where
pure = pureE
foreign import pureE :: forall a. a -> Effect a
instance bindEffect :: Bind Effect where
bind = bindE
foreign import bindE :: forall a b. Effect a -> (a -> Effect b) -> Effect b
instance monadEffect :: Monad Effect
-- | The `Semigroup` instance for effects allows you to run two effects, one
-- | after the other, and then combine their results using the result type's
-- | `Semigroup` instance.
instance semigroupEffect :: Semigroup a => Semigroup (Effect a) where
append = lift2 append
-- | If you have a `Monoid a` instance, then `mempty :: Effect a` is defined as
-- | `pure mempty`.
instance monoidEffect :: Monoid a => Monoid (Effect a) where
mempty = pureE mempty
-- | Loop until a condition becomes `true`.
-- |
-- | `untilE b` is an effectful computation which repeatedly runs the effectful
-- | computation `b`, until its return value is `true`.
foreign import untilE :: Effect Boolean -> Effect Unit
-- | Loop while a condition is `true`.
-- |
-- | `whileE b m` is effectful computation which runs the effectful computation
-- | `b`. If its result is `true`, it runs the effectful computation `m` and
-- | loops. If not, the computation ends.
foreign import whileE :: forall a. Effect Boolean -> Effect a -> Effect Unit
-- | Loop over a consecutive collection of numbers.
-- |
-- | `forE lo hi f` runs the computation returned by the function `f` for each
-- | of the inputs between `lo` (inclusive) and `hi` (exclusive).
foreign import forE :: Int -> Int -> (Int -> Effect Unit) -> Effect Unit
-- | Loop over an array of values.
-- |
-- | `foreachE xs f` runs the computation returned by the function `f` for each
-- | of the inputs `xs`.
foreign import foreachE
:: forall a. Array a -> (a -> Effect Unit) -> Effect Unit