Skip to content

Instantly share code, notes, and snippets.

Created May 7, 2017 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/ba9ee6d11867731db5d2ce614d829260 to your computer and use it in GitHub Desktop.
Save anonymous/ba9ee6d11867731db5d2ce614d829260 to your computer and use it in GitHub Desktop.
takeWhileMap
{
"version": "1.0.0",
"summary": "provides takeWhileMap, function is executed only while it returns Just smth, first Nothing stops the execution",
"repository": "https://github.com/user/project.git",
"license": "BSD3",
"source-directories": [
"."
],
"exposed-modules": [],
"dependencies": {
"elm-lang/core": "5.1.1 <= v < 5.1.1",
"elm-lang/html": "2.0.0 <= v < 2.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}
<html>
<head>
<style>
html {
background: #F7F7F7;
color: red;
}
</style>
</head>
<body>
<script>
var app = Elm.Main.fullscreen()
</script>
</body>
</html>
module Main exposing (..)
import Html exposing (Html, text)
import List exposing (..)
takeWhileMap : (a -> Maybe b) -> List a -> List b
takeWhileMap f src =
Tuple.second
(List.foldl
(\item acc ->
case acc of
( True, res ) ->
case (f item) of
Just val -> ( True, res ++ [ val ] )
Nothing -> ( False, res )
( False, res ) -> acc)
( True, [] )
src)
main : Html a
main =
text
(toString
(takeWhileMap
(\n -> if (n <= 3) then Just (n - 10) else Nothing)
[ 2, 1, 0, 1, 3, 5, 1, 0, 2, 3 ])
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment