30-seconds-of-code: Simplification of the code

Created on 12 Apr 2019  路  7Comments  路  Source: 30-seconds/30-seconds-of-code

Using rest/spread we ca simplify some examples even more

const over = (...fns) => (...args) => fns.map(fn => fn.apply(null, args));
const over = (...fns) => (...args) => fns.map(fn => fn( ...args));

Number functions converts boolean to 1 or 0, however it would work if undefined is used as a negative ...

const bifurcateBy = (arr, fn) =>
  arr.reduce((acc, val, i) => (acc[fn(val, i) ? 0 : 1].push(val), acc), [[], []]);

const bifurcateBy = (arr, fn) =>
  arr.reduce((acc, val, i) => (acc[Number(fn(val, i))].push(val), acc), [[], []]);

[].flat (Bonus: it respects Symbol.species)

const deepFlatten = arr => [].concat(...arr.map(v => (Array.isArray(v) ? deepFlatten(v) : v)));
const deepFlatten = arr => arr.flat(Infinity);
// also flatten

Most helpful comment

We've discussed a few of these. As it stand we need to implement them. If you'd like to open a PR modifying or updating a snippet, feel free to

All 7 comments

objectFromPairs --> Object.fromEntries
objectToPairs --> Object.entries

We've discussed a few of these. As it stand we need to implement them. If you'd like to open a PR modifying or updating a snippet, feel free to

@GrosSacASac would you like to update these or want us from 30 core team to do it by ourselves?

Looks like a straightforward process by following our contributing docs.

it is easier if you do it

Alright. We'll do it. Thanks for mentioning it! 馃憤

Closing due to inactivity. objectToPairs is discussed under #947, objectFromPairs will be implemented in a similar fashion under the same issue and PRs.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for any follow-up tasks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Greenheart picture Greenheart  路  3Comments

ecwyne picture ecwyne  路  4Comments

GrosSacASac picture GrosSacASac  路  3Comments

larrybotha picture larrybotha  路  3Comments

fejes713 picture fejes713  路  4Comments