Created
September 28, 2016 21:03
-
-
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.
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 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