JSFiddle - React, Tailwind, and code Playground
JavaScript
function Reduced(wrapped) {
this._wrapped = wrapped;
}
Reduced.prototype.unwrap = function() {
return this._wrapped;
}
Reduced.isReduced = function(obj) {
return (obj instanceof Reduced);
}
function reduce(coll, fn, seed) {
var result = seed;
for (var i = 0; i < coll.length; i++) {
result = fn(result, coll[i]);
if (Reduced.isReduced(result)) {
return result.unwrap();
}
}
return result;
}
function transduce(transducer, append, coll) {
var step = transducer(append);
var seed = step();
var result = reduce(coll, step, seed);
return step(result);
}
function append(result, item) {
if (arguments.length === 2) {
return result.concat([item]);
}
if (arguments.length === 1) {
return result;
}
if (arguments.length === 0) {
return [];
}
}
function take(n) {
return function(step) {
var count = 0;
return function(result, item) {
if (arguments.length === 2) {
if (count++ < n) {
return step(result, item);
} else {
return new Reduced(result);
}
}
if (arguments.length === 1) {
return step(result);
}
if (arguments.length === 0) {
return step();
}
}
}
}
function partition(n) {
if (n < 1) {
throw new Error('n должен быть не меньше 1');
}
return function(step) {
var cur = [];
return function(result, item) {
if (arguments.length === 2) {
cur.push(item);
if (cur.length === n) {
result = step(result, cur);
cur = [];
return result;
} else {
return result;
}
}
if (arguments.length === 1) {
if (cur.length > 0) {
result = step(result, cur);
return step(result);
}
}
if (arguments.length === 0) {
return step();
}
}
}
}
// из underscore.js
function compose() {
var args = arguments;
var start = args.length - 1;
return function() {
...