Skip to content

Commit

Permalink
Merge pull request #15 from purescript/lazy-folds
Browse files Browse the repository at this point in the history
Add "lazy" version of `maybe`
  • Loading branch information
garyb committed Jul 31, 2015
2 parents 7d20791 + e21d347 commit a0ff5eb
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
32 changes: 32 additions & 0 deletions docs/Data/Maybe.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ maybe x f Nothing == x
maybe x f (Just y) == f y
```

#### `maybe'`

``` purescript
maybe' :: forall a b. (Unit -> b) -> (a -> b) -> Maybe a -> b
```

Similar to `maybe` but for use in cases where the default value may be
expensive to compute. As PureScript is not lazy, the standard `maybe` has
to evaluate the default value before returning the result, whereas here
the value is only computed when the `Maybe` is known to be `Nothing`.

``` purescript
maybe' (\_ -> x) f Nothing == x
maybe' (\_ -> x) f (Just y) == f y
```

#### `fromMaybe`

``` purescript
Expand All @@ -70,6 +86,22 @@ fromMaybe x Nothing == x
fromMaybe x (Just y) == y
```

#### `fromMaybe'`

``` purescript
fromMaybe' :: forall a. (Unit -> a) -> Maybe a -> a
```

Similar to `fromMaybe` but for use in cases where the default value may be
expensive to compute. As PureScript is not lazy, the standard `fromMaybe`
has to evaluate the default value before returning the result, whereas here
the value is only computed when the `Maybe` is known to be `Nothing`.

``` purescript
fromMaybe' (\_ -> x) Nothing == x
fromMaybe' (\_ -> x) (Just y) == y
```

#### `isJust`

``` purescript
Expand Down
25 changes: 25 additions & 0 deletions src/Data/Maybe.purs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ maybe :: forall a b. b -> (a -> b) -> Maybe a -> b
maybe b _ Nothing = b
maybe _ f (Just a) = f a

-- | Similar to `maybe` but for use in cases where the default value may be
-- | expensive to compute. As PureScript is not lazy, the standard `maybe` has
-- | to evaluate the default value before returning the result, whereas here
-- | the value is only computed when the `Maybe` is known to be `Nothing`.
-- |
-- | ``` purescript
-- | maybe' (\_ -> x) f Nothing == x
-- | maybe' (\_ -> x) f (Just y) == f y
-- | ```
maybe' :: forall a b. (Unit -> b) -> (a -> b) -> Maybe a -> b
maybe' g _ Nothing = g unit
maybe' _ f (Just a) = f a

-- | Takes a default value, and a `Maybe` value. If the `Maybe` value is
-- | `Nothing` the default value is returned, otherwise the value inside the
-- | `Just` is returned.
Expand All @@ -38,6 +51,18 @@ maybe _ f (Just a) = f a
fromMaybe :: forall a. a -> Maybe a -> a
fromMaybe a = maybe a (id :: forall a. a -> a)

-- | Similar to `fromMaybe` but for use in cases where the default value may be
-- | expensive to compute. As PureScript is not lazy, the standard `fromMaybe`
-- | has to evaluate the default value before returning the result, whereas here
-- | the value is only computed when the `Maybe` is known to be `Nothing`.
-- |
-- | ``` purescript
-- | fromMaybe' (\_ -> x) Nothing == x
-- | fromMaybe' (\_ -> x) (Just y) == y
-- | ```
fromMaybe' :: forall a. (Unit -> a) -> Maybe a -> a
fromMaybe' a = maybe' a (id :: forall a. a -> a)

-- | Returns `true` when the `Maybe` value was constructed with `Just`.
isJust :: forall a. Maybe a -> Boolean
isJust = maybe false (const true)
Expand Down

0 comments on commit a0ff5eb

Please sign in to comment.