Giter Club home page Giter Club logo

proposal-array-prototype-partition's Introduction

Array.prototype.partition

A proposal for an utility function for splitting an Array using a predicate. You can find an initial implementation of a polyfill here

Usage:

const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
const isEven = (n) => n % 2 === 0;

const [even, odd] = numbers.partition(n => isEven(n));

Why not use .filter or .reduce?

While .filter is a helpful way of eliminating unwanted items from your collection by filtering them out, wanting to simply remove the items is not always the case. The .reduce function can serve the purpose of manipulating the items, but it doesn't cover easily the need of having multiple arrays built for you depending on conditions you apply to the items.

Current State

Using .filter

const numbers = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,15]

const isEven = n => n % 2 === 0

const even = numbers.filter(n => isEven(n))
const odd = numbers.filter(n => !isEven(n)) 

Using .reduce

const numbers = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,15]

const isEven = n => n % 2 === 0

const [even, odd] = numbers.reduce(
  ([even, odd], num) => 
    isEven(num) ? [even.concat(num), odd]: [even, odd.concat(num)],
  [[], []]
)

Using for

const numbers = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,15]

const isEven = n => n % 2 === 0

let even = []
let odd = []

for (const number of numbers) {
  isEven(number) ? even.push(number) : odd.push(number)
}

Prior art

Other Languages

Javascript Ecosystem

Draft Spec

Array.prototype.partition ( callbackfn [ , thisArg ] )

NOTE - callbackfn should be a function that accepts three arguments and returns a value that is coercible to the Boolean value true or false. partition calls callbackfn once for each element in the array, in ascending order, and constructs two new arrays: one of all the values for which callbackfn returns true and another of all the values for which callbackfn returns false. callbackfn is called only for elements of the array which actually exist; it is not called for missing elements of the array.

If a thisArg parameter is provided, it will be used as the this value for each invocation of callbackfn. If it is not provided, undefined is used instead.

callbackfn is called with three arguments: the value of the element, the index of the element, and the object being traversed.

partition does not directly mutate the object on which it is called but the object may be mutated by the calls to callbackfn.

The range of elements processed by partition is set before the first call to callbackfn. Elements which are appended to the array after the call to partition begins will not be visited by callbackfn. If existing elements of the array are changed their value as passed to callbackfn will be the value at the time partition visits them; elements that are deleted after the call to partition begins and before being visited are not visited.

When the partition method is called with one or two arguments, the following steps are taken:

1. Let `O` be ? `ToObject(this value)`.
2. Let `len` be ? `LengthOfArrayLike(O)`.
3. If `IsCallable(callbackfn)` is `false`, throw a `TypeError` exception.
4. Let `A` be ? ArraySpeciesCreate(O, 0).
5. Let `B` be ? ArraySpeciesCreate(O, 0).
6. Let `Tuple` be ? ArrayCreate(2).
7. Let k be 0.
8. Let `toA` be 0.
9. Let `toB` be 0.
10. Repeat while `k < len`: 
    a. Let `Pk` be ! ToString(k).
    b. Let `kPresent` be ? HasProperty(O, Pk).
    c. If `kPresent` is `true`, then
        i. Let `kValue` be ? `Get(O, Pk)`.
        ii. Let `selected` be ! `ToBoolean(? Call(callbackfn, thisArg, « kValue, k, O »))`.
        iii. If `selected` is `true`, then
            1. Perform ? `CreateDataPropertyOrThrow(A, ! ToString(toA), kValue)`.
            2. Set `toA` to `toA + 1`.
        iv. Else,
            1. Perform ? `CreateDataPropertyOrThrow(B, ! ToString(toB), kValue)`.
            2. Set `toB` to `toB + 1`.
    d. Set `k` to `k + 1`
11. Perform ? `CreateDataPropertyOrThrow(Tuple, ! ToString(0), A)
12. Perform ? `CreateDataPropertyOrThrow(Tuple, ! ToString(1), B)
13. Return `Tuple`

proposal-array-prototype-partition's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

evaporei

proposal-array-prototype-partition's Issues

Consider `groupBy` generic operation

It's a bit more generic operation "partition"

let groupBy = (f, coll) => {
  let rf = (acc, el) => {
    let id = f(el);
    acc[id] = [... (acc[id] || []), el]
    return acc
  }
  return coll.reduce(rf , {})
}

Can be used as partition as:

let {false: f, true: t} = groupBy( x => x == 2 , [1,2,3])

Maybe should we accumulate over an actual Map allowing f return any object (?!)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.