Skip to content

Instantly share code, notes, and snippets.

Created November 14, 2011 01:53
Show Gist options
  • Select an option

  • Save anonymous/1363062 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/1363062 to your computer and use it in GitHub Desktop.
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css" type="text/css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://code.jquery.com/qunit/git/qunit.js"></script>
</head>
<body>
<h1 id="qunit-header">
Functional Programming Samples</h1>
<h2 id="qunit-banner">
</h2>
<div id="qunit-testrunner-toolbar">
</div>
<h2 id="qunit-userAgent">
</h2>
<ol id="qunit-tests">
</ol>
<div id="qunit-fixture">
</div>
<script type="text/javascript">
/* map and reduce EcmaScript3 implementation */
var map = Array.prototype.map
?
function (a, fn) { return a.map(fn); }
:
function (a, fn) {
var result = [];
for (var i = 0; i < a.length; i++) {
if (i in a) result[i] = fn.call(null, a[i], i, a);
}
return result;
};
var reduce = Array.prototype.reduce
?
function (a, fn, initial) {
if (arguments.length > 2)
return a.reduce(fn, initial);
else
return a.reduce(fn);
}
:
function (a, fn, initial) {
var i = 0;
var l = a.length;
var accum;
if (arguments.length > 2)
accum = initial;
else {
if (l == 0) throw TypeError();
while (i < l) {
if (i in a) {
accum = a[i++];
break;
}
else
i++;
}
if (i == l) throw TypeError();
}
while (i < l) {
if (i in a)
accum = fn.call(undefined, accum, a[i], i, a);
i++
}
return accum;
};
/* higher-order functions */
var isEven = function (input) {
return input % 2 === 0;
}
var not = function (f) {
return function () {
return !f.apply(this, arguments);
};
}
var isOdd = not(isEven);
/* */
var isPositive = function (input) {
return input > 0;
};
var isZero = function (input) { return input === 0; };
var and = function (f, g) {
return function () {
return f.apply(this, arguments) &&
g.apply(this, arguments);
};
};
var isNegative = and(not(isPositive), not(isZero));
/* */
var square = function (input) { return input * input };
var sum = function (a, b) { return a + b };
var compose = function (f, g) {
return function () {
return f.call(this, g.apply(this, arguments));
};
};
var squareOfSum = compose(square, sum);
/* */
var mapper = function (f) {
return function (a) { return map(a, f); };
};
var incrementer = mapper(function (input) { return input + 1 });
/* tests */
$(function () {
module("Higher-order functions sample");
test("isOdd/isEven", function () {
ok(!isOdd(2), "isOdd, passing 2, returns false.");
ok(isEven(2), "isEven, passing 2, returns true.");
});
test("isNegative/isZero/isPositive", function () {
ok(isNegative(-1), "isNegative, passing -1, returns true.");
ok(!isNegative(0), "isNegative, passing 0, returns false.");
ok(!isNegative(1), "isNegative, passing 1, returns false.");
});
test("composing", function () {
equals(squareOfSum(6, 3), 81, "squareOfSum, passing 6 and 3, returns 81.");
});
test("mapper", function () {
ok(incrementer([4, 5, 6]).toString() == [5, 6, 7], "incrementer, passing [4,5,6], returns [5,6,7]");
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment