jxxcarlson / htree / HTree.String

HTree.String has some convenience functions for dealing with outlines and trees whose nodes are labeled by strings.

parse : String -> Tree String

> import HTree.String exposing(..)
> import Example.Test exposing(..)
> import HTree

o2 = -- from Example.Test
A
  p
  q
B
  r
  s
C

> t = parse o2
  Tree "*" [
     Tree "A" [Tree "p" [],Tree "q" []]
    ,Tree "B" [Tree "r" [],Tree "s" []]
    ,Tree "C" []]

> t = parse o2 |> HTree.tagWithDepth
  Tree ("*",0) [
     Tree ("A",1) [Tree ("p",2) [],Tree ("q",2) []]
    ,Tree ("B",1) [Tree ("r",2) [],Tree ("s",2) []]
    ,Tree ("C",1) []]

toString : Tree String -> String

Compute the string representation of a tree labeled by strings

> t = parse o2

toString t

 "*\nA\n  p\n  q\nB\n  r\n  s\nC"

toOutline : Tree String -> String

Compute the string representation of a tree labeled by strings; omit the root label

> toOutline t
"A\n  p\n  q\nB\n  r\n  s\nC"

level : String -> Basics.Int

Compute the level of a string: the number of leading spaces, divided by 2