Skip to main content Link Search Menu Expand Document (external link)

Array overview

A simple extension to the Effect Array module


Table of contents


Constructors

unfold

Same as Array.unfold but with cycle detection and curried

Signature

export declare const unfold: <A, B>(
  f: (a: A, isCyclical: boolean) => Option.Option<MTypes.Pair<B, A>>
) => (a: A) => Array<B>

unfoldNonEmpty

Same as unfold but f always returns a B and an Option

Signature

export declare const unfoldNonEmpty: <A, B>(
  f: (a: A, isCyclical: boolean) => MTypes.Pair<B, Option.Option<A>>
) => (a: A) => MTypes.OverOne<B>

Destructors

firstSomeResult

Applies a function f that returns an option to each element of self. Returns the first some encountered if any. Returns none otherwise.

Signature

export declare const firstSomeResult: <A, B>(
  f: (a: NoInfer<A>) => Option.Option<B>
) => (self: Iterable<A>) => Option.Option<B>

Equivalences

numberEquivalence

Equivalence for arrays of numbers. To be removed when Equal.equals will handle Arrays properly (from Effect 4.0 onwards)

Signature

export declare const numberEquivalence: Equivalence.Equivalence<readonly number[]>

Predicates

hasLength

Returns true if the length of self is l

Signature

export declare const hasLength: (l: number) => <A>(self: ReadonlyArray<A>) => boolean

Utils

differenceSorted

Removes all elements of that from self. The sorting order o must also be the one that was used to sort self and that

Signature

export declare const differenceSorted: <A>(o: Order.Order<A>) => (that: Iterable<A>) => (self: Iterable<A>) => Array<A>

extractFirst

Extracts from an array the first item that matches the predicate. Returns the extracted item and the remaining items.

Signature

export declare const extractFirst: {
  <A, B extends A>(
    refinement: (a: NoInfer<A>, i: number) => a is B
  ): (self: ReadonlyArray<A>) => MTypes.Pair<Option.Option<B>, MTypes.MutableArray<A>>
  <A>(
    predicate: (a: NoInfer<A>, i: number) => boolean
  ): (self: ReadonlyArray<A>) => MTypes.Pair<Option.Option<A>, MTypes.MutableArray<A>>
}

findAll

Returns an array of the indexes of all elements of self matching the predicate

Signature

export declare const findAll: <A>(predicate: Predicate.Predicate<NoInfer<A>>) => (self: Iterable<A>) => Array<number>

getFromEnd

This function provides a safe way to read a value at a particular index from the end of a ReadonlyArray. Index 0 will return the last element of the array.

Signature

export declare const getFromEnd: (index: number) => <A>(self: ReadonlyArray<A>) => Option.Option<A>

getter

Same as get but with flipped parameters

Signature

export declare const getter: <A>(self: ReadonlyArray<A>) => MTypes.OneArgFunction<number, Option.Option<A>>

groupBy

Same as Array.groupBy but with a value projection function

Signature

export declare const groupBy: <A, B>({
  fKey,
  fValue
}: {
  readonly fKey: (a: NoInfer<A>) => string
  readonly fValue: (a: NoInfer<A>) => B
}) => (self: Iterable<A>) => Record<string, MTypes.OverOne<B>>

groupByNum

The elements of self are mapped by a fValue function and grouped by a fKey function. Size is the size of the output array. If fKey returns a negative index or an index superior or equal to size, the corresponding value is ignored. There may be holes in the output array. Can be used to reverse the ungroup function.

Signature

export declare const groupByNum: <A, B>({
  size,
  fKey,
  fValue
}: {
  readonly size: number
  readonly fKey: (a: NoInfer<A>) => number
  readonly fValue: (a: NoInfer<A>) => B
}) => (self: ReadonlyArray<A>) => ReadonlyArray<ReadonlyArray<B>>

Example

import { MArray } from "@parischap/effect-lib"
import { pipe, Tuple } from "effect"

const foo: ReadonlyArray<readonly [number, number]> = [
  [0, 1],
  [0, 2],
  [0, 3],
  [1, 1],
  [1, 2],
  [1, 3]
]

assert.deepStrictEqual(pipe(foo, MArray.groupByNum({ size: 2, fKey: Tuple.getFirst, fValue: Tuple.getSecond })), [
  [1, 2, 3],
  [1, 2, 3]
])

hasDuplicates

Returns true if the provided ReadonlyArray contains duplicates

Signature

export declare const hasDuplicates: (self: readonly unknown[]) => boolean

hasDuplicatesWith

Returns true if the provided ReadonlyArray contains duplicates using the provided isEquivalent function

Signature

export declare const hasDuplicatesWith: <A>(
  isEquivalent: Equivalence.Equivalence<NoInfer<A>>
) => (self: ReadonlyArray<A>) => boolean

longestCommonSubArray

This function returns the longest sub-array common to self and that starting at index 0

Signature

export declare const longestCommonSubArray: <A>(that: Iterable<A>) => (self: Iterable<A>) => Array<A>

match012

Matches the elements of an array, applying functions to cases of empty arrays, arrays containing a single element, and arrays containing two or more elements.

Signature

export declare const match012: <A, B, C = B, D = B>(options: {
  readonly onEmpty: Function.LazyArg<B>
  readonly onSingleton: (self: NoInfer<A>) => C
  readonly onOverTwo: (self: MTypes.ReadonlyOverTwo<NoInfer<A>>) => D
}) => (self: ReadonlyArray<A>) => B | C | D

mergeSorted

Merges two sorted Iterables into a sorted array. Elements in self are assured to be before equal elements in that in the resulting array. The sorting order o must also be the one that was used to sort self and that

Signature

export declare const mergeSorted: <A>(o: Order.Order<A>) => (that: Iterable<A>) => (self: Iterable<A>) => Array<A>

modifyHead

Returns a copy of self with the first element modified by a function f. Returns a copy of self if it contains no elements.

Signature

export declare const modifyHead: <S extends MTypes.AnyReadonlyArray, B>(
  f: (a: Array.ReadonlyArray.Infer<S>) => B
) => (self: S) => Array.ReadonlyArray.With<S, Array.ReadonlyArray.Infer<S> | B>

modifyInit

Returns a copy of self with all elements but the last modified by a function f. Returns a copy of self if it contains at most one element.

Signature

export declare const modifyInit: <S extends MTypes.AnyReadonlyArray, B>(
  f: (a: Array.ReadonlyArray.Infer<S>, i: number) => B
) => (self: S) => Array.ReadonlyArray.With<S, B>

modifyLast

Returns a copy of self with the last element modified by a function f. Returns a copy of self if it contains no elements.

Signature

export declare const modifyLast: <S extends MTypes.AnyReadonlyArray, B>(
  f: (a: Array.ReadonlyArray.Infer<S>) => B
) => (self: S) => Array.ReadonlyArray.With<S, Array.ReadonlyArray.Infer<S> | B>

modifyTail

Returns a copy of self with all elements but the first modified by a function f . Returns a copy of self if it contains at most one element.

Signature

export declare const modifyTail: <S extends MTypes.AnyReadonlyArray, B>(
  f: (a: Array.ReadonlyArray.Infer<S>, i: number) => B
) => (self: S) => Array.ReadonlyArray.With<S, B>

splitAtFromRight

Splits self into two segments, with the last segment containing a maximum of n elements. The value of n can be 0.

Signature

export declare const splitAtFromRight: (
  n: number
) => <A>(self: ReadonlyArray<A>) => [beforeIndex: Array<A>, fromIndex: Array<A>]

splitNonEmptyAtFromRight

Splits self into two segments, with the last segment containing a maximum of n elements. The value of n must be >=1.

Signature

export declare const splitNonEmptyAtFromRight: (
  n: number
) => <A>(self: MTypes.OverOne<A>) => [beforeIndex: Array<A>, fromIndex: MTypes.OverOne<A>]

takeBut

Takes all elements of self except the n last elements

Signature

export declare const takeBut: (n: number) => <A>(self: ReadonlyArray<A>) => Array<A>

takeRightBut

Takes all elements of self except the n first elements

Signature

export declare const takeRightBut: (n: number) => <A>(self: ReadonlyArray<A>) => Array<A>

ungroup

Flattens an array of arrays adding an index that will allow to reverse this operation with groupByNum

Signature

export declare const ungroup: <A>(as: ReadonlyArray<ReadonlyArray<A>>) => Array<[number, A]>

Example

import { MArray } from "@parischap/effect-lib"
import { pipe } from "effect"

assert.deepStrictEqual(
  pipe(
    [
      [1, 2, 3],
      [3, 4, 5]
    ],
    MArray.ungroup
  ),
  [
    [0, 1],
    [0, 2],
    [0, 3],
    [1, 3],
    [1, 4],
    [1, 5]
  ]
)

unsafeGet

Unsafe gets an element from an array. No bounds check, faster than the Effect version

Signature

export declare const unsafeGet: (index: number) => <A>(self: ReadonlyArray<A>) => A

unsafeGetter

Same as unsafeGet but with flipped parameters

Signature

export declare const unsafeGetter: <A>(self: ReadonlyArray<A>) => MTypes.OneArgFunction<number, A>