-
-
Save rightfold/07bd1b1822449c9fcee9 to your computer and use it in GitHub Desktop.
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
stringEq = | |
eq: (==) | |
stringOrd = | |
cmp: (x, y) -> | |
| x == y => 0 | |
| x < y => -1 | |
| x > y => +1 | |
stringOrd <<< stringEq | |
scala = !-> | |
class Set | |
(@ord) -> | |
@root = null | |
add: (key) -> | |
add = (node) ~> | |
return {left: null, key, right: null} if node == null | |
switch @ord.cmp(key, node.key) | |
| 0 => {node.left, key, node.right} | |
| -1 => {left: add(node.left), node.key, node.right} | |
| +1 => {node.left, node.key, right: add(node.right)} | |
new Set(@ord) | |
..root = add(@root) | |
set = new Set(stringOrd) | |
set = set.add('a') | |
set = set.add('b') | |
set = set.add('c') | |
set = set.add('a') | |
console.log(set) | |
ocaml = !-> | |
make = (ord) -> | |
class Set | |
-> | |
@root = null | |
add: (key) -> | |
add = (node) -> | |
return {left: null, key, right: null} if node == null | |
switch ord.cmp(key, node.key) | |
| 0 => {node.left, key, node.right} | |
| -1 => {left: add(node.left), node.key, node.right} | |
| +1 => {node.left, node.key, right: add(node.right)} | |
new Set() | |
..root = add(@root) | |
Set | |
Set = make(stringOrd) | |
set = new Set() | |
set = set.add('a') | |
set = set.add('b') | |
set = set.add('c') | |
set = set.add('a') | |
console.log(set) | |
scala! | |
ocaml! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment