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
// To play around with this code, copy and paste the contents of this gist | |
// into the javascript pane of http://jsbin.com/umoK/1/edit or go to | |
// http://jsbin.com/uZAfIqA/2/edit | |
// ============================================================================= | |
// Generic API | |
// ============================================================================= | |
//+ words :: string -> array | |
var words = split(' '); |
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
function treeNode(value, left, right) { | |
return { | |
value: value, | |
left: left, | |
right: right, | |
map: function(transformation) { | |
var nextValue = transformation(nextValue); | |
var nextLeft; | |
if (left) nextLeft = left.map(transformation); |
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
export type Tree<T> = Leaf<T> | TreeNodeArray<T> | TreeNodeObject<T>; | |
type GenericObject<T> = { [key: string]: T }; | |
type ChildrenArray<T> = Tree<T>[]; | |
type ChildrenObject<T> = GenericObject<Tree<T>> | |
function mapObj<T, U>(fn: ((value: T, key: string) => U), obj: GenericObject<T>): GenericObject<U> { | |
let ret = {}; |
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 { find, identity, toPairs, compose, map, curry } = require('ramda'); | |
/* tslint:disable: no-any */ | |
type AnyType = any; | |
/* tslint:enable: no-any */ | |
type TComparator = (a: AnyType, b: AnyType) => number; | |
type TObjectComparator = { | |
[key: string]: TComparator; |
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
function addOneAndMultiplyTwo(x) { return multiplyTwo(addOne(x)); } | |
[1, 2, 3].map(addOneAndMultiplyTwo); | |
listNode(1, listNode(2, listNode(3))).map(addOneAndMultiplyTwo); | |
treeNode(1, treeNode(2), treeNode(3)).map(addOneAndMultiplyTwo); |
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
function map(transformation, data) { | |
return data.map(transformation); | |
} | |
map(addOne, [1, 2, 3]); | |
map(addOne, listNode(1, listNode(2, listNode(3)))); | |
map(addOne, treeNode(1, treeNode(2), treeNode(3))); |
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
[1, 2, 3].map(identity); | |
// [1, 2, 3] | |
listNode(1, listNode(2, listNode(3))).map(identity); | |
// listNode(1, listNode(2, listNode(3))) | |
treeNode(1, treeNode(2), treeNode(3)).map(identity); | |
// treeNode(1, treeNode(2), treeNode(3)) |
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
function identity(x) { return x; } |
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
var tree = treeNode(1, treeNode(2), treeNode(3)); | |
tree.map(function(x) { | |
return x + 1; | |
}); | |
// treeNode(2, treeNode(3), treeNode(4)) |
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
function treeNode(value, left, right) { | |
return { | |
value: value, | |
left: left, | |
right: right | |
}; | |
} | |
var myTree = treeNode(1, treeNode(2), treeNode(3)); |