Published
Edited
Apr 28, 2021
1 star
Insert cell
Insert cell
Insert cell
function pick(obj, keys) {
let result = {};
for(const key of keys) {
result[key] = obj[key];
}
return result;
}
Insert cell
Insert cell
{
const user = {
id: 7,
name: "Tom",
lastname: "Keen",
email: "noreply@example.com",
password: "hudson"
};
return pick(user, ['name', 'lastname']);
}
Insert cell
Insert cell
users = [
{
id: 7,
name: "Tom",
lastname: "Keen",
email: "noreply@example.com",
password: "hudson"
},
{
id: 30,
name: "Smokey",
lastname: "Putnum",
email: "noreply@example.com",
password: "carnival"
},
{
id: 69,
name: "Lady",
lastname: "Luck",
email: "noreply@example.com",
password: "norestforthewicked"
}
];
Insert cell
Insert cell
str(users.map(function(user) {
return pick(user, ['name', 'lastname']);
}));
Insert cell
Insert cell
function public_info(user) {
return pick(user, ['name', 'lastname']);
}
Insert cell
str(users.map(public_info));
Insert cell
Insert cell
// This isn't the real `fetch`.

Fetch('/users').then(function(users) {
return users.map(function(user) {
return pick(user, ['name', 'lastname']);
})
});
Insert cell
Insert cell
Fetch('/users').then(users => users.map(user => pick(user, ['name', 'lastname'])));
Insert cell
Insert cell
Fetch('/users').then(users => users.map(public_info));
Insert cell
Insert cell
function user_list(users) {
return users.map(public_info);
}
Insert cell
Insert cell
Fetch('/users').then(user_list);
Insert cell
Insert cell
{
const public_info = pickk(['name', 'lastname']);
const user_list = map(public_info);

return Fetch('/users').then(user_list);
}
Insert cell
Insert cell
Fetch('/users').then(map(pickk(['name', 'lastname'])));
Insert cell
Insert cell
Insert cell
function take(count, data) {
return data.slice(0, count);
}
Insert cell
Insert cell
take(2, ['first', 'second', 'rest']);
Insert cell
Insert cell
{
const first_two = ttake(2);
return first_two(['first', 'second', 'rest']);
}
Insert cell
Insert cell
{
function filter(func, data) {
return data.filter(func);
}
return filter(Boolean, [true, '', null, 'that']);
}
Insert cell
Insert cell
{
const exclude_falsey = filter(Boolean);

return exclude_falsey([true, '', null, 'that']);
}
Insert cell
Insert cell
Insert cell
Insert cell
{
function filter(func, data) {
return data.filter(func);
}
const exclude_falsey = filter.bind(null, Boolean);

return exclude_falsey([true, '', null, 'that']);
}
Insert cell
Insert cell
{
function filter(func, data) {
// This is it. We are counting.
if(arguments.length === 1) {
// if .length is 1 that means we got `func`
// it also means we don't have `data`
// so we return another function that
// remembers `func` and waits for `data`
return arg => filter(func, arg);
}

return data.filter(func);
}
// Now it is possible to do this.
const exclude_falsey = filter(Boolean);
const result_1 = exclude_falsey([true, '', null, 'that']);
// And also.
const result_2 = filter(Boolean, [true, '', null, 'that']);

return str([result_1, result_2]);
}
Insert cell
Insert cell
Insert cell
function bind(func, ...first_args) {
return (...rest) => func(...first_args, ...rest);
}
Insert cell
Insert cell
{
function filter(func, data) {
return data.filter(func);
}
const exclude_falsey = bind(filter, Boolean);

return exclude_falsey([true, '', null, 'that']);
}
Insert cell
Insert cell
Insert cell
function filter(func) {
return function(data) {
return data.filter(func, data);
}
}
Insert cell
Insert cell
Insert cell
{
const exclude_falsey = filter(Boolean);

return exclude_falsey([true, '', null, 'that']);
}
Insert cell
Insert cell
filter (Boolean) ([true, '', null, 'that']);
Insert cell
Insert cell
Insert cell
function curry(fn, arity, ...rest) {
if (arguments.length === 1) {
// Guess how many arguments
// the function needs.
// This doesn't always work.
arity = fn.length;
}

// Do we have what we need?
if (arity <= rest.length) {
return fn(...rest);
}

// Execute `curry.bind` with `fn`, `arity` and `rest` as arguments
// it will return a function waiting for more arguments
return curry.bind(null, fn, arity, ...rest);
}
Insert cell
Insert cell
{
function filter(func, data) {
return data.filter(func);
}
const curried_filter = curry(filter);

const exclude_falsey = curried_filter(Boolean);

return exclude_falsey([true, '', null, 'that']);
}
Insert cell
Insert cell
{
const filter = curry(function(func, data) {
return data.filter(func);
});
const exclude_falsey = filter(Boolean);

return exclude_falsey([true, '', null, 'that']);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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