function p(x) { console.log(x); }
function inc(x) { return x + 1; }
p([1, 2, 3].map(inc));
// [2, 3, 4]
Images from http://www.mathsisfun.com/sets/function.html
function factorial(n) {
return product(1, n);
}
function product(ns) {
var result = 1, start = 0, end = ns.length;
while(start < end) { product *= ns[start]; start += 1; }
}
var arr = [1, 2, 3, 4]
p(arr.splice(0, 2)); // [1, 2]
p(arr.splice(0, 2)); // [3, 4]
var label = "Hello {name}",
element = "#greeter";
function greet(user) {
$(element).html(label.format({ name: user }));
}
function memoize(f) {
var fTable = {};
return function() {
var input = JSON.stringify(arguments);
console.log(fTable);
if(!fTable[input]) {
fTable[input] = f.apply(f, arguments);
}
return fTable[input];
}
}
var add = memoize(function(x, y) { return x + y; })
console.log(add(1, 2));
console.log(add(1, 2));
//impure
var signUp = function(attrs) {
var user = saveUser(attrs);
welcomeUser(user);
};
//pure
var signUp = function(Db, Email, attrs) {
return function() {
var user = saveUser(Db, attrs);
welcomeUser(Email, user);
};
};
function add(a) {
return function(b) {
return a + b;
}
}
p(add(1)) // function
p(add(1)(2)) // 3
var inc = add(1)
p(inc(2)); // 3
function map(f) {
return function(xs) {
return xs.map(f);
}
}
var addOne = map(add(1));
p(addOne([1, 2, 3])); // [2, 3, 4]
var inc = add(1);
var inc = function(x) { add(1, x); }
function reduce(op) {
return function(initial) {
return function(xs) {
return xs.reduce(op, initial, xs);
}
}
}
var curry = require("ramda").curry;
var add = curry(function(a, b) {
return a + b;
});
var inc = add(1);
var match = curry(function(what, x) {
return x.match(what);
});
var replace = curry(function(what, replacement, x) {
return x.replace(what, replacement);
});
var curry = require("ramda").curry;
var filter = curry(function(f, xs) {
return xs.filter(f);
});
var map = curry(function(f, xs) {
return xs.map(f);
});
var reduce = curry(function(op, initial, xs) {
return xs.reduce(op, initial);
});
var split = curry(function(delimiter, xs) {
return xs.split(delimiter);
});
var join = curry(function(delimiter, xs) {
return xs.join(delimiter);
});
var words = split(" ");
p(words("Varna Conf 2015")) // ["Varna", "Conf", "2015"]
var unwords = join(" ");
p(join(["Varna", "Conf", "2015"])) // "Varna Conf 2015"
var sum = reduce(add, 0);
var product = reduce(mult, 1);
var slice = curry(function(start, end, xs) {
return xs.slice(start, end);
});
var take = slice(0);
function compose(f, g) {
return function(x) {
return f(g(x));
}
}
var exclaim = function(x) { return x + '!'; };
var shout = compose(exclaim, toUpperCase);
p(shout("varna baby")); // VARNA BABY!
//not pointfree because we mention the data: word
var snakeCase = function (word) {
return word.toLowerCase().replace(/\s+/ig, '_');
};
//pointfree
var snakeCase = compose(replace(/\s+/ig, '_'), toLowerCase);
//not pointfree because we mention the data: name
var initials = function (name) {
return name.split(' ').map(compose(toUpperCase, head)).join('. ');
};
//pointfree
var initials = compose(join('. '),
map(compose(toUpperCase, head)),
split(' '));
initials("hunter stockton thompson");
// 'H. S. T'
var app = compose(f1, f2, ..., fn);
app(initialData);
var head = function(xs) { return xs[0]; }
var last = compose(head, reverse);
p(head([])); // undefined
p(last([])); // undefined
if(!nullOrUndefined(arg1)) {
// do something
} else {
// stop the world!
}
if(!nullOrUndefined(arg2)) {
// do something
} else {
// stop the world!
}
function Container(x) {
this.__value = x;
}
Container.of = function(x) {
return new Container(x);
}
Container.prototype.map = function(f) {
return Container.of(f(this.__value));
}
var a = Container.of("VarnaConf");
p(a) // Container("VarnaConf")
var b = a.map(length);
p(b) // Container(9)
var a = Maybe.of(5);
p(a.map(inc)); // Maybe(6)
var b = Maybe.of(null);
p(b.map(inc)); // Maybe(null)
function Maybe(x) {
this.__value = x;
}
Maybe.of = function(x) { return new Maybe(x); }
Maybe.prototype.isNothing = function() {
return nullOrUndefined(this.__value);
}
Maybe.prototype.map = function(f) {
if(this.isNothing()) {
return this;
}
return Maybe.of(f(this.__value));
}
var map = curry(function(f, functor) {
return functor.map(f);
});
var n = Maybe.of(10);
var result = compose(map(inc), map(inc));
var result2 = map(compose(inc, inc));
console.log(result(n)); // 12
console.log(result2(n)); // 12
// identity
map(id) === id;
// composition
compose(map(f), map(g)) === map(compose(f, g));