Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kkruups/472e5ba095eddaacfab72d3f0f9ba2d9 to your computer and use it in GitHub Desktop.
Save kkruups/472e5ba095eddaacfab72d3f0f9ba2d9 to your computer and use it in GitHub Desktop.
Elm Insights: Article on Pipelining
{-
To run in elmO (elm Online Editor): copy n paste into http://elm-lang.org/try
New andThen Pipeline Pattern, replaces `andThen` infix operation
introduced in v0.18 to
Result.andThen
Maybe.andThen
Json.Decode.andThen
Task.andThen
--}
import Html exposing (text, div, li, a)
import Html.Attributes as Attributes exposing (style, href)
--Last callback in andThen Pipeline
isFloatBetweenZeroAnd100: Float -> Result String Float
isFloatBetweenZeroAnd100 num =
if num >= 0.0 && num <= 100.0 then
Ok (num * 10)
else
Err "Not a number between 0.0 and 100.0"
--andThen Pipeline Pattern
changeIntStringToFloatnRaiseItBy10: String -> Result String Float
changeIntStringToFloatnRaiseItBy10 str =
(String.toInt str)
|>Result.andThen (\x -> String.toFloat( toString x)) -- inline anonymous callback
|>Result.andThen (isFloatBetweenZeroAnd100 )
main =
div[
Attributes.style[
("padding", "30px")
,("color", "red")
]
] [ text <| toString <| changeIntStringToFloatnRaiseItBy10 "24"
,li[style[ ("padding-top", "50px")
,("color", "skyblue")
] ]
[
a [
href "https://medium.com/@kkruups/elm-insights-c067c66161bb#.2dnoytme1"
,style[ ("padding-top", "50px")
,("color", "dodgerblue")
,("font-style", "italic")
]
][
text "elm Insights: Farewell Backticks, Hello Pipeline"
]
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment