Skip to content

Instantly share code, notes, and snippets.

@sgdan
Created November 5, 2017 12:36
Show Gist options
  • Save sgdan/0447a69d9143ea33cf90b578cd741cae to your computer and use it in GitHub Desktop.
Save sgdan/0447a69d9143ea33cf90b578cd741cae to your computer and use it in GitHub Desktop.
Iterate through an Elm list and compare each element against all the others
{--
Iterate through a list and compare each element to all the others to decide what to keep
Run at http://elm-lang.org:1234/try
--}
import Html exposing (text, div, br)
import String exposing (..)
initial = ["a", "ab", "c", "cdb", "cd"]
isChild i j = String.startsWith i j && not (i == j)
hasParentIn item list = List.any (\i -> isChild i item) list
parents = List.filter (\i -> not (hasParentIn i initial)) initial
main = div [] [
"initial: " ++ toString initial |> text, br[][],
"parents: " ++ toString parents |> text, br[][],
"isChild a ab: " ++ toString (isChild "a" "ab") |> text, br[][],
"isChild ab a: " ++ toString (isChild "ab" "a") |> text, br[][],
"isChild a a: " ++ toString (isChild "a" "a") |> text, br[][],
"hasParentIn ab initial: " ++ toString (hasParentIn "ab" initial) |> text, br[][],
"hasParentIn b initial: " ++ toString (hasParentIn "b" initial) |> text, br[][]
]
{--
Should see the following output:
initial: ["a","ab","c","cdb","cd"]
parents: ["a","c"]
isChild a ab: True
isChild ab a: False
isChild a a: False
hasParentIn ab initial: True
hasParentIn b initial: False
--}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment