Skip to content

Commit

Permalink
add isNonEmpty
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Jun 5, 2019
1 parent 26644c7 commit 7c25859
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
15 changes: 14 additions & 1 deletion docs/modules/Array.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Adapted from https://github.com/purescript/purescript-arrays
- [insertAt (function)](#insertat-function)
- [intersection (function)](#intersection-function)
- [isEmpty (function)](#isempty-function)
- [isNonEmpty (function)](#isnonempty-function)
- [isOutOfBound (function)](#isoutofbound-function)
- [last (function)](#last-function)
- [lefts (function)](#lefts-function)
Expand Down Expand Up @@ -138,7 +139,7 @@ value and the rest of the array.
**Signature**

```ts
export function chop<A, B>(f: (as: Array<A>) => [B, Array<A>]): (as: Array<A>) => Array<B> { ... }
export function chop<A, B>(f: (as: NonEmptyArray<A>) => [B, Array<A>]): (as: Array<A>) => Array<B> { ... }
```

**Example**
Expand Down Expand Up @@ -790,6 +791,18 @@ assert.strictEqual(isEmpty([]), true)

Added in v2.0.0

# isNonEmpty (function)

Test whether an array is non empty narrowing down the type to `NonEmptyArray<A>`

**Signature**

```ts
export function isNonEmpty<A>(as: Array<A>): as is NonEmptyArray<A> { ... }
```

Added in v2.0.0

# isOutOfBound (function)

Test whether an array contains a particular index
Expand Down
13 changes: 11 additions & 2 deletions src/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,15 @@ export function isEmpty<A>(as: Array<A>): boolean {
return as.length === 0
}

/**
* Test whether an array is non empty narrowing down the type to `NonEmptyArray<A>`
*
* @since 2.0.0
*/
export function isNonEmpty<A>(as: Array<A>): as is NonEmptyArray<A> {
return as.length > 0
}

/**
* Test whether an array contains a particular index
*
Expand Down Expand Up @@ -1113,11 +1122,11 @@ export function sortBy<A>(ords: Array<Ord<A>>): (as: Array<A>) => Array<A> {
*
* @since 2.0.0
*/
export function chop<A, B>(f: (as: Array<A>) => [B, Array<A>]): (as: Array<A>) => Array<B> {
export function chop<A, B>(f: (as: NonEmptyArray<A>) => [B, Array<A>]): (as: Array<A>) => Array<B> {
return as => {
const result: Array<B> = []
let cs: Array<A> = as
while (cs.length > 0) {
while (isNonEmpty(cs)) {
const [b, c] = f(cs)
result.push(b)
cs = c
Expand Down
8 changes: 7 additions & 1 deletion test/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ import {
findLastMap,
getShow,
reverse,
getEq
getEq,
isNonEmpty
} from '../src/Array'
import { left, right } from '../src/Either'
import { fold as foldMonoid, monoidSum, monoidString } from '../src/Monoid'
Expand Down Expand Up @@ -152,6 +153,11 @@ describe('Array', () => {
assert.strictEqual(isEmpty([]), true)
})

it('isNotEmpty', () => {
assert.strictEqual(isNonEmpty(as), true)
assert.strictEqual(isNonEmpty([]), false)
})

it('cons', () => {
assert.deepStrictEqual(cons(0, as), [0, 1, 2, 3])
assert.deepStrictEqual(cons([1], [[2]]), [[1], [2]])
Expand Down

0 comments on commit 7c25859

Please sign in to comment.