Skip to content

Instantly share code, notes, and snippets.

@gnfisher
Created November 7, 2019 20:07
Show Gist options
  • Save gnfisher/1e9df20205a162e1077e29c28a52307f to your computer and use it in GitHub Desktop.
Save gnfisher/1e9df20205a162e1077e29c28a52307f to your computer and use it in GitHub Desktop.
map2 : (a -> b -> c) -> Validatable a -> Validatable b -> Validatable c
map2 func v1 v2 =
case ( v1, v2 ) of
( Invalid a fatal warnings, Valid b ) ->
Invalid (func a b) fatal warnings
( Invalid a fa wa, Warnings b wb ) ->
Invalid (func a b) fa (wa ++ Nonempty.toList wb)
( Invalid a fa wa, Invalid b fb wb ) ->
Invalid (func a b) (Nonempty.append fa fb) (wa ++ wb)
( Warnings a wa, Valid b ) ->
Warnings (func a b) wa
( Warnings a wa, Warnings b wb ) ->
Warnings (func a b) (Nonempty.append wa wb)
( Warnings a wa, Invalid b fb wb ) ->
Invalid (func a b) fb (Nonempty.toList wa ++ wb)
( Valid a, Valid b ) ->
Valid (func a b)
( Valid a, Warnings b wb ) ->
Warnings (func a b) wb
( Valid a, Invalid b fb wb ) ->
Invalid (func a b) fb wb
map6 : (a -> b -> c -> d -> e -> f -> g) ->
(Validatable a) ->
(Validatable b) ->
(Validatable c) ->
(Validatable d) ->
(Validatable e) ->
(Validatable f) ->
(Validatable g)
map6 sixArgFuc va vb vc vd ve vf =
Valid sixArgFuc
|> andMap va
|> andMap vb
|> andMap vc
|> andMap vd
|> andMap ve
|> andMap vf
andMap : Validatable a -> Validatable (a -> b) -> Validatable b
andMap itemV functionV =
map2 (\item function -> function item) itemV functionV
map2 : (a -> b -> c) -> Validatable a -> Validatable b -> Validatable c
map2 func v1 v2 =
Valid func
|> andMap v1
|> andMap v2
andMap (Valid 1) (Valid increment)
-- Valid 2
Valid (\x -> x + 1) -- Validatable (Int -> Int)
|> andMap (Valid 1) -- Validatable Int
Valid (\x y -> x + y) -- Validatable (Int -> (Int -> Int))
|> andMap (Valid 1) -- Validtable (Int -> Int)
|> andMap (Valid 2) -- Validatable Int
Valid (\x y -> x + y) <*> (Valid 1) <*> (Valid 2)
-- Valid 3
Valid (\x y -> x + y) (Valid 1) (Valid 2)
-- Valid 3
Valid (\x y z -> x + y + z) -- Validatable (Int -> (Int -> (Int -> Int)))
|> andMap (Valid 3) -- Validatable (Int -> (Int -> Int))
|> andMap (Valid 1) -- Validtable (Int -> Int)
|> andMap (Valid 2) -- Validatable Int
-- Valid 6
f1 = Valid "hi"
f2 = Invalid "world" (Nonempty Apie.Api.Enum.Fatal.MustBePresent) []
someFunction (\x y -> { name = x, cost = y }) validatableX validtableY
-- Valid { name = "hi", cost = "world" }
-- Invalid "hello world" (Nonempty Apie.Api.Enum.Fatal.MustBePresent) []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment