Created
April 1, 2018 21:56
-
-
Save mgyongyosi/6c736b9087d7231e8c652fe479d7b18e to your computer and use it in GitHub Desktop.
List.pairwise 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
module Common.List exposing (pairwise) | |
import List exposing (drop, head, length, take) | |
import List.Extra exposing (zip) | |
import Maybe.Extra exposing (toList) | |
pairwise : List a -> List ( a, a ) | |
pairwise lst = | |
let | |
grp = | |
take 2 lst | |
tup = | |
zip (take 1 grp) (drop 1 grp) | |
|> head | |
|> toList | |
remaining = | |
drop 1 lst | |
in | |
if 2 == length grp then | |
List.append tup (pairwise remaining) | |
else | |
[] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This should be equivalent: