Created
March 22, 2019 09:05
-
-
Save TwistingTwists/592a31b04f8e67728e27e159b2115c6f to your computer and use it in GitHub Desktop.
recursive nodes in Elm
This file contains hidden or 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 Main exposing (main) | |
import Browser | |
import Html exposing (..) | |
import Html.Events exposing (onClick) | |
-- The node value and a list of child nodes | |
type Node a | |
= Node a (List (Node a)) | |
type alias Model = | |
{ root : Node Int } | |
initTree : Node Int | |
initTree = | |
Node -22 | |
[ Node 42 [] | |
, Node 100 [] | |
, Node 43 | |
[ Node 87 [] | |
, Node 88 | |
[ Node 77 [] | |
, Node 69 [] | |
] | |
, Node 33 [] | |
] | |
, Node 10 [] | |
] | |
initialModel : Model | |
initialModel = | |
{ root = initTree | |
} | |
viewNode : Node Int -> Html msg | |
viewNode (Node value nodes) = | |
li [] | |
(List.append | |
[ text (String.fromInt value) ] | |
[ ul [] (List.map viewNode nodes) ] | |
) | |
view : Model -> Html Msg | |
view model = | |
ul [] [ viewNode model.root ] | |
type Msg | |
= NoOp | |
update : Msg -> Model -> Model | |
update msg model = | |
case msg of | |
NoOp -> | |
model | |
main : Program () Model Msg | |
main = | |
Browser.sandbox | |
{ init = initialModel | |
, view = view | |
, update = update | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment