Skip to content

Commit

Permalink
Replace Data.Validation.Semiring.unV by Data.Validation.Semiring.vali…
Browse files Browse the repository at this point in the history
…dation (#79)

* Replace Data.Validation.Semiring.unV by Data.Validation.Semiring.validation

* Import Data.Generic.Rep from prelude
  • Loading branch information
kl0tl authored Dec 28, 2020
1 parent 9ff354d commit 0c7051f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 13 deletions.
3 changes: 1 addition & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"devDependencies": {
"purescript-console": "master",
"purescript-assert": "master",
"purescript-record": "master",
"purescript-generics-rep": "master"
"purescript-record": "master"
}
}
1 change: 0 additions & 1 deletion spago.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
, "effect"
, "either"
, "foldable-traversable"
, "generics-rep"
, "integers"
, "js-uri"
, "lists"
Expand Down
18 changes: 9 additions & 9 deletions src/Routing/Match.purs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Data.Semiring.Free (Free, free)
import Data.String.NonEmpty (NonEmptyString)
import Data.String.NonEmpty as NES
import Data.Tuple (Tuple(..), snd)
import Data.Validation.Semiring (V, invalid, unV)
import Data.Validation.Semiring (V, invalid, validation)
import Data.Number as Number
import Routing.Match.Error (MatchError(..), showMatchError)
import Routing.Types (Route, RoutePart(..))
Expand All @@ -27,7 +27,7 @@ derive instance newtypeMatch :: Newtype (Match a) _

instance matchFunctor :: Functor Match where
map fn (Match r2e) = Match $ \r ->
unV invalid (\(Tuple rs a) -> pure $ Tuple rs (fn a)) $ r2e r
validation invalid (\(Tuple rs a) -> pure $ Tuple rs (fn a)) $ r2e r

instance matchAlt :: Alt Match where
alt (Match r2e1) (Match r2e2) = Match $ \r -> do
Expand All @@ -40,11 +40,11 @@ instance matchAlternative :: Alternative Match

instance matchApply :: Apply Match where
apply (Match r2a2b) (Match r2a) =
Match $ (\r -> unV (processFnErr r) processFnRes (r2a2b r))
Match $ (\r -> validation (processFnErr r) processFnRes (r2a2b r))
where processFnErr r err =
invalid $ err * unV identity (const one) (r2a r)
invalid $ err * validation identity (const one) (r2a r)
processFnRes (Tuple rs a2b) =
unV invalid (\(Tuple rss a) -> pure $ Tuple rss (a2b a)) (r2a rs)
validation invalid (\(Tuple rss a) -> pure $ Tuple rss (a2b a)) (r2a rs)

instance matchApplicative :: Applicative Match where
pure a = Match \r -> pure $ Tuple r a
Expand Down Expand Up @@ -160,7 +160,7 @@ list (Match r2a) =
Match $ go Nil
where go :: List a -> Route -> V (Free MatchError) (Tuple Route (List a))
go accum r =
unV
validation
(const $ pure (Tuple r (reverse accum)))
(\(Tuple rs a) -> go (Cons a accum) rs)
(r2a r)
Expand All @@ -169,7 +169,7 @@ list (Match r2a) =
-- [[String]] -fold with semicolon-> [String] -fold with newline-> String
runMatch :: forall a. Match a -> Route -> Either String a
runMatch (Match fn) route =
unV foldErrors (Right <<< snd) $ fn route
validation foldErrors (Right <<< snd) $ fn route
where
foldErrors errs =
Left $ foldl (\b a -> a <> "\n" <> b) "" do
Expand All @@ -195,7 +195,7 @@ runMatch (Match fn) route =
-- | ```
eitherMatch :: forall a b. Match (Either a b) -> Match b
eitherMatch (Match r2eab) = Match $ \r ->
unV invalid runEither $ (r2eab r)
validation invalid runEither $ (r2eab r)
where
runEither (Tuple rs eit) =
case eit of
Expand All @@ -209,4 +209,4 @@ eitherMatch (Match r2eab) = Match $ \r ->
-- | -- (Right (fromFoldable [(Tuple "a" "1")]))
-- | ```
optionalMatch :: forall a. Match a -> Match (Maybe a)
optionalMatch (Match fn) = Match (\route -> unV (const $ pure (Tuple route Nothing)) (pure <<< map Just) $ fn route)
optionalMatch (Match fn) = Match (\route -> validation (const $ pure (Tuple route Nothing)) (pure <<< map Just) $ fn route)
2 changes: 1 addition & 1 deletion test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import Data.Bifunctor (lmap)
import Data.Either (Either(..))
import Data.Foldable (oneOf)
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Show (genericShow)
import Data.List (List)
import Data.List as L
import Data.Map as M
import Data.Show.Generic (genericShow)
import Data.String.NonEmpty (NonEmptyString)
import Data.String.NonEmpty as NES
import Data.Tuple (Tuple(..))
Expand Down

0 comments on commit 0c7051f

Please sign in to comment.