Created
June 10, 2018 18:48
-
-
Save wegry/0eb7d31d4dda7a5971296c336c083b4b to your computer and use it in GitHub Desktop.
Purescript binary tree codegen
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
| module Main where | |
| import Prelude | |
| data Tree a = Empty | Leaf a | Node a (Tree a) (Tree a) | |
| depthFirstTraversal x = | |
| case x of | |
| Empty -> [] | |
| Leaf a -> [a] | |
| Node a left right -> depthFirstTraversal left <> [a] <> depthFirstTraversal right |
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
| "use strict"; | |
| var Data_Semigroup = require("../Data.Semigroup"); | |
| var Prelude = require("../Prelude"); | |
| var Empty = (function () { | |
| function Empty() { | |
| }; | |
| Empty.value = new Empty(); | |
| return Empty; | |
| })(); | |
| var Leaf = (function () { | |
| function Leaf(value0) { | |
| this.value0 = value0; | |
| }; | |
| Leaf.create = function (value0) { | |
| return new Leaf(value0); | |
| }; | |
| return Leaf; | |
| })(); | |
| var Node = (function () { | |
| function Node(value0, value1, value2) { | |
| this.value0 = value0; | |
| this.value1 = value1; | |
| this.value2 = value2; | |
| }; | |
| Node.create = function (value0) { | |
| return function (value1) { | |
| return function (value2) { | |
| return new Node(value0, value1, value2); | |
| }; | |
| }; | |
| }; | |
| return Node; | |
| })(); | |
| var depthFirstTraversal = function (x) { | |
| if (x instanceof Empty) { | |
| return [ ]; | |
| }; | |
| if (x instanceof Leaf) { | |
| return [ x.value0 ]; | |
| }; | |
| if (x instanceof Node) { | |
| return Data_Semigroup.append(Data_Semigroup.semigroupArray)(depthFirstTraversal(x.value1))(Data_Semigroup.append(Data_Semigroup.semigroupArray)([ x.value0 ])(depthFirstTraversal(x.value2))); | |
| }; | |
| throw new Error("Failed pattern match at Main line 8, column 3 - line 11, column 81: " + [ x.constructor.name ]); | |
| }; | |
| module.exports = { | |
| Empty: Empty, | |
| Leaf: Leaf, | |
| Node: Node, | |
| depthFirstTraversal: depthFirstTraversal | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment