git.iko.soy/rowtype-yoga/purescript-yoga-variation
on main help sign in
Add helpers
commit53e67c2161221a58454965fd36f1fa6c729a0e59 authorsigma-andex <77549848+sigma-andex@users.noreply.github.com> dateTue 10 Jan 2023, 11:49 (4 years ago) parent50ffacd
3 files changed+59−9
README.md+1 spago.dhall+9−1 src/Yoga/Variation/Variation.purs+49−8
README.md+1
@@ -1 +1,2 @@
11# purescript-yoga-variation
2+
spago.dhall+9−1
@@ -12,7 +12,15 @@ to generate this file without the comments in this block.
1212-}
1313{ name = "my-project"
1414, dependencies =
15− [ "arrays", "console", "effect", "prelude", "validation", "variant" ]
15+ [ "arrays"
16+ , "bifunctors"
17+ , "console"
18+ , "effect"
19+ , "either"
20+ , "prelude"
21+ , "validation"
22+ , "variant"
23+ ]
1624, packages = ./packages.dhall
1725, sources = [ "src/**/*.purs", "test/**/*.purs" ]
1826}
src/Yoga/Variation/Variation.purs+49−8
@@ -1,19 +1,28 @@
11module Yoga.Variation.Variation
22 ( Validated
3+ , fromEither
34 , invalid
5+ , invalidM
46 , key
7+ , mapErrors
8+ , module Validation
9+ , module Variant
510 , valid
6− )
7− where
11+ , validM
12+ ) where
813
914import Prelude
1015
1116import Data.Array.NonEmpty (NonEmptyArray)
1217import Data.Array.NonEmpty as NonEmpty
18+import Data.Bifunctor (class Bifunctor, lmap)
19+import Data.Either (Either, either)
1320import Data.Symbol (class IsSymbol)
14−import Data.Validation.Semigroup as Validation
15−import Data.Variant as Variant
16−import Prim.Row (class Cons)
21+import Data.Validation.Semigroup as ValidationSG
22+import Data.Validation.Semigroup hiding (invalid) as Validation
23+import Data.Variant (class Contractable, class VariantBounded, class VariantBoundedEnums, class VariantEqs, class VariantMapCases, class VariantMatchCases, class VariantOrds, class VariantShows, class VariantTraverseCases, Unvariant(..), Unvariant', Variant, case_, contract, default, expand, inj, match, on, onMatch, over, overOne, overSome, prj, revariant, traverse, traverseOne, traverseSome, unvariant, variantBounded, variantBoundedEnums, variantEqs, variantOrds, variantShows) as Variant
24+import Prim.Row (class Cons, class Union)
25+import Prim.RowList (class RowToList)
1726import Type.Proxy (Proxy(..))
1827
1928type Validated err result = Validation.V (NonEmptyArray (Variant.Variant err)) result
@@ -21,14 +30,46 @@ type Validated err result = Validation.V (NonEmptyArray (Variant.Variant err)) r
2130valid :: forall err result. result -> Validated err result
2231valid = pure
2332
33+validM :: forall m err result. Applicative m => result -> m (Validated err result)
34+validM = valid >>> pure
35+
2436invalid
25− ∷ ∀ (t63 ∷ Type) (sym ∷ Symbol) (error ∷ Type) (others ∷ Row Type) (errors ∷ Row Type)
37+ ∷ ∀ (result ∷ Type) (sym ∷ Symbol) (error ∷ Type) (others ∷ Row Type) (errors ∷ Row Type)
2638 . Cons sym error others errors
2739 ⇒ IsSymbol sym
2840 ⇒ Proxy sym
2941 → error
30− → Validation.V (NonEmptyArray (Variant.Variant errors)) t63
31−invalid proxy value = Validation.invalid $ NonEmpty.singleton $ Variant.inj proxy value
42+ → Validation.V (NonEmptyArray (Variant.Variant errors)) result
43+invalid proxy value = ValidationSG.invalid $ NonEmpty.singleton $ Variant.inj proxy value
44+
45+invalidM
46+ ∷ ∀ (result ∷ Type) (sym ∷ Symbol) (error ∷ Type) (others ∷ Row Type) (errors ∷ Row Type) m
47+ . Applicative m
48+ => Cons sym error others errors
49+ ⇒ IsSymbol sym
50+ ⇒ Proxy sym
51+ → error
52+ → m (Validation.V (NonEmptyArray (Variant.Variant errors)) result)
53+invalidM proxy = invalid proxy >>> pure
54+
55+fromEither
56+ :: ∀ (result ∷ Type) (sym ∷ Symbol) (error ∷ Type) (others ∷ Row Type) (errors ∷ Row Type)
57+ . Cons sym error others errors
58+ ⇒ IsSymbol sym
59+ ⇒ Proxy sym
60+ -> Either error result
61+ -> Validated errors result
62+fromEither proxy = either (invalid proxy) valid
3263
3364key ∷ ∀ (sym ∷ Symbol). Proxy sym
3465key = Proxy
66+
67+mapErrors
68+ :: forall result handlersRl handlers errorsRemaining errorsOut errorsIn
69+ . RowToList handlers handlersRl
70+ => Variant.VariantMatchCases handlersRl errorsRemaining (Variant.Variant errorsOut)
71+ => Union errorsRemaining errorsOut errorsIn
72+ => Record handlers
73+ -> Validated errorsIn result
74+ -> Validated errorsOut result
75+mapErrors handlers = lmap (map $ Variant.onMatch handlers identity)