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 inc = add(1);
var a = Container.of(5);
var b = a.map(b);
console.log(b.__value); // 6
var today = Container.of(["Beer", "JS"]);
today = today.map(concat).map(capitalize);
console.log(today.__value); // "BEERJS"
var map = ramda.curry(function(f, xs) {
return xs.map(f);
});
var compose = ramda.compose;
var a = Container.of(5);
var b = map(inc, a);
console.log(b.__value); // 6
var today = Container.of(["Beer", "JS"]);
today = compose(map(capitalize), map(concat))(today);
console.log(today.__value); // "BEERJS"
map :: Function -> Container a -> Container b
capitalize :: String -> String
// a :: Container String
var a = Container.of("beer");
// b :: Container String
var b = map(capitalize, a); // Container.of("BEER")ч
compose(map(capitalize), map(concat))
==
map(compose(capitalize, concat))
var a = Maybe.of(5);
map(inc, a); // Maybe(6)
var b = Maybe.of(null);
console.log(map(inc, b)); // Maybe(null)
var Maybe = function(x) {
this.__value = x;
}
Maybe.of = function(x) {
return new Maybe(x);
}
Maybe.prototype.isNothing = function() {
return (this.__value === null ||
this.__value === undefined);
}
Maybe.prototype.map = function(f) {
return this.isNothing() ?
Maybe.of(null) : Maybe.of(f(this.__value));
}
var jsify = function(str) { return str + ".js" }
var beerify = function(str) { return "Beer" + str }
var a = Maybe.of("");
compose(map(capitalize),
map(beerify),
map(jsify)) (a);
Maybe.of("BEER.JS");
var safeHead = function(xs) {
return Maybe.of(xs[0]);
};
var a = [1, 2, 3];
console.log(compose(inc, safeHead)(a));
// Maybe.of(2)
var safeHead = function(xs) {
return Maybe.of(xs[0]);
};
var b = [];
console.log(compose(inc, safeHead)(b));
// Maybe.of(null)
getDom :: String -> Maybe DOMElement
addText :: String -> DOMElement -> DOMElement
var element1 = getDom("Something missing");
var element2 = getDom("#someId");
map(addText("Beer.JS!"), element1);
// Maybe.of(null);
map(addText("Beer.JS!"), element2);
// Maybe.of(DOMElement("Beer.JS!"))
var nullCreator = function() { return null; }
var a = Maybe.of("");
compose(map(capitalize),
map(nullCreator),
map(beerify),
map(jsify))(a);
Maybe.of(null);
data Maybe a = Just a | Nothing
safeHead :: [a] -> Maybe a
safeHead [] = Nothing
safeHead (x:xs) = Just x