Skip to content

Commit e47d505

Browse files
committed
Merge pull request #59 from raichoo/master
Add `partition` to Data.Array
2 parents f2ed8d2 + bf7dd08 commit e47d505

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

.jshintrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"freeze": true,
66
"funcscope": true,
77
"futurehostile": true,
8-
"globalstrict": true,
8+
"strict": "global",
99
"latedef": true,
1010
"maxparams": 1,
1111
"noarg": true,

docs/Data/Array.md

+33
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ termination.
101101
null :: forall a. Array a -> Boolean
102102
```
103103

104+
Test whether an array is empty.
105+
104106
#### `length`
105107

106108
``` purescript
@@ -115,6 +117,14 @@ Get the number of elements in an array.
115117
cons :: forall a. a -> Array a -> Array a
116118
```
117119

120+
Attaches an element to the front of an array, creating a new array.
121+
122+
```purescript
123+
cons 1 [2, 3, 4] = [1, 2, 3, 4]
124+
```
125+
126+
Note, the running time of this function is `O(n)`.
127+
118128
#### `(:)`
119129

120130
``` purescript
@@ -158,6 +168,10 @@ determine the ordering of elements.
158168
head :: forall a. Array a -> Maybe a
159169
```
160170

171+
Get the first element in an array, or `Nothing` if the array is empty
172+
173+
Running time: `O(1)`.
174+
161175
#### `last`
162176

163177
``` purescript
@@ -215,6 +229,9 @@ f arr = case uncons arr of
215229
index :: forall a. Array a -> Int -> Maybe a
216230
```
217231

232+
This function provides a safe way to read a value at a particular index
233+
from an array.
234+
218235
#### `(!!)`
219236

220237
``` purescript
@@ -309,6 +326,8 @@ index is out-of-bounds.
309326
reverse :: forall a. Array a -> Array a
310327
```
311328

329+
Reverse an array, creating a new array.
330+
312331
#### `concat`
313332

314333
``` purescript
@@ -335,6 +354,16 @@ filter :: forall a. (a -> Boolean) -> Array a -> Array a
335354
Filter an array, keeping the elements which satisfy a predicate function,
336355
creating a new array.
337356

357+
#### `partition`
358+
359+
``` purescript
360+
partition :: forall a. (a -> Boolean) -> Array a -> { yes :: Array a, no :: Array a }
361+
```
362+
363+
Partition an array using a predicate function, creating a set of
364+
new arrays. One for the values satisfying the predicate function
365+
and one for values that don't.
366+
338367
#### `filterM`
339368

340369
``` purescript
@@ -372,6 +401,8 @@ a value, creating a new array.
372401
sort :: forall a. (Ord a) => Array a -> Array a
373402
```
374403

404+
Sort the elements of an array in increasing order, creating a new array.
405+
375406
#### `sortBy`
376407

377408
``` purescript
@@ -387,6 +418,8 @@ the specified partial ordering, creating a new array.
387418
slice :: forall a. Int -> Int -> Array a -> Array a
388419
```
389420

421+
Extract a subarray by a start and end index.
422+
390423
#### `take`
391424

392425
``` purescript

src/Data/Array.js

+15
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

+8
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)