made with esnextbin
Last active
August 26, 2016 21:56
-
-
Save xgrommx/eaadc9f5be7fcaa6e4f59010277576c5 to your computer and use it in GitHub Desktop.
esnextbin sketch
This file contains 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
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>ESNextbin Sketch</title> | |
<!-- put additional styles and scripts here --> | |
</head> | |
<body> | |
<!-- put markup and other contents here --> | |
</body> | |
</html> |
This file contains 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
const Type = require('union-type'); | |
const T = () => true; | |
const Tree = Type({ | |
Leaf: [T], | |
Node: [Tree, Tree] | |
}); | |
const {Leaf, Node} = Tree; | |
const tree = Node (Node (Leaf (1), Node (Leaf (2), Leaf (3))), Node (Leaf (2), Leaf (3))) | |
const map = f => Tree.case({ | |
Leaf(v) { | |
return Leaf(f(v)); | |
}, | |
Node(l, r) { | |
return Node(map(f)(l), map(f)(r)); | |
} | |
}); | |
const toString = Tree.case({ | |
Leaf(v) { | |
return `Leaf (${v})`; | |
}, | |
Node(l, r) { | |
return `Node(${toString(l)}, ${toString(r)})` | |
} | |
}); | |
const height = Tree.case({ | |
Leaf(_) { | |
return 0; | |
}, | |
Node(l, r) { | |
return 1 + Math.max(height(l), height(r)); | |
} | |
}); | |
const size = Tree.case({ | |
Leaf(_) { | |
return 1; | |
}, | |
Node(l, r) { | |
return 1 + size(l) + size(r); | |
} | |
}); | |
console.log(toString(tree), 'show'); | |
console.log(height(tree), 'height'); | |
console.log(size(tree), 'size'); | |
console.log(toString(map(x => x * 10)(tree)), 'map'); |
This file contains 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
{ | |
"name": "esnextbin-sketch", | |
"version": "0.0.0", | |
"dependencies": { | |
"union-type": "0.3.1" | |
} | |
} |
This file contains 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 Type = require('union-type'); | |
var T = function T() { | |
return true; | |
}; | |
var Tree = Type({ | |
Leaf: [T], | |
Node: [Tree, Tree] | |
}); | |
var _Leaf = Tree.Leaf; | |
var _Node = Tree.Node; | |
var tree = _Node(_Node(_Leaf(1), _Node(_Leaf(2), _Leaf(3))), _Node(_Leaf(2), _Leaf(3))); | |
var map = function map(f) { | |
return Tree.case({ | |
Leaf: function Leaf(v) { | |
return _Leaf(f(v)); | |
}, | |
Node: function Node(l, r) { | |
return _Node(map(f)(l), map(f)(r)); | |
} | |
}); | |
}; | |
var toString = Tree.case({ | |
Leaf: function Leaf(v) { | |
return 'Leaf (' + v + ')'; | |
}, | |
Node: function Node(l, r) { | |
return 'Node(' + toString(l) + ', ' + toString(r) + ')'; | |
} | |
}); | |
var height = Tree.case({ | |
Leaf: function Leaf(_) { | |
return 0; | |
}, | |
Node: function Node(l, r) { | |
return 1 + Math.max(height(l), height(r)); | |
} | |
}); | |
var size = Tree.case({ | |
Leaf: function Leaf(_) { | |
return 1; | |
}, | |
Node: function Node(l, r) { | |
return 1 + size(l) + size(r); | |
} | |
}); | |
console.log(toString(tree), 'show'); | |
console.log(height(tree), 'height'); | |
console.log(size(tree), 'size'); | |
console.log(toString(map(function (x) { | |
return x * 10; | |
})(tree)), 'map'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment