Skip to content

Instantly share code, notes, and snippets.

@scjudd
Created September 28, 2016 21:03
Show Gist options
  • Save scjudd/60a9c7d1558be07ebdec22888aedad22 to your computer and use it in GitHub Desktop.
Save scjudd/60a9c7d1558be07ebdec22888aedad22 to your computer and use it in GitHub Desktop.
A neat little recursive data structure I'm using in an Elm project.
module OptionTree exposing (..)
type Node meta a
= Tree meta (List (Node meta a))
| OneOf (List a)
| ManyOf (List a)
select : Node meta item -> item -> List item -> List item
select node item list =
case node of
Tree _ leaves ->
List.foldl (\leaf -> select leaf item) list leaves
OneOf items ->
if List.member item items then
item :: (List.filter (not << (flip List.member) items) list)
else
list
ManyOf items ->
if List.member item items then
item :: list
else
list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment