Skip to content

Rename unV to validation and deprecate it #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions src/Data/Validation/Semigroup.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

module Data.Validation.Semigroup
( V(..)
, validation
, unV
, invalid
, isValid
Expand All @@ -23,6 +24,7 @@ import Data.Foldable (class Foldable)
import Data.Ord (class Ord1)
import Data.Traversable (class Traversable)
import Data.Newtype (class Newtype)
import Prim.TypeError (class Warn, Text)

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

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

-- | Unpack the `V` type constructor, providing functions to handle the error
-- | and success cases.
unV :: forall err result r. (err -> r) -> (result -> r) -> V err result -> r
unV f _ (V (Left err)) = f err
unV _ g (V (Right result)) = g result
-- | Takes two functions an a `V` value, if the validation failed the error is
-- | applied to the first function, if the validation succeeded the inner value
-- | is applied to the second function.
validation :: forall err result r. (err -> r) -> (result -> r) -> V err result -> r
validation f _ (V (Left err)) = f err
validation _ g (V (Right result)) = g result

-- | Deprecated previous name of `validation`.
unV :: forall err result r. Warn (Text "'unV' is deprecated, use 'validation' instead") => (err -> r) -> (result -> r) -> V err result -> r
unV = validation

-- | Fail with a validation error.
invalid :: forall err result. err -> V err result
Expand All @@ -68,7 +75,7 @@ toEither (V e) = e
-- | (`>>=` would be expected to be consistent).
andThen :: forall err a b. V err a -> (a -> V err b) -> V err b
andThen v1 f =
unV invalid f v1
validation invalid f v1

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

instance foldableV :: Foldable (V err) where
foldMap = unV (const mempty)
foldr f b = unV (const b) (flip f b)
foldl f b = unV (const b) (f b)
foldMap = validation (const mempty)
foldr f b = validation (const b) (flip f b)
foldl f b = validation (const b) (f b)

instance traversableV :: Traversable (V err) where
sequence = unV (pure <<< V <<< Left) (map (V <<< Right))
traverse f = unV (pure <<< V <<< Left) (map (V <<< Right) <<< f)
sequence = validation (pure <<< V <<< Left) (map (V <<< Right))
traverse f = validation (pure <<< V <<< Left) (map (V <<< Right) <<< f)
29 changes: 18 additions & 11 deletions src/Data/Validation/Semiring.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
-- | with multiple alternatives.
module Data.Validation.Semiring
( V(..)
, validation
, unV
, invalid
, isValid
Expand All @@ -22,6 +23,7 @@ import Data.Foldable (class Foldable)
import Data.Ord (class Ord1)
import Data.Traversable (class Traversable)
import Data.Newtype (class Newtype)
import Prim.TypeError (class Warn, Text)

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

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

-- | Unpack the `V` type constructor, providing functions to handle the error
-- | and success cases.
unV :: forall err result r. (err -> r) -> (result -> r) -> V err result -> r
unV f _ (V (Left err)) = f err
unV _ g (V (Right result)) = g result
-- | Takes two functions an a `V` value, if the validation failed the error is
-- | applied to the first function, if the validation succeeded the inner value
-- | is applied to the second function.
validation :: forall err result r. (err -> r) -> (result -> r) -> V err result -> r
validation f _ (V (Left err)) = f err
validation _ g (V (Right result)) = g result

-- | Deprecated previous name of `validation`.
unV :: forall err result r. Warn (Text "'unV' is deprecated, use 'validation' instead") => (err -> r) -> (result -> r) -> V err result -> r
unV = validation

-- | Fail with a validation error.
invalid :: forall err result. err -> V err result
Expand All @@ -69,7 +76,7 @@ toEither (V e) = e
-- | (`>>=` would be expected to be consistent).
andThen :: forall err a b. V err a -> (a -> V err b) -> V err b
andThen v1 f =
unV invalid f v1
validation invalid f v1

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

instance foldableV :: Foldable (V err) where
foldMap = unV (const mempty)
foldr f b = unV (const b) (flip f b)
foldl f b = unV (const b) (f b)
foldMap = validation (const mempty)
foldr f b = validation (const b) (flip f b)
foldl f b = validation (const b) (f b)

instance traversableV :: Traversable (V err) where
sequence = unV (pure <<< V <<< Left) (map (V <<< Right))
traverse f = unV (pure <<< V <<< Left) (map (V <<< Right) <<< f)
sequence = validation (pure <<< V <<< Left) (map (V <<< Right))
traverse f = validation (pure <<< V <<< Left) (map (V <<< Right) <<< f)