Skip to content

Commit b20386e

Browse files
authored
Add Discard class (#115)
* Add Discard class * Use bind * Remove unused import
1 parent 09effab commit b20386e

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/Control/Bind.purs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module Control.Bind
22
( class Bind, bind, (>>=)
33
, bindFlipped, (=<<)
4+
, class Discard, discard
45
, join
56
, composeKleisli, (>=>)
67
, composeKleisliFlipped, (<=<)
@@ -16,6 +17,7 @@ import Control.Category (id)
1617

1718
import Data.Function (flip)
1819
import Data.Functor (class Functor, map, void, ($>), (<#>), (<$), (<$>))
20+
import Data.Unit (Unit)
1921

2022
-- | The `Bind` type class extends the [`Apply`](#apply) type class with a
2123
-- | "bind" operation `(>>=)` which composes computations in sequence, using
@@ -66,6 +68,17 @@ instance bindArray :: Bind Array where
6668

6769
foreign import arrayBind :: forall a b. Array a -> (a -> Array b) -> Array b
6870

71+
-- | A class for types whose values can safely be discarded
72+
-- | in a `do` notation block.
73+
-- |
74+
-- | An example is the `Unit` type, since there is only one
75+
-- | possible value which can be returned.
76+
class Discard a where
77+
discard :: forall f b. Bind f => f a -> (a -> f b) -> f b
78+
79+
instance discardUnit :: Discard Unit where
80+
discard = bind
81+
6982
-- | Collapse two applications of a monadic type constructor into one.
7083
join :: forall a m. Bind m => m (m a) -> m a
7184
join m = m >>= id

src/Prelude.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module Prelude
2828

2929
import Control.Applicative (class Applicative, pure, liftA1, unless, when)
3030
import Control.Apply (class Apply, apply, (*>), (<*), (<*>))
31-
import Control.Bind (class Bind, bind, ifM, join, (<=<), (=<<), (>=>), (>>=))
31+
import Control.Bind (class Bind, bind, class Discard, discard, ifM, join, (<=<), (=<<), (>=>), (>>=))
3232
import Control.Category (class Category, id)
3333
import Control.Monad (class Monad, ap, liftM1, unlessM, whenM)
3434
import Control.Semigroupoid (class Semigroupoid, compose, (<<<), (>>>))

0 commit comments

Comments
 (0)