Skip to content

Instantly share code, notes, and snippets.

@akoppela
Last active February 5, 2018 06:49
Show Gist options
  • Save akoppela/e17716b748935d1bd76894f42e362863 to your computer and use it in GitHub Desktop.
Save akoppela/e17716b748935d1bd76894f42e362863 to your computer and use it in GitHub Desktop.
{-| Takes two maybe values and maybe test. Returns first maybe value if test is just true.
Otherwise returns second maybe value if test is just false.
-}
maybeAndWhenOrHelper : Maybe a -> Maybe a -> Maybe Bool -> Maybe a
maybeAndWhenOrHelper maybeValueWhen maybeValueWhenNot =
let
doTest test =
if test then
maybeValueWhen
else
maybeValueWhenNot
in
Maybe.andThen doTest
{-| Takes two values and maybe test. Returns just first value if test is just true.
Otherwise returns just second value if test is just false.
maybeAndWhenOr 42 24 (Just True) == Just 42
maybeAndWhenOr 42 24 (Just False) == Just 24
maybeAndWhenOr 42 24 Nothing == Nothing
-}
maybeAndWhenOr : a -> a -> Maybe Bool -> Maybe a
maybeAndWhenOr valueWhen valueWhenNot =
maybeAndWhenOrHelper (Just valueWhen) (Just valueWhenNot)
{-| Takes some value and maybe test. Returns just value if test is just true.
Otherwise returns nothing.
maybeAndWhen 42 (Just True) == Just 42
maybeAndWhen 42 (Just False) == Nothing
maybeAndWhen 42 Nothing == Nothing
-}
maybeAndWhen : a -> Maybe Bool -> Maybe a
maybeAndWhen value =
maybeAndWhenOrHelper (Just value) Nothing
{-| Takes some value and maybe test. Returns just value if test is just false.
Otherwise returns nothing.
maybeAndWhenNot 42 (Just True) == Nothing
maybeAndWhenNot 42 (Just False) == Just 42
maybeAndWhenNot 42 Nothing == Nothing
-}
maybeAndWhenNot : a -> Maybe Bool -> Maybe a
maybeAndWhenNot value =
maybeAndWhenOrHelper Nothing (Just value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment