Skip to content

Instantly share code, notes, and snippets.

@joefiorini
Created January 8, 2015 17:06
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 joefiorini/696bc10f0e4d7c33616d to your computer and use it in GitHub Desktop.
Save joefiorini/696bc10f0e4d7c33616d to your computer and use it in GitHub Desktop.
String Calculator Kata in Elm
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