Created
January 8, 2015 17:06
-
-
Save joefiorini/696bc10f0e4d7c33616d to your computer and use it in GitHub Desktop.
String Calculator Kata 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
import Graphics.Element (..) | |
import Graphics.Input.Field (Content, Selection, Direction(..)) | |
import Graphics.Input.Field as Field | |
import Signal | |
import Time | |
import String | |
import List | |
import Result | |
import Text (plainText) | |
import Debug | |
content : Signal.Channel Field.Content | |
content = | |
Signal.channel Field.noContent | |
convert result = | |
case String.toFloat result of | |
Result.Ok n -> n | |
Result.Err s -> 0 | |
calculate : String -> Float | |
calculate s = | |
List.foldl (+) 0 <| List.map convert <| spaceSplit s | |
spaceSplit = | |
String.split " " | |
sumString = | |
toString << calculate << (.string) | |
main : Signal Element | |
main = | |
Signal.map (\s -> (scene s) <| sumString s) <| Signal.subscribe content | |
scene : Field.Content -> String -> Element | |
scene fieldContent result = | |
flow down | |
[ Field.field Field.defaultStyle (Signal.send content) "Type here!" fieldContent | |
, plainText result | |
] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment