Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #349: add _.partition function #1430

Merged
merged 2 commits into from
Jan 31, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@
<li>- <a href="#compact">compact</a></li>
<li>- <a href="#flatten">flatten</a></li>
<li>- <a href="#without">without</a></li>
<li>- <a href="#partition">partition</a></li>
<li>- <a href="#union">union</a></li>
<li>- <a href="#intersection">intersection</a></li>
<li>- <a href="#difference">difference</a></li>
Expand Down Expand Up @@ -432,7 +433,7 @@ <h2 id="collections">Collection Functions (Arrays or Objects)</h2>
array-like objects such as</i> <tt>arguments</tt>, <tt>NodeList</tt><i>
and similar. But it works by duck-typing, so avoid passing objects with
a numeric <tt>length</tt> property. It's also good to note that an
<tt>each</tt> loop cannot be broken out of — to break, use <b>_.find</b>
<tt>each</tt> loop cannot be broken out of — to break, use <b>_.find</b>
instead.
</i>
</p>
Expand Down Expand Up @@ -841,6 +842,17 @@ <h2 id="arrays">Array Functions</h2>
<pre>
_.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
=&gt; [2, 3, 4]
</pre>

<p id="partition">
<b class="header">partition</b><code>_.partition(array, predicate)</code>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it okay to use "predicate" now? The documentation doesn't currently use this term, though it should: "iterator" is both vague and slightly misleading.

I'd like to open a pull request to replace "iterator" with "predicate" where appropriate (in the code as well as the documentation). Would such a pull request be considered?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I just used what was natural to me. I'd +1 such a PR.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that would be a great change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<br />
Split <b>array</b> into two arrays: one whose elements all satisfy
<b>predicate</b>, and one whose elements all do not satisfy <b>predicate</b>.
</p>
<pre>
_.partition([0, 1, 2, 3, 4, 5], function(e) { return isOdd(e); });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason this isn't simply

_.partition([0, 1, 2, 3, 4, 5], isOdd);

?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah, yeah we can do that. I had simplified an earlier version that included the parity test implementation, but didn't notice that it could be simplified more. Will update with commit SHA.

edit: 678900b

=&gt; [[1, 3, 5], [0, 2, 4]]
</pre>

<p id="union">
Expand Down
10 changes: 10 additions & 0 deletions test/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ $(document).ready(function() {
ok(_.without(list, list[0]).length == 1, 'ditto.');
});

test("partition", function() {
var list = [0, 1, 2, 3, 4, 5];
function inspect(x) { return x instanceof Array ? '[' + _.map(x, inspect) + ']' : '' + x; }
equal(inspect(_.partition(list, function(x) { return x < 4; })), '[[0,1,2,3],[4,5]]', 'handles bool return values');
equal(inspect(_.partition(list, function(x) { return x & 1; })), '[[1,3,5],[0,2,4]]', 'handles 0 and 1 return values');
equal(inspect(_.partition(list, function(x) { return x - 3; })), '[[0,1,2,4,5],[3]]', 'handles other numeric return values');
equal(inspect(_.partition(list, function(x) { return x > 1 ? null : true; })), '[[0,1],[2,3,4,5]]', 'handles null return values');
equal(inspect(_.partition(list, function(x) { if(x < 2) return true; })), '[[0,1],[2,3,4,5]]', 'handles undefined return values');
});

test("uniq", function() {
var list = [1, 2, 1, 3, 1, 4];
equal(_.uniq(list).join(', '), '1, 2, 3, 4', 'can find the unique values of an unsorted array');
Expand Down
10 changes: 10 additions & 0 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,16 @@
return _.difference(array, slice.call(arguments, 1));
};

// Split an array into two arrays: one whose elements all satisfy the given
// predicate, and one whose elements all do not satisfy the predicate.
_.partition = function(array, predicate) {
var pass = [], fail = [];
each(array, function(elem) {
(predicate(elem) ? pass : fail).push(elem);
});
return [pass, fail];
};

// Produce a duplicate-free version of the array. If the array has already
// been sorted, you have the option of using a faster algorithm.
// Aliased as `unique`.
Expand Down