Overview

Data structure which represents non-empty arrays


Table of contents


NonEmptyArray (interface)

Signature

export interface NonEmptyArray<A> extends Array<A> {
  0: A
}

Added in v2.0.0

URI (type alias)

Signature

export type URI = typeof URI

Added in v2.0.0

URI (constant)

Signature

export const URI: "NonEmptyArray" = ...

Added in v2.0.0

cons (constant)

Append an element to the front of an array, creating a new non empty array

Signature

export const cons: <A>(head: A, tail: Array<A>) => NonEmptyArray<A> = ...

Example

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

assert.deepStrictEqual(cons(1, [2, 3, 4]), [1, 2, 3, 4])

Added in v2.0.0

copy (constant)

Signature

export const copy: <A>(nea: NonEmptyArray<A>) => NonEmptyArray<A> = ...

Added in v2.0.0

getEq (constant)

Signature

export const getEq: <A>(E: Eq<A>) => Eq<NonEmptyArray<A>> = ...

Example

import { getEq, cons } from 'fp-ts/lib/NonEmptyArray'
import { eqNumber } from 'fp-ts/lib/Eq'

const E = getEq(eqNumber)
assert.strictEqual(E.equals(cons(1, [2]), [1, 2]), true)
assert.strictEqual(E.equals(cons(1, [2]), [1, 3]), false)

Added in v2.0.0

getShow (constant)

Signature

export const getShow: <A>(S: Show<A>) => Show<NonEmptyArray<A>> = ...

Added in v2.0.0

nonEmptyArray (constant)

Signature

export const nonEmptyArray: Monad1<URI> &
  Comonad1<URI> &
  TraversableWithIndex1<URI, number> &
  FunctorWithIndex1<URI, number> &
  FoldableWithIndex1<URI, number> = ...

Added in v2.0.0

of (constant)

Signature

export const of: <A>(a: A) => NonEmptyArray<A> = ...

Added in v2.0.0

reverse (constant)

Signature

export const reverse: <A>(nea: NonEmptyArray<A>) => NonEmptyArray<A> = ...

Added in v2.0.0

snoc (constant)

Append an element to the end of an array, creating a new non empty array

Signature

export const snoc: <A>(init: Array<A>, end: A) => NonEmptyArray<A> = ...

Example

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

assert.deepStrictEqual(snoc([1, 2, 3], 4), [1, 2, 3, 4])

Added in v2.0.0

filter (function)

Signature

export function filter<A, B extends A>(
  refinement: Refinement<A, B>
): (nea: NonEmptyArray<A>) => Option<NonEmptyArray<A>>
export function filter<A>(predicate: Predicate<A>): (nea: NonEmptyArray<A>) => Option<NonEmptyArray<A>> { ... }

Added in v2.0.0

filterWithIndex (function)

Signature

export function filterWithIndex<A>(
  predicate: (i: number, a: A) => boolean
): (nea: NonEmptyArray<A>) => Option<NonEmptyArray<A>> { ... }

Added in v2.0.0

fromArray (function)

Builds a NonEmptyArray from an Array returning none if as is an empty array

Signature

export function fromArray<A>(as: Array<A>): Option<NonEmptyArray<A>> { ... }

Added in v2.0.0

getSemigroup (function)

Builds a Semigroup instance for NonEmptyArray

Signature

export function getSemigroup<A = never>(): Semigroup<NonEmptyArray<A>> { ... }

Added in v2.0.0

group (function)

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

Signature

export function group<A>(E: Eq<A>): (as: Array<A>) => Array<NonEmptyArray<A>> { ... }

Example

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

assert.deepStrictEqual(group(ordNumber)([1, 2, 1, 1]), [cons(1, []), cons(2, []), cons(1, [1])])

Added in v2.0.0

groupBy (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

Signature

export function groupBy<A>(f: (a: A) => string): (as: Array<A>) => Record<string, NonEmptyArray<A>> { ... }

Example

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

assert.deepStrictEqual(groupBy((s: string) => String(s.length))(['foo', 'bar', 'foobar']), {
  '3': cons('foo', ['bar']),
  '6': cons('foobar', [])
})

Added in v2.0.0

groupSort (function)

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

Signature

export function groupSort<A>(O: Ord<A>): (as: Array<A>) => Array<NonEmptyArray<A>> { ... }

Example

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

assert.deepStrictEqual(groupSort(ordNumber)([1, 2, 1, 1]), [cons(1, [1, 1]), cons(2, [])])

Added in v2.0.0

head (function)

Signature

export function head<A>(nea: NonEmptyArray<A>): A { ... }

Added in v2.0.0

insertAt (function)

Signature

export function insertAt<A>(i: number, a: A): (nea: NonEmptyArray<A>) => Option<NonEmptyArray<A>> { ... }

Added in v2.0.0

last (function)

Signature

export function last<A>(nea: NonEmptyArray<A>): A { ... }

Added in v2.0.0

max (function)

Signature

export function max<A>(ord: Ord<A>): (nea: NonEmptyArray<A>) => A { ... }

Added in v2.0.0

min (function)

Signature

export function min<A>(ord: Ord<A>): (nea: NonEmptyArray<A>) => A { ... }

Added in v2.0.0

modifyAt (function)

Signature

export function modifyAt<A>(i: number, f: (a: A) => A): (nea: NonEmptyArray<A>) => Option<NonEmptyArray<A>> { ... }

Added in v2.0.0

sort (function)

Signature

export function sort<A>(O: Ord<A>): (nea: NonEmptyArray<A>) => NonEmptyArray<A> { ... }

Added in v2.0.0

tail (function)

Signature

export function tail<A>(nea: NonEmptyArray<A>): Array<A> { ... }

Added in v2.0.0

updateAt (function)

Signature

export function updateAt<A>(i: number, a: A): (nea: NonEmptyArray<A>) => Option<NonEmptyArray<A>> { ... }

Added in v2.0.0

ap (export)

Signature

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

Added in v2.0.0

apFirst (export)

Signature

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

Added in v2.0.0

apSecond (export)

Signature

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

Added in v2.0.0

chain (export)

Signature

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

Added in v2.0.0

chainFirst (export)

Signature

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

Added in v2.0.0

duplicate (export)

Signature

<A>(ma: NonEmptyArray<A>) => NonEmptyArray<NonEmptyArray<A>>

Added in v2.0.0

extend (export)

Signature

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

Added in v2.0.0

flatten (export)

Signature

<A>(mma: NonEmptyArray<NonEmptyArray<A>>) => NonEmptyArray<A>

Added in v2.0.0

foldMap (export)

Signature

;<M>(M: Monoid<M>) => <A>(f: (a: A) => M) => (fa: NonEmptyArray<A>) => M

Added in v2.0.0

foldMapWithIndex (export)

Signature

;<M>(M: Monoid<M>) => <A>(f: (i: number, a: A) => M) => (fa: NonEmptyArray<A>) => M

Added in v2.0.0

map (export)

Signature

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

Added in v2.0.0

mapWithIndex (export)

Signature

<A, B>(f: (i: number, a: A) => B) => (fa: NonEmptyArray<A>) => NonEmptyArray<B>

Added in v2.0.0

reduce (export)

Signature

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

Added in v2.0.0

reduceRight (export)

Signature

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

Added in v2.0.0

reduceRightWithIndex (export)

Signature

;<A, B>(b: B, f: (i: number, a: A, b: B) => B) => (fa: NonEmptyArray<A>) => B

Added in v2.0.0

reduceWithIndex (export)

Signature

;<A, B>(b: B, f: (i: number, b: B, a: A) => B) => (fa: NonEmptyArray<A>) => B

Added in v2.0.0