tomjkidd / elm-multiway-tree-zipper / MultiwayTree

A library for constructing multi-way trees. Each Tree carries two pieces of information, it's datum and children.

Types


type Tree a
    = Tree a (Forest a)

A type to keep track of datum and children.


type alias Forest a =
List (Tree a)

A list of Trees. Convenient for describing children.

Operations

datum : Tree a -> a

Access the datum of the current tree

children : Tree a -> Forest a

Access the children of the current tree

foldl : (a -> b -> b) -> b -> Tree a -> b

Reduce a Tree from the left.

foldr : (a -> b -> b) -> b -> Tree a -> b

Reduce a Tree from the right.

flatten : Tree a -> List a

Flattens a Tree into a List where the root is the first element of that list.

tuplesOfDatumAndFlatChildren : Tree a -> List ( a, List a )

A special version of flatten which flattens a Tree into a List of Tuples like (element, [all elements in subtree])

Tree.tuplesOfDatumAndFlatChildren
    Tree
    "a"
    [ Tree "b" []
    , Tree "c" []
    , Tree "d" []
    ]
    == [ ( "a", [ "b", "c", "d" ] ), ( "b", [] ), ( "c", [] ), ( "d", [] ) ]

filter : (a -> Basics.Bool) -> Tree a -> Maybe (Tree a)

Filter the MultiwayTree. Keep only elements whose datum satisfy the predicate.

filterWithChildPrecedence : (a -> Basics.Bool) -> Tree a -> Maybe (Tree a)

Filter the MultiwayTree. If the predicate is True for a Child the entire path to the root will be part of the result Tree.

length : Tree a -> Basics.Int

Return the length of the Tree. Calculated recusively as datum (1) + length of children (n) Since a MultiwayTree is never empty this function will never return Int < 1.

insertChild : Tree a -> Tree a -> Tree a

Inserts a Tree as the first child of a Tree

appendChild : Tree a -> Tree a -> Tree a

Inserts a Tree as the last child of a Tree

Mapping

map : (a -> b) -> Tree a -> Tree b

Map over the MultiwayTree

mapListOverTree : (a -> b -> result) -> List a -> Tree b -> Maybe (Tree result)

Map a Function over a List and a MultiwayTree.

indexedMap : (Basics.Int -> a -> b) -> Tree a -> Maybe (Tree b)

Same as map but the function is also applied to the index of each element (starting at zero).

Sorting

sortBy : (a -> comparable) -> Tree a -> Tree a

Sort values by a derived property. Does not alter the nesting structure of the Tree, that is it does not move Nodes up or down levels.

sortBy identity
    Tree
    "a"
    [ Tree "b" []
    , Tree "d" []
    , Tree "c" []
    ]
    == Tree "a"
        [ Tree "b" []
        , Tree "c" []
        , Tree "d" []
        ]

sortWith : (a -> a -> Basics.Order) -> Tree a -> Tree a

Sort values with a custom comparison function like:

flippedComparison a b =
    case compare a b of
      LT -> GT
      EQ -> EQ
      GT -> LT

This is also the most general sort function, allowing you
to define any other.