How to create a partition function in javascript.

2019-01-29 12:00发布

I've been trying to create a generic partition function that returns an array of arrays. the function should be made under the following guidelines:
Arguments:

  1. An array
  2. A function

Objectives:

  1. Call <function> for each element in <array> passing it the arguments:

    element, key, <array>
    
  2. Return an array that is made up of 2 sub arrays:

     0. An array that contains all the values for which <function> returned something truthy
     1. An array that contains all the values for which <function> returned something falsy

Here is what I have so far. I get the return of two. I feel like maybe I just have to do the filter function on two separate occasions, but I'm not sure how to put it together. Thoughts and suggestions are highly appreciated.

_.partition = function (collection, test){
    var allValues = [];
    var matches = [];
    var misMatches = [];
    _.filter(collection.value, function(value, key, collection){
      if (test(value[key], key, collection) === "string"){
        matches.push(value[key]);
      }else{
        misMatches.push(value[key]);
      }
    });
  return allValues.push(matches, misMatches);
}

3条回答
在下西门庆
2楼-- · 2019-01-29 12:08

You're correct about calling the filter method on separate occasions. One filter call would obtain the truthy values; the other would obtain the falsy values:

_.partition = function(collection, testFunc) {
  var matches = collection.filter(function(elem) {
    return test(elem) === 'string';
  });

  var misMatches = collection.filter(function(elem) {
    return test(elem) !== 'string';
  });

  return [matches, misMatches];
}
查看更多
放我归山
3楼-- · 2019-01-29 12:17

You are close, but there are a couple issues I see:

  1. You are returning the result of allValues.push which is not allValues itself, but rather the new length of the array.
  2. You are using _.filter to iterate over array elements and sort them into two arrays. This is strange, since it's not the intended use of _.filter.

If you want a quick and readable solution using _.filter, this will work:

_.mixin({
  partition: function(collection, test) {
    return [
      _.filter(collection, test),  // items which satisfy condition
      _.filter(collection, _.negate(test))  // items which don't
    ];
  }
});

A more efficient solution which makes only one pass over the collection is below (this is almost what you already have):

_.mixin({
  partition: function(collection, test) {
    var matches = [], misMatches = [], value;

    // can replace this loop with _.each
    for (var i = 0, len = collection.length; i < len; ++i) {
      value = collection[i];

      // push the value into the appropriate array
      if (test(value, i, collection)) {
        matches.push(value);
      } else {
        misMatches.push(value);
      }
    }

    return [matches, misMatches];
  }
});

Usage examples (and Plunker):

function isOdd(x) {
  return x % 2;
}

// _.mixin allows you to do either one of these
_.partition([1, 2, 3, 4, 5, 6], isOdd);  // result: [[1, 3, 5], [2, 4, 6]]
_([1, 2, 3, 4, 5, 6]).partition(isOdd);  // result: [[1, 3, 5], [2, 4, 6]]

// this is a use case you brought up in the comments
_.partition([1, "a", 2, "b", 3, "c"], _.isString);  // result: [["a", "b", "c"], [1, 2, 3]]
查看更多
劳资没心,怎么记你
4楼-- · 2019-01-29 12:23

Here is a version which uses reduce:

function partition(arr, filter) {
  return arr.reduce(
    (r, e, i, a) => {
      r[filter(e, i, a) ? 0 : 1].push(e);
      return r;
    }, [[], []]);
}

Here's an alternative version which uses Array#filter to find the matches, and builds an array of non-matches as it goes along:

function partition(arr, filter) {
  var fail = [];

  var pass = arr.filter((e, i, a) => {
    if (filter(e, i, a)) return true;
    fail.push(e);
  });

  return [pass, fail];
}
查看更多
登录 后发表回答