Hyperlinkv0.8.0-beta.28

Array

Array.modifyLastNonEmptyconsteffect/Array.ts:2426
<A, B>(f: (a: A) => B): (
  self: NonEmptyReadonlyArray<A>
) => NonEmptyArray<A | B>
<A, B>(self: NonEmptyReadonlyArray<A>, f: (a: A) => B): NonEmptyArray<
  A | B
>

Applies a function to the last element of a non-empty array, returning a new array.

When to use

Use when you already know the array is non-empty and the new last element depends on the current last element.

Example (Modifying the last element)

import { Array } from "effect"

console.log(Array.modifyLastNonEmpty([1, 2, 3], (n) => n * 2)) // [1, 2, 6]
Source effect/Array.ts:24268 lines
export const modifyLastNonEmpty: {
  <A, B>(f: (a: A) => B): (self: NonEmptyReadonlyArray<A>) => NonEmptyArray<A | B>
  <A, B>(self: NonEmptyReadonlyArray<A>, f: (a: A) => B): NonEmptyArray<A | B>
} = dual(
  2,
  <A, B>(self: NonEmptyReadonlyArray<A>, f: (a: A) => B): NonEmptyArray<A | B> =>
    append(initNonEmpty(self), f(lastNonEmpty(self)))
)
Referenced by 1 symbols