Published
Edited
Jun 10, 2021
1 fork
3 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const array = [1, 2, 3, 4];
const callback = (state, value) => {
if(state == null) {
return value;
}

return state + value;
};

const result = reduce_v1(array, callback);
return md`[${array.join(', ')}] => ${result}`;
}
Insert cell
Insert cell
Insert cell
Insert cell
{
const array = [1, 2, 3, 4];
const callback = (state, value) => {
return state + value;
};

const result = reduce_v2(array, callback);
return md`[${array.join(', ')}] => ${result}`;
}
Insert cell
Insert cell
Insert cell
Insert cell
function max(number, another_one) {
if(number > another_one) {
return number;
} else {
return another_one;
}
}
Insert cell
Insert cell
{
const array = [40, 41, 42, 39, 38];

// 40 > 41 > 42 > 39 > 38
const result = reduce_v2(array, max);
return md`[${array.join(', ')}] => ${result}`
}
Insert cell
Insert cell
{
const max_1 = max(max(40, 42), 41);
const max_2 = max(40, max(42, 41));
return [max_1, max_2];
}
Insert cell
Insert cell
function concat(one, another) {
return one.concat(another);
}
Insert cell
Insert cell
{
const to_str = arr => `[${arr.join(', ')}]`;
const array = [[40, 41], [42], [39, 38]];

// [40, 41] + [42] + [39, 38]
const result = reduce_v2(array, concat);
return md`${to_str(array.map(to_str))} => ${to_str(result)}`;
}
Insert cell
Insert cell
function union(one, another) {
const set = new Set([...one, ...another]);
return Array.from(set);
}
Insert cell
Insert cell
{
const union_1 = union(union([40, 41], [40, 41, 42]), [39]);
const union_2 = union([40, 41], union([40, 41, 42], [39]));

return [union_1.join(', '), union_2.join(', ')];
}
Insert cell
Insert cell
{
const array = [
['hello'],
['hello', 'awesome'],
['world', '!'],
['!!', 'world']
];

return reduce_v2(array, union);
}
Insert cell
Insert cell
test_array_1 = [40, 41, [42], [39, 38]];
Insert cell
Insert cell
{
try {
return reduce_v2(test_array_1, concat);
} catch(e) {
return md`**${e.name}**: ${e.message}`;
}
}
Insert cell
Insert cell
Insert cell
Insert cell
reduce_v3(test_array_1, [], concat);
Insert cell
Insert cell
{
const concat_1 = concat([], ['hello']);
const concat_2 = concat(['hello'], []);

return [concat_1.join(', '), concat_2.join(', ')];
}
Insert cell
{
const union_1 = union([], ['hello']);
const union_2 = union(['hello'], []);

return [union_1.join(', '), union_2.join(', ')];
}
Insert cell
Insert cell
test_array_2 = [
{name: 'Harold'},
{lastname: 'Cooper'},
{state: 'wrong'}
];
Insert cell
Insert cell
reduce_v3(test_array_2, (state, value) => Object.assign(state, value));
Insert cell
Insert cell
test_array_2[0];
Insert cell
Insert cell
reduce_v3(test_array_3, {}, (state, value) => Object.assign(state, value));
Insert cell
test_array_3 = [
{name: 'Harold'},
{lastname: 'Cooper'},
{state: 'wrong'}
];
Insert cell
test_array_3[0];
Insert cell
Insert cell
Insert cell
Insert cell
list = Stream(['node', 'js']);
Insert cell
state = Stream.scan(union, [], list);
Insert cell
{
list(['node']);
list(['js', 'deno']);
list(['node', 'javascript']);

return state();
}
Insert cell
Insert cell
Insert cell
Insert cell
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