Skip to content

Instantly share code, notes, and snippets.

@rightfold

rightfold/.ls Secret

Last active February 15, 2016 10:57
Show Gist options
  • Save rightfold/07bd1b1822449c9fcee9 to your computer and use it in GitHub Desktop.
Save rightfold/07bd1b1822449c9fcee9 to your computer and use it in GitHub Desktop.
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