Skip to content

Add partition to Data.Array #59

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

Merged
merged 3 commits into from
Feb 27, 2016
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
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 }
Copy link
Contributor

Choose a reason for hiding this comment

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

I like the return type, I frequently have to consult Hackage to remember the right way round in Haskell for things like this.


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