← Back

Source

nonEmptyArray

Monad1<URI> & Comonad1<URI> & Foldable2v1<URI> & Traversable2v1<URI>

Added in v1.0.0 (instance)

NonEmptyArray

constructor(readonly head: A, readonly tail: Array<A>) {}

Added in v1.0.0 (data)

Data structure which represents non-empty arrays

ap

<B>(fab: NonEmptyArray<(a: A) => B>): NonEmptyArray<B>

Added in v1.0.0 (method)

Instance-bound implementation of Apply

Example

import { NonEmptyArray } from 'fp-ts/lib/NonEmptyArray'

const x = new NonEmptyArray(1, [2])
const double = (n: number): number => n * 2
assert.deepEqual(x.ap(new NonEmptyArray(double, [double])).toArray(), [2, 4, 2, 4])

ap_

<B, C>(this: NonEmptyArray<(b: B) => C>, fb: NonEmptyArray<B>): NonEmptyArray<C>

Added in v1.0.0 (method)

Flipped version of ap

Example

import { NonEmptyArray } from 'fp-ts/lib/NonEmptyArray'

const x = new NonEmptyArray(1, [2])
const double = (n: number) => n * 2
assert.deepEqual(new NonEmptyArray(double, [double]).ap_(x).toArray(), [2, 4, 2, 4])

chain

<B>(f: (a: A) => NonEmptyArray<B>): NonEmptyArray<B>

Added in v1.0.0 (method)

Instance-bound implementation of Chain

Example

import { NonEmptyArray } from 'fp-ts/lib/NonEmptyArray'

const x = new NonEmptyArray(1, [2])
const f = (a: number) => new NonEmptyArray(a, [4])
assert.deepEqual(x.chain(f).toArray(), [1, 4, 2, 4])

concat

(y: NonEmptyArray<A>): NonEmptyArray<A>

Added in v1.0.0 (method)

Instance-bound implementation of Semigroup

Example

import { NonEmptyArray } from 'fp-ts/lib/NonEmptyArray'

const x = new NonEmptyArray(1, [2])
const y = new NonEmptyArray(3, [4])
assert.deepEqual(x.concat(y).toArray(), [1, 2, 3, 4])

concatArray

(as: Array<A>): NonEmptyArray<A>

Added in v1.0.0 (method)

Concatenates this NonEmptyArray and passed Array

Example

import { NonEmptyArray } from 'fp-ts/lib/NonEmptyArray'

assert.deepEqual(new NonEmptyArray<number>(1, []).concatArray([2]), new NonEmptyArray(1, [2]))

extend

<B>(f: (fa: NonEmptyArray<A>) => B): NonEmptyArray<B>

Added in v1.0.0 (method)

Instance-bound implementation of Extend

Example

import { NonEmptyArray } from 'fp-ts/lib/NonEmptyArray'
import { fold, monoidSum } from 'fp-ts/lib/Monoid'

const sum = (as: NonEmptyArray<number>) => fold(monoidSum)(as.toArray())
assert.deepEqual(new NonEmptyArray(1, [2, 3, 4]).extend(sum), new NonEmptyArray(10, [9, 7, 4]))

extract

(): A

Added in v1.0.0 (method)

Instance-bound implementation of Comonad

Example

import { NonEmptyArray } from 'fp-ts/lib/NonEmptyArray'

assert.strictEqual(new NonEmptyArray(1, [2, 3]).extract(), 1)

inspect

(): string

Added in v1.0.0 (method)

Same as toString

last

(): A

Added in v1.6.0 (method)

Gets last element of this NonEmptyArray

Example

import { NonEmptyArray } from 'fp-ts/lib/NonEmptyArray'

assert.strictEqual(new NonEmptyArray(1, [2, 3]).last(), 3)
assert.strictEqual(new NonEmptyArray(1, []).last(), 1)

length

(): number

Added in v1.10.0 (method)

map

<B>(f: (a: A) => B): NonEmptyArray<B>

Added in v1.0.0 (method)

Instance-bound implementation of Functor

Example

import { NonEmptyArray } from 'fp-ts/lib/NonEmptyArray'

const double = (n: number): number => n * 2
assert.deepEqual(new NonEmptyArray(1, [2]).map(double), new NonEmptyArray(2, [4]))

max

(ord: Ord<A>): A

Added in v1.3.0 (method)

Gets maximum of this NonEmptyArray using specified Ord instance

Example

import { NonEmptyArray } from 'fp-ts/lib/NonEmptyArray'
import { ordNumber } from 'fp-ts/lib/Ord'

assert.strictEqual(new NonEmptyArray(1, [2, 3]).max(ordNumber), 3)

min

(ord: Ord<A>): A

Added in v1.3.0 (method)

Gets minimum of this NonEmptyArray using specified Ord instance

Example

import { NonEmptyArray } from 'fp-ts/lib/NonEmptyArray'
import { ordNumber } from 'fp-ts/lib/Ord'

assert.strictEqual(new NonEmptyArray(1, [2, 3]).min(ordNumber), 1)

reduce

<B>(b: B, f: (b: B, a: A) => B): B

Added in v1.0.0 (method)

Instance-bound implementation of Foldable

Example

import { NonEmptyArray } from 'fp-ts/lib/NonEmptyArray'

const x = new NonEmptyArray('a', ['b'])
assert.strictEqual(x.reduce('', (b, a) => b + a), 'ab')

reverse

(): NonEmptyArray<A>

Added in v1.6.0 (method)

Reverts this NonEmptyArray

Example

import { NonEmptyArray } from 'fp-ts/lib/NonEmptyArray'

assert.deepEqual(new NonEmptyArray(1, [2, 3]).reverse(), new NonEmptyArray(3, [2, 1]))

sort

(ord: Ord<A>): NonEmptyArray<A>

Added in v1.6.0 (method)

Sorts this NonEmptyArray using specified Ord instance

Example

import { NonEmptyArray } from 'fp-ts/lib/NonEmptyArray'
import { ordNumber } from 'fp-ts/lib/Ord'

assert.deepEqual(new NonEmptyArray(3, [2, 1]).sort(ordNumber), new NonEmptyArray(1, [2, 3]))

toArray

(): Array<A>

Added in v1.0.0 (method)

Converts this NonEmptyArray to plain Array

Example

import { NonEmptyArray } from 'fp-ts/lib/NonEmptyArray'

assert.deepEqual(new NonEmptyArray(1, [2, 3]).toArray(), [1, 2, 3])

toString

(): string

Added in v1.0.0 (method)

Return stringified representation of this NonEmptyArray

fromArray

<A>(as: Array<A>): Option<NonEmptyArray<A>>

Added in v1.0.0 (function)

Builds NonEmptyArray from Array returning Option#none or Option#some depending on amount of values in passed array

getSemigroup

<A = never>(): Semigroup<NonEmptyArray<A>>

Added in v1.0.0 (function)

Builds Semigroup instance for NonEmptyArray of specified type arument

group

<A>(S: Setoid<A>) => (as: Array<A>): Array<NonEmptyArray<A>>

Added in v1.7.0 (function)

Group equal, consecutive elements of an array into non empty arrays.

Example

import { NonEmptyArray, group } from 'fp-ts/lib/NonEmptyArray'
import { ordNumber } from 'fp-ts/lib/Ord'

assert.deepEqual(group(ordNumber)([1, 2, 1, 1]), [
  new NonEmptyArray(1, []),
  new NonEmptyArray(2, []),
  new NonEmptyArray(1, [1])
])

groupBy

<A>(as: Array<A>, f: (a: A) => string): { [key: string]: NonEmptyArray<A> }

Added in v1.10.0 (function)

Splits an array into sub-non-empty-arrays stored in an object, based on the result of calling a string-returning function on each element, and grouping the results according to values returned

Example

import { NonEmptyArray, groupBy } from 'fp-ts/lib/NonEmptyArray'

assert.deepEqual(groupBy(['foo', 'bar', 'foobar'], a => String(a.length)), {
  '3': new NonEmptyArray('foo', ['bar']),
  '6': new NonEmptyArray('foobar', [])
})

groupSort

<A>(O: Ord<A>): ((as: Array<A>) => Array<NonEmptyArray<A>>)

Added in v1.7.0 (function)

Sort and then group the elements of an array into non empty arrays.

Example

import { NonEmptyArray, groupSort } from 'fp-ts/lib/NonEmptyArray'
import { ordNumber } from 'fp-ts/lib/Ord'

assert.deepEqual(groupSort(ordNumber)([1, 2, 1, 1]), [new NonEmptyArray(1, [1, 1]), new NonEmptyArray(2, [])])