Skip to content

Instantly share code, notes, and snippets.

@dnadlinger
Last active December 26, 2015 22:39
Show Gist options
  • Select an option

  • Save dnadlinger/7224800 to your computer and use it in GitHub Desktop.

Select an option

Save dnadlinger/7224800 to your computer and use it in GitHub Desktop.
struct Node(T) {
T val;
Node!T[] children;
}
import std.range;
ForwardRange!T flatten(T)(Node!T root) {
import std.algorithm;
return only(root.val).chain(map!flatten(root.children).joiner).inputRangeObject;
}
void main() {
alias N = Node!int;
auto a = N(1, [
N(2, [
N(3, [
N(4)
])
]),
N(5)
]);
import std.stdio;
writeln(a.flatten);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment