Skip to content

Commit 94ea28b

Browse files
authored
Rename unV to validation and deprecate it (#33)
1 parent 4441c72 commit 94ea28b

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

src/Data/Validation/Semigroup.purs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
module Data.Validation.Semigroup
88
( V(..)
9+
, validation
910
, unV
1011
, invalid
1112
, isValid
@@ -23,6 +24,7 @@ import Data.Foldable (class Foldable)
2324
import Data.Ord (class Ord1)
2425
import Data.Traversable (class Traversable)
2526
import Data.Newtype (class Newtype)
27+
import Prim.TypeError (class Warn, Text)
2628

2729
-- | The `V` functor, used for applicative validation
2830
-- |
@@ -42,11 +44,16 @@ newtype V err result = V (Either err result)
4244

4345
derive instance newtypeV :: Newtype (V err result) _
4446

45-
-- | Unpack the `V` type constructor, providing functions to handle the error
46-
-- | and success cases.
47-
unV :: forall err result r. (err -> r) -> (result -> r) -> V err result -> r
48-
unV f _ (V (Left err)) = f err
49-
unV _ g (V (Right result)) = g result
47+
-- | Takes two functions an a `V` value, if the validation failed the error is
48+
-- | applied to the first function, if the validation succeeded the inner value
49+
-- | is applied to the second function.
50+
validation :: forall err result r. (err -> r) -> (result -> r) -> V err result -> r
51+
validation f _ (V (Left err)) = f err
52+
validation _ g (V (Right result)) = g result
53+
54+
-- | Deprecated previous name of `validation`.
55+
unV :: forall err result r. Warn (Text "'unV' is deprecated, use 'validation' instead") => (err -> r) -> (result -> r) -> V err result -> r
56+
unV = validation
5057

5158
-- | Fail with a validation error.
5259
invalid :: forall err result. err -> V err result
@@ -68,7 +75,7 @@ toEither (V e) = e
6875
-- | (`>>=` would be expected to be consistent).
6976
andThen :: forall err a b. V err a -> (a -> V err b) -> V err b
7077
andThen v1 f =
71-
unV invalid f v1
78+
validation invalid f v1
7279

7380
derive instance eqV :: (Eq err, Eq result) => Eq (V err result)
7481
derive instance eq1V :: Eq err => Eq1 (V err)
@@ -101,10 +108,10 @@ instance monoidV :: (Semigroup err, Monoid a) => Monoid (V err a) where
101108
mempty = pure mempty
102109

103110
instance foldableV :: Foldable (V err) where
104-
foldMap = unV (const mempty)
105-
foldr f b = unV (const b) (flip f b)
106-
foldl f b = unV (const b) (f b)
111+
foldMap = validation (const mempty)
112+
foldr f b = validation (const b) (flip f b)
113+
foldl f b = validation (const b) (f b)
107114

108115
instance traversableV :: Traversable (V err) where
109-
sequence = unV (pure <<< V <<< Left) (map (V <<< Right))
110-
traverse f = unV (pure <<< V <<< Left) (map (V <<< Right) <<< f)
116+
sequence = validation (pure <<< V <<< Left) (map (V <<< Right))
117+
traverse f = validation (pure <<< V <<< Left) (map (V <<< Right) <<< f)

src/Data/Validation/Semiring.purs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
-- | with multiple alternatives.
44
module Data.Validation.Semiring
55
( V(..)
6+
, validation
67
, unV
78
, invalid
89
, isValid
@@ -22,6 +23,7 @@ import Data.Foldable (class Foldable)
2223
import Data.Ord (class Ord1)
2324
import Data.Traversable (class Traversable)
2425
import Data.Newtype (class Newtype)
26+
import Prim.TypeError (class Warn, Text)
2527

2628
-- | The `V` functor, used for alternative validation
2729
-- |
@@ -43,11 +45,16 @@ newtype V err result = V (Either err result)
4345

4446
derive instance newtypeV :: Newtype (V err result) _
4547

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
5158

5259
-- | Fail with a validation error.
5360
invalid :: forall err result. err -> V err result
@@ -69,7 +76,7 @@ toEither (V e) = e
6976
-- | (`>>=` would be expected to be consistent).
7077
andThen :: forall err a b. V err a -> (a -> V err b) -> V err b
7178
andThen v1 f =
72-
unV invalid f v1
79+
validation invalid f v1
7380

7481
derive instance eqV :: (Eq err, Eq result) => Eq (V err result)
7582
derive instance eq1V :: Eq err => Eq1 (V err)
@@ -110,10 +117,10 @@ instance plusV :: Semiring err => Plus (V err) where
110117
empty = V (Left zero)
111118

112119
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)
116123

117124
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

Comments
 (0)