Skip to content

Instantly share code, notes, and snippets.

@fredcy
Created April 7, 2016 14:28
Show Gist options
  • Save fredcy/c746ab5cda42d2042a33ce986298f69b to your computer and use it in GitHub Desktop.
Save fredcy/c746ab5cda42d2042a33ce986298f69b to your computer and use it in GitHub Desktop.
Apply (a -> Maybe b) function to list returning Nothing if any application results in Nothing, else Just (List b)
import Html exposing (text)
data1 = [ 1, 2, 3, 11 ]
fn1 x = if x < 12 then Just (x + 100) else Nothing
maybeMap : (a -> Maybe b) -> List a -> (Maybe (List b))
maybeMap fn list =
case list of
[] -> Just []
(h :: t) ->
case fn h of
Nothing -> Nothing
Just hb ->
case (maybeMap fn t) of
Nothing -> Nothing
Just tb -> Just (hb :: tb)
main =
text <| toString <| maybeMap fn1 [10, 11, 2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment