Skip to content

Commit

Permalink
Merge pull request #59 from raichoo/master
Browse files Browse the repository at this point in the history
Add `partition` to Data.Array
  • Loading branch information
garyb committed Feb 27, 2016
2 parents f2ed8d2 + bf7dd08 commit e47d505
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"freeze": true,
"funcscope": true,
"futurehostile": true,
"globalstrict": true,
"strict": "global",
"latedef": true,
"maxparams": 1,
"noarg": true,
Expand Down
33 changes: 33 additions & 0 deletions docs/Data/Array.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ termination.
null :: forall a. Array a -> Boolean
```

Test whether an array is empty.

#### `length`

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

Attaches an element to the front of an array, creating a new array.

```purescript
cons 1 [2, 3, 4] = [1, 2, 3, 4]
```

Note, the running time of this function is `O(n)`.

#### `(:)`

``` purescript
Expand Down Expand Up @@ -158,6 +168,10 @@ determine the ordering of elements.
head :: forall a. Array a -> Maybe a
```

Get the first element in an array, or `Nothing` if the array is empty

Running time: `O(1)`.

#### `last`

``` purescript
Expand Down Expand Up @@ -215,6 +229,9 @@ f arr = case uncons arr of
index :: forall a. Array a -> Int -> Maybe a
```

This function provides a safe way to read a value at a particular index
from an array.

#### `(!!)`

``` purescript
Expand Down Expand Up @@ -309,6 +326,8 @@ index is out-of-bounds.
reverse :: forall a. Array a -> Array a
```

Reverse an array, creating a new array.

#### `concat`

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

#### `partition`

``` purescript
partition :: forall a. (a -> Boolean) -> Array a -> { yes :: Array a, no :: Array a }
```

Partition an array using a predicate function, creating a set of
new arrays. One for the values satisfying the predicate function
and one for values that don't.

#### `filterM`

``` purescript
Expand Down Expand Up @@ -372,6 +401,8 @@ a value, creating a new array.
sort :: forall a. (Ord a) => Array a -> Array a
```

Sort the elements of an array in increasing order, creating a new array.

#### `sortBy`

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

Extract a subarray by a start and end index.

#### `take`

``` purescript
Expand Down
15 changes: 15 additions & 0 deletions src/Data/Array.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,21 @@ exports.filter = function (f) {
};
};

exports.partition = function (f) {
return function (xs) {
var yes = [];
var no = [];
for (var i = 0; i < xs.length; i++) {
var x = xs[i];
if (f(x))
yes.push(x);
else
no.push(x);
}
return { yes: yes, no: no };
};
};

//------------------------------------------------------------------------------
// Sorting ---------------------------------------------------------------------
//------------------------------------------------------------------------------
Expand Down
8 changes: 8 additions & 0 deletions src/Data/Array.purs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ module Data.Array
, concat
, concatMap
, filter
, partition
, filterM
, mapMaybe
, catMaybes
Expand Down Expand Up @@ -369,6 +370,13 @@ concatMap = flip bind
-- | creating a new array.
foreign import filter :: forall a. (a -> Boolean) -> Array a -> Array a

-- | Partition an array using a predicate function, creating a set of
-- | new arrays. One for the values satisfying the predicate function
-- | and one for values that don't.
foreign import partition :: forall a. (a -> Boolean)
-> Array a
-> { yes :: Array a, no :: Array a }

-- | Filter where the predicate returns a monadic `Boolean`.
-- |
-- | ```purescript
Expand Down

0 comments on commit e47d505

Please sign in to comment.