Last active
December 26, 2015 22:39
-
-
Save dnadlinger/7224800 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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