Skip to content

Instantly share code, notes, and snippets.

@mauroc8
Last active February 25, 2021 01:52
Show Gist options
  • Save mauroc8/905ea147acd2d9d26dbd3e65a1602db7 to your computer and use it in GitHub Desktop.
Save mauroc8/905ea147acd2d9d26dbd3e65a1602db7 to your computer and use it in GitHub Desktop.
NodeId.elm
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