Last active
August 29, 2015 14:03
-
-
Save m1el/1bf5758bfb0da6a7e9a6 to your computer and use it in GitHub Desktop.
playing with maybes in elm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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