Skip to content

Commit cf7c202

Browse files
committed
Add partition to Data.Array
1 parent f2ed8d2 commit cf7c202

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/Data/Array.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,21 @@ exports.filter = function (f) {
174174
};
175175
};
176176

177+
exports.partition = function (f) {
178+
return function (xs) {
179+
var yes = [];
180+
var no = [];
181+
for (var i = 0; i < xs.length; i++) {
182+
var x = xs[i];
183+
if (f(x))
184+
yes.push(x);
185+
else
186+
no.push(x);
187+
}
188+
return {yes: yes, no: no};
189+
};
190+
};
191+
177192
//------------------------------------------------------------------------------
178193
// Sorting ---------------------------------------------------------------------
179194
//------------------------------------------------------------------------------

src/Data/Array.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ module Data.Array
6464
, concat
6565
, concatMap
6666
, filter
67+
, partition
6768
, filterM
6869
, mapMaybe
6970
, catMaybes
@@ -369,6 +370,13 @@ concatMap = flip bind
369370
-- | creating a new array.
370371
foreign import filter :: forall a. (a -> Boolean) -> Array a -> Array a
371372

373+
-- | Partition an array using a predicate function, creating a set of
374+
-- | new arrays. One for the values satisfying the predicate function
375+
-- | and one for values that don't.
376+
foreign import partition :: forall a. (a -> Boolean)
377+
-> Array a
378+
-> { yes :: Array a, no :: Array a }
379+
372380
-- | Filter where the predicate returns a monadic `Boolean`.
373381
-- |
374382
-- | ```purescript

0 commit comments

Comments
 (0)