jxxcarlson/htree/1.0.2

NOTE: the latest version is 2.0.1

HTree

Convert hierarchical lists to a rose tree, convert a rose tree to a list of node labels, convert a rose tree to a string representing the corresponding outline, etc. See HTreeExample.elm.

fromList : a -> (a -> Int) -> List a -> Tree a

Suppose give an list of items of type a, a "root" element of type a, and a function that determines the level of an item. The function fromList returns the corresponding rose tree. For an example, consider the outline given by the string `Example.Test.o3:

A
  p
    1
    2
    3
  q
B
  r
  s
C

Then we have

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


> data = String.split "\n" o2 |> List.filter (\line -> line /= "")
["A","  p","  q","B","  r","  s","C"]
    : List String


> t = fromList "*" HS.level data
Tree "*"
   [Tree "A" [Tree ("  p") [],Tree ("  q") []]
   ,Tree "B" [Tree ("  r") [],Tree ("  s") []]
   ,Tree "C" []]

The function HS.level returns the number of leading spaces divided by 2 -- integer division, no remainder.

toList : Tree b -> List b

Map a tree to a list of node contents:

> import Tree


> t |> Tree.map (\label -> String.trim label)
  Tree "*" [
     Tree "A" [Tree "p" [],Tree "q" []]
    ,Tree "B" [Tree "r" [],Tree "s" []]
    ,Tree "C" []]


 > t |> Tree.map (\label -> String.trim label) |> toList
   ["*","A","p","q","B","r","s","C"]
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
        r
        s
    C
tag : Tree a -> Tree ( a, Int )

The tag 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.

> tag (Tree.map String.trim t)
  Tree ("*",0) [
    Tree ("A",1) [Tree ("p",2) [],Tree ("q",2) []]
   ,Tree ("B",1) [Tree ("r",2) [],Tree ("s",2) []]
   ,Tree ("C",1) []]
depth : Tree a -> Int

depth t is the depth of the tree.

> depth t
2 : I
nodeCount : Tree a -> Int

nodecount t is the number of notes in the tree t

> nodeCount t
8 : Int

The count includes the root.