-
Notifications
You must be signed in to change notification settings - Fork 19
/
First.purs
58 lines (38 loc) · 1.61 KB
/
First.purs
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
module Data.Maybe.First where
import Prelude
import Control.Extend (class Extend)
import Data.Eq (class Eq1)
import Data.Functor.Invariant (class Invariant)
import Data.Maybe (Maybe(..))
import Data.Monoid (class Monoid)
import Data.Newtype (class Newtype)
import Data.Ord (class Ord1)
-- | Monoid returning the first (left-most) non-`Nothing` value.
-- |
-- | ``` purescript
-- | First (Just x) <> First (Just y) == First (Just x)
-- | First Nothing <> First (Just y) == First (Just y)
-- | First Nothing <> Nothing == First Nothing
-- | mempty :: First _ == First Nothing
-- | ```
newtype First a = First (Maybe a)
derive instance newtypeFirst :: Newtype (First a) _
derive newtype instance eqFirst :: (Eq a) => Eq (First a)
derive newtype instance eq1First :: Eq1 First
derive newtype instance ordFirst :: (Ord a) => Ord (First a)
derive newtype instance ord1First :: Ord1 First
derive newtype instance boundedFirst :: (Bounded a) => Bounded (First a)
derive newtype instance functorFirst :: Functor First
derive newtype instance invariantFirst :: Invariant First
derive newtype instance applyFirst :: Apply First
derive newtype instance applicativeFirst :: Applicative First
derive newtype instance bindFirst :: Bind First
derive newtype instance monadFirst :: Monad First
derive newtype instance extendFirst :: Extend First
instance showFirst :: (Show a) => Show (First a) where
show (First a) = "First (" <> show a <> ")"
instance semigroupFirst :: Semigroup (First a) where
append first@(First (Just _)) _ = first
append _ second = second
instance monoidFirst :: Monoid (First a) where
mempty = First Nothing