Working with hierarchical lists.
fromList : a -> (a -> Basics.Int) -> List a -> Tree a
Given:
fromList
returns the corresponding rose tree. For an example, consider this snippet
-- IMPORTS
import Example.Test as Example exposing (o2)
import HTree.String as HS
import Tree exposing (Tree)
import HTree exposing(..)
-- INPUT STRING
o2 =
"""
A
p
q
B
"""
-- CODE
data : List String
data = String.split "\n" o2 |> List.filter (not << String.isEmpty)
--> ["A"," p"," q","B"]
tree : Tree String
tree =
-- `HS.level` returns the number of leading spaces
-- divided by 2 using integer division.
fromList "*" HS.level data
--> Tree "*"
--> [ Tree "A" [ Tree " p" [], Tree " q" [] ]
--> , Tree "B" []
--> ]
flatten : Tree b -> List b
Flatten a tree into a list of labels.
Tree.map (\label -> String.trim label) tree
--> Tree "*"
--> [ Tree "A" [ Tree "p" [], Tree "q" [] ]
--> , Tree "B" []
--> ]
Tree.map (\label -> String.trim label) tree
|> toList
--> [ "*", "A", "p", "q", "B"]
toOutline : (a -> String) -> Tree a -> String
Given a function that maps labels to strings, construct a string that represents the tree as an outline.
> t |> toOutline identity
"*\nA\n p\n q\nB\n r\n s\nC" : String
The string returned is
*
A
p
q
B
depth : Tree a -> Basics.Int
depth t is the depth of the tree.
> depth t
2 : Int
nodeCount : Tree a -> Basics.Int
nodecount t is the number of notes in the tree t
> nodeCount t
8 : Int
The count includes the root.
tagWithDepth : Tree a -> Tree ( a, Basics.Int )
The tagWithDepth
function transforms a tree of items into
a tree of tuples of the form (a, k)
, where k
is the
depth of a
in the tree.
> tagWithDepth (Tree.map String.trim tree)
Tree ("*",0) [
Tree ("A",1) [Tree ("p",2) [],Tree ("q",2) []]
,Tree ("B",1) []
]