Skip to content

Instantly share code, notes, and snippets.

@m1el
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m1el/1bf5758bfb0da6a7e9a6 to your computer and use it in GitHub Desktop.
Save m1el/1bf5758bfb0da6a7e9a6 to your computer and use it in GitHub Desktop.
playing with maybes in elm
-- http://elm-lang.org/try
import Regex (..)
import Maybe (..)
import Graphics.Input as Input
import Graphics.Input.Field as Field
maybeMatch : String -> String -> Maybe Match
maybeMatch reg str =
case find (AtMost 1) (regex reg) str of
[] -> Nothing
x::xs -> Just x
-- should be a library function
fmap : (a -> b) -> Maybe a -> Maybe b
fmap fn x = case x of
Nothing -> Nothing
Just x -> Just (fn x)
-- should be a library function
orElse : Maybe a -> Maybe a -> Maybe a
orElse a b = case a of
Nothing -> b
_ -> a
result : String -> String
result str =
let mbyResult reg res = (\_->res) `fmap` (maybeMatch reg str)
foo = mbyResult "foo" "foo matched"
bar = mbyResult "bar" "bar matched"
in maybe "nothing matched" id (foo `orElse` bar)
content = Input.input Field.noContent
main : Signal Element
main =
let input = Field.field Field.defaultStyle content.handle id "enter a string"
output str = str.string |> result |> plainText
display content = flow down [ input content, output content ]
in lift display content.signal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment