Editing reducibles This edit will create a new revision. Your details (optional) Name Email (won’t be displayed; might be used for Gravatar) URL Test case details Title * Published (uncheck if you want to fiddle around before making the page public) Description (in case you feel further explanation is needed)(Markdown syntax is allowed) Comparing clojure like reducers to native implementations Are you a spammer? (just answer the question) Preparation code Preparation code HTML (this will be inserted in the <body> of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) Include JavaScript libraries as follows: <script src="//cdn.ext/library.js"></script> Define setup for all tests (variables, functions, arrays or other objects that will be used in the tests) (runs before each clocked test loop, outside of the timed code region) (e.g. define local test variables, reset global variables, clear canvas, etc.) (see FAQ) function increment(x) { return x + 1 } function isOdd(n) { return n % 2 } function sum(x, y) { return (x || 0) + (y || 0) } function reduced(value) { return Object.create(reduced.prototype, { value: { value: value } }) } reduced.is = function is(value) { return value && value.constructor === reduced } function reducible(reduce) { return { reduce: reduce } } function reducer(process) { return function(f, reducible) { return { reduce: function(next, start) { return reducible.reduce(function(result, item) { var value = process(f, item, reduced) var ended = false //reduced.is(value) value = ended ? value.value : value result = value === undefined ? result : next(result, value) return ended ? reduced(result) : result }, start) } } } } function reduce(f, reducible, start) { return reducible.reduce(f, start) } var filter = reducer(function(f, item) { if (f(item)) return item }) var map = reducer(function(f, item) { return f(item) }) function into(reducible, target) { // reduce reducible and colelect it's // items into array which we return after. return reduce(function(result, item) { result.push(item) return result }, reducible, target || []) } function range(from, to) { var values = [] while (from < to) values.push(from++) return values } Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code range(1, 100).map(increment).filter(isOdd).reduce(sum); Test 2 Title Async (check if this is an asynchronous test) Code reduce(sum, filter(isOdd, map(increment, range(1, 100))));