Last active
February 25, 2021 01:52
-
-
Save mauroc8/905ea147acd2d9d26dbd3e65a1602db7 to your computer and use it in GitHub Desktop.
NodeId.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 Utils.NodeId exposing (NodeId, child, root, toZipper) | |
import Tree exposing (Tree) | |
import Tree.Zipper as Zipper exposing (Zipper) | |
--- NODE ID | |
type NodeId | |
= Child Int NodeId | |
| Root | |
root : NodeId | |
root = | |
Root | |
child : Int -> NodeId -> NodeId | |
child = | |
Child | |
toZipper : | |
Tree a | |
-> NodeId | |
-> Maybe (Zipper a) | |
toZipper treeRoot nodeId = | |
case nodeId of | |
Root -> | |
Zipper.fromTree treeRoot | |
|> Just | |
Child index nextNodeId -> | |
toZipper treeRoot nextNodeId | |
|> Maybe.andThen Zipper.firstChild | |
|> Maybe.andThen (repeat index Zipper.nextSibling) | |
repeat : Int -> (x -> Maybe x) -> x -> Maybe x | |
repeat times fn x = | |
if times == 0 then | |
Just x | |
else | |
repeat (abs times - 1) fn x | |
|> Maybe.andThen fn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment