3
3
-- | with multiple alternatives.
4
4
module Data.Validation.Semiring
5
5
( V (..)
6
+ , validation
6
7
, unV
7
8
, invalid
8
9
, isValid
@@ -22,6 +23,7 @@ import Data.Foldable (class Foldable)
22
23
import Data.Ord (class Ord1 )
23
24
import Data.Traversable (class Traversable )
24
25
import Data.Newtype (class Newtype )
26
+ import Prim.TypeError (class Warn , Text )
25
27
26
28
-- | The `V` functor, used for alternative validation
27
29
-- |
@@ -43,11 +45,16 @@ newtype V err result = V (Either err result)
43
45
44
46
derive instance newtypeV :: Newtype (V err result ) _
45
47
46
- -- | Unpack the `V` type constructor, providing functions to handle the error
47
- -- | and success cases.
48
- unV :: forall err result r . (err -> r ) -> (result -> r ) -> V err result -> r
49
- unV f _ (V (Left err)) = f err
50
- unV _ g (V (Right result)) = g result
48
+ -- | Takes two functions an a `V` value, if the validation failed the error is
49
+ -- | applied to the first function, if the validation succeeded the inner value
50
+ -- | is applied to the second function.
51
+ validation :: forall err result r . (err -> r ) -> (result -> r ) -> V err result -> r
52
+ validation f _ (V (Left err)) = f err
53
+ validation _ g (V (Right result)) = g result
54
+
55
+ -- | Deprecated previous name of `validation`.
56
+ unV :: forall err result r . Warn (Text " 'unV' is deprecated, use 'validation' instead" ) => (err -> r ) -> (result -> r ) -> V err result -> r
57
+ unV = validation
51
58
52
59
-- | Fail with a validation error.
53
60
invalid :: forall err result . err -> V err result
@@ -69,7 +76,7 @@ toEither (V e) = e
69
76
-- | (`>>=` would be expected to be consistent).
70
77
andThen :: forall err a b . V err a -> (a -> V err b ) -> V err b
71
78
andThen v1 f =
72
- unV invalid f v1
79
+ validation invalid f v1
73
80
74
81
derive instance eqV :: (Eq err , Eq result ) => Eq (V err result )
75
82
derive instance eq1V :: Eq err => Eq1 (V err )
@@ -110,10 +117,10 @@ instance plusV :: Semiring err => Plus (V err) where
110
117
empty = V (Left zero)
111
118
112
119
instance foldableV :: Foldable (V err ) where
113
- foldMap = unV (const mempty)
114
- foldr f b = unV (const b) (flip f b)
115
- foldl f b = unV (const b) (f b)
120
+ foldMap = validation (const mempty)
121
+ foldr f b = validation (const b) (flip f b)
122
+ foldl f b = validation (const b) (f b)
116
123
117
124
instance traversableV :: Traversable (V err ) where
118
- sequence = unV (pure <<< V <<< Left ) (map (V <<< Right ))
119
- traverse f = unV (pure <<< V <<< Left ) (map (V <<< Right ) <<< f)
125
+ sequence = validation (pure <<< V <<< Left ) (map (V <<< Right ))
126
+ traverse f = validation (pure <<< V <<< Left ) (map (V <<< Right ) <<< f)
0 commit comments