Public
Edited
Oct 4, 2019
2 stars
Insert cell
Insert cell
data = [1,2,3,4,5]
Insert cell
Insert cell
data.map(x => x + 1);
Insert cell
Insert cell
data.filter(x => x > 2);
Insert cell
Insert cell
data
.map(x => x + 1)
.filter(x => x > 2);
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
reduce = (reducer, init, coll) => {
reducer = buildReducer(reducer);
if (coll === undefined) {
coll = init;
init = reducer.init();
}
if (init === undefined) {
init = reducer.init();
}
let result = init;
for (let x of coll) {
result = reducer.step(result, x);
}
return reducer.result(result);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
addMapToReducer = (fn, reducer) => {
return (xs, x) => reducer(xs, fn(x));
}
Insert cell
plusOneToArray = addMapToReducer(x => x + 1, arrayConcatReducer)
Insert cell
Insert cell
reduce(plusOneToArray, [], data)
Insert cell
reduce(addMapToReducer(x => x + 1, sumReducer), 0, data)
Insert cell
md`### Transducer`
Insert cell
Insert cell
mapping = mapFn => reducer => {
return (xs, x) => reducer(xs, mapFn(x));
}
Insert cell
mapping(x => x + 1)(arrayConcatReducer)([1,2],5)
Insert cell
Insert cell
addOneTransducer = mapping(x => x + 1)
Insert cell
reduce(addOneTransducer(sumReducer), 5, data)
Insert cell
reduce(addOneTransducer(arrayConcatReducer), [], data)
Insert cell
reduce(addOneTransducer(objRandomKeyReducer), {}, new Set([55,56]))
Insert cell
Insert cell
filtering = pred => reducer => {
return (xs, x) => pred(x) ? reducer(xs, x) : xs
}
Insert cell
gtTwoTransducer = filtering(x => x > 2)
Insert cell
reduce(gtTwoTransducer(arrayConcatReducer), [], data)
Insert cell
Insert cell
reduce(gtTwoTransducer(sumReducer), 0, range(5))
Insert cell
Insert cell
transduce = (transducer, reducer, init, coll) => {
reducer = transducer(reducer);
return reduce(reducer, init, coll)
}
Insert cell
transduce(addOneTransducer, sumReducer, 0, data)
Insert cell
Insert cell
Insert cell
{
let reducer =
filtering(x => x > 2)(
mapping(x => x + 1)(
arrayConcatReducer
)
);
return reduce(reducer, [], [1,2,3,4,5])
}

Insert cell
Insert cell
Insert cell
compose2 = (f, g) => (...args) => f(g(...args))
Insert cell
compose = (...fns) => reduce(compose2, fns.shift(), fns);
Insert cell
Insert cell
{
let reducer = compose(
filtering(x => x > 2),
mapping(x => x + 1)
)(arrayConcatReducer)
return reduce(reducer, [], [1,2,3,4,5])
}
Insert cell
transduce(compose(
filtering(x => x > 2),
mapping(x => x + 1)
), arrayConcatReducer, [], [1,2,3,4,5])
Insert cell
Insert cell
{
function filterThenMapThenToArrayReducer(xs, x) {
if (x > 2) {
x = x + 1;
xs.push(x);
return xs;
} else {
return xs;
}
}
return reduce(filterThenMapThenToArrayReducer, [], [1,2,3,4,5])
}
Insert cell
Insert cell
flatten = reducer => (xs, x) => reduce(reducer, xs, x)
Insert cell
flatMap = fn => {
return compose(mapping(fn), flatten)
}
Insert cell
bufferCount = bufferSize => reducer => {
let n = 0, els = [];
return (xs, x) => {
els[n++] = x;
if (n === bufferSize) {
const result = els;
els = [];
n = 0;
return reducer(xs, result);
} else {
return xs
}
}
}
Insert cell
reduce(bufferCount(3)(arrayConcatReducer),[],[1,2,3,4,5,6])
Insert cell
{
const string = "I *!#* love you";
const transducer = compose(
flatMap(str => str.split(' ')),
filtering(x => x !== "*!#*"),
mapping(x => x === "you" ? x + "tube" : x)
);
return transduce(transducer, arrayConcatReducer, [], [string]).join(' ');
}
Insert cell
transduce(compose(
mapping(x => x * 2),
filtering(x => x > 5),
mapping(x => x + 1)
), sumReducer, 0, [1,2,3,4,5])
Insert cell
dropping = n => reducer => {
return (xs, x) => {
let toDrop = n;
if (toDrop === 0)
return reducer(xs, x)
else
toDrop--;
}
}
Insert cell
t1 = filtering(x => x > 2)
Insert cell
t2 = filtering(x => x > 3)
Insert cell
t = mapping(x => x + 1)
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
splitting = (transducers) => (reducers) => {
const keys = Object.keys(transducers);
const reds = keys.reduce((obj, k) => (obj[k] = transducers[k](reducers[k]), obj), {});
const xss = {};
return (xs, x) => {
return keys.reduce((obj, k) => (obj[k] = reds[k](xs[k], x),obj), {})
}
}
Insert cell
{
return transduce(compose(t,splitting({foo:t1, bar: t2})), {foo:arrayConcatReducer, bar: arrayConcatReducer}, {foo: [], bar: [] }, [1,2,3,4,5])
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more