Skip to content

Instantly share code, notes, and snippets.

@groovelab
Last active August 29, 2015 14:16
Show Gist options
  • Save groovelab/9d6c77186d91aea3512a to your computer and use it in GitHub Desktop.
Save groovelab/9d6c77186d91aea3512a to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<meta charset="utf8">
<title>JavaScriptで学ぶ関数型プログラミング テストページ</title>
</head>
<body>
<h1>JavaScriptで学ぶ関数型プログラミングのテストページ</h1>
<script type="text/javascript" src="http://underscorejs.org/underscore.js"></script>
<script type="text/javascript">
// ここにコードを記述する
</script>
</body>
</html>
var ret = [];
for (var i=0; i<10; i++) {
var result = i * i;
ret.push(result);
}
console.log(ret); // [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
var squareUntil = function(limit) {
return _.map(_.range(limit), function(n) {
return n * n;
});
}
console.log(squareUntil(10)); // [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
function curry2(fun) {
return function(secondArg) {
return function(firstArg) {
return fun(firstArg, secondArg);
};
};
}
function div(n, d) {
return n / d;
}
var div10 = curry2(div)(10);
console.log(div10(20)); // 2
function curry3(fun) {
return function(last) {
return function(middle) {
return function(first) {
return fun(first, middle, last);
};
};
};
}
function toHex(n) {
var hex = n.toString(16);
return (hex.length < 2) ? [0, hex].join('') : hex;
}
console.log(toHex(15)); // 0f
console.log(toHex(255)); // ff
function rgbToHexString(r, g, b) {
return ['#', toHex(r), toHex(g), toHex(b)].join('');
}
console.log(rgbToHexString(255, 255, 255)); // #ffffff
var blueGreen = curry3(rgbToHexString)(255)(200);
console.log(blueGreen(1)); // #01c8ff
function partial1(fun, arg1) {
return function(/* args */) {
var args = [arg1].concat(_.toArray(arguments));
return fun.apply(null, args);
};
}
function div(n, d) {
return n / d;
}
var over10Part1 = partial1(div, 10);
console.log(over10Part1(5)); // 2
function partial2(fun, arg1, arg2) {
return function(/* args */) {
var args = [arg1].concat([arg2], _.toArray(arguments));
return fun.apply(null, args);
}
}
function toHex(n) {
var hex = n.toString(16);
return (hex.length < 2) ? [0, hex].join('') : hex;
}
console.log(toHex(15)); // 0f
console.log(toHex(255)); // ff
function rgbToHexString(r, g, b) {
return ['#', toHex(r), toHex(g), toHex(b)].join('');
}
console.log(rgbToHexString(255, 255, 255)); // #ffffff
var redGreen = partial2(rgbToHexString, 1, 200);
console.log(redGreen(255)); // #01c8ff
var func = function(){
return 1;
};
[0, function(){return 1;}]
var a = {num: 1, fun: function(){return 2;}}
1 + (function(){return 1;})()
somefunc(1, function(){return 1;});
function func() {
return function(){
return 1;
};
}
function add(addValue, validator) {
return function(n) {
if (!validator(n)) {
return NaN;
}
return n + addValue;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment