dranges.tupletree
This is a module to deal with tuples as trees: polymorphic trees, like this:
Tuple!(int, Tuple!(string, char), double, Tuple!(string, Tuple!(string, char))) tree;
And then reducing them, mapping functions on them, etc. My goal is to link it with the pattern-matching modules.
License:
Boost License 1.0.
Authors:
Philippe Sigaud
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- template
isTree
(T)
- A well-formed heterogeneous tree has the form
tuple(payload, children...) where children
are trees themselves. payload can be of any type and is always there, except for the empty tree
(which is a PITA to deal with compared to normal/leaf trees and so may be discarded).
Example:
auto e = tuple(); // an (the) empty tree.
auto l1 = tuple(1); // leaf (payload == 1), no children.
auto l2 = tuple("a"); // another leaf, with a string payload: "a".
auto t1 = tuple((int i, string s) { return i+s.length;}, l1, l2); // a tree. Its payload is an anonymous function.
auto t2 = tuple('a', // Homogeneous tree encoded as a tuple-tree
tuple('b',
tuple('c'), tuple('d')),
tuple('e',
tuple('f')),
tuple('g'));
auto t3 = tuple( tuple(1), tuple(2), tuple(3) ); // t3 payload is tuple(1) (not particularly seen as a tree in this context), and two children (leaves)
- template
isEmptyTree
(T)
- Is true iff T is an empty tree.
- template
isLeaf
(T)
- Is true iff T is a leaf.
- Tuple!()
emptyTree
();
Tuple!(P)
leaf
(P)(P payload);
Tuple!(P,Ch)
tree
(P, Ch...)(P payload, Ch children);
- Convenience functions to create trees.
- Tr.Types[0]
payload
(Tr)(Tr tree);
- Returns a tree's
payload
.
- Tuple!(Tr.Types[1 .. __dollar])
children
(Tr)(Tr tr);
- Returns a tree's
children
. As D function cannot return naked tuples, the returned value is wrapped in a std.typecons.Tuple.
- template
mapTree
(alias fun)
- Maps a function
fun on a tree. fun will be applied on all payloads, so must be a polymorphic function.
mapTree
returns the transformed tree, which has the same shape than the original, but different values.
See_Also dranges.functional.extendFun to affect only some types and not the other ones, and dranges.tuple.mapTuple .
Note:
it's a greedy function, no lazy map here.
- template
reduceTree
(alias onLeaf,alias onBranch)
- Reduces a tree down to one value (which may very well be a complicated structure in itself, like another tree).
You must provide two polymorphic functions:
onLeaf , which is called on all leaves and onBranch which
is called on all non-leaf values.
|