Skip to content

Instantly share code, notes, and snippets.

@wegry
Last active June 10, 2018 17:50
Show Gist options
  • Save wegry/50d492cd2c24b3175bfb337e9893ea83 to your computer and use it in GitHub Desktop.
Save wegry/50d492cd2c24b3175bfb337e9893ea83 to your computer and use it in GitHub Desktop.
Toy example of reason codegen
function forEachU(a, f) {
for(var i = 0 ,i_finish = a.length - 1 | 0; i <= i_finish; ++i){
f(a[i]);
}
return /* () */0;
}
function forEach(a, f) {
return forEachU(a, Curry.__1(f));
}
/* export */
function __1(o) {
var arity = o.length;
if (arity === 1) {
return o;
} else {
return (function (a0) {
return _1(o, a0);
});
}
}
/* export */
function __(tag, block) {
block.tag = tag;
return block;
}
/* export */
// Generated by BUCKLESCRIPT VERSION 2.2.3, PLEASE EDIT WITH CARE
import * as Block from "stdlib/block";
import * Belt_Array from "stdlib/belt_Array";
function depthFirstTraversal(param) {
if (typeof param === "number") {
return /* array */[];
} else if (param.tag) {
var match = param[0];
return Belt_Array.concatMany(/* array */[
depthFirstTraversal(match[1]),
/* array */[match[0]],
depthFirstTraversal(match[2])
]);
} else {
return /* array */[param[0]];
}
}
var tree = /* Node */Block.__(1, [/* tuple */[
6,
/* Node */Block.__(1, [/* tuple */[
5,
/* Leaf */Block.__(0, [4]),
/* Empty */0
]]),
/* Node */Block.__(1, [/* tuple */[
8,
/* Empty */0,
/* Leaf */Block.__(0, [9])
]])
]]);
Belt_Array.forEach(depthFirstTraversal(tree), (function (prim) {
console.log(prim);
return /* () */0;
}));
export {
depthFirstTraversal ,
tree ,
}
/* Not a pure module */
open Belt;
type Tree('a) =
| Empty
| Leaf('a)
| Node('a, Tree('a), Tree('a));
let rec depthFirstTraversal: Tree => array('a) = (tree) => {
switch (tree) {
| Empty => [||]
| Leaf(x) => [|x|]
| Node(x, left, right) => [|
depthFirstTraversal(left),
[|x|],
depthFirstTraversal(right)
|] |. Array.concatMany
}
};
let tree = Node(6, Node(5, Leaf(4), Empty), Node(8, Empty, Node(9)));
tree |. depthFirstTraversal |. Js.log;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment