<A>(self: NonEmptyChunk<A>): AReturns the last element of this non empty chunk.
Example (Getting the last element of a non-empty chunk)
import { Chunk } from "effect"
const nonEmptyChunk = Chunk.make(1, 2, 3, 4)
console.log(Chunk.lastNonEmpty(nonEmptyChunk)) // 4
const singleElement = Chunk.make("hello")
console.log(Chunk.lastNonEmpty(singleElement)) // "hello"
// Type safety: this function only accepts NonEmptyChunk
// Chunk.lastNonEmpty(Chunk.empty()) // TypeScript errorelements
Source effect/Chunk.ts:15331 lines
export const const lastNonEmpty: <A>(
self: NonEmptyChunk<A>
) => A
Returns the last element of this non empty chunk.
Example (Getting the last element of a non-empty chunk)
import { Chunk } from "effect"
const nonEmptyChunk = Chunk.make(1, 2, 3, 4)
console.log(Chunk.lastNonEmpty(nonEmptyChunk)) // 4
const singleElement = Chunk.make("hello")
console.log(Chunk.lastNonEmpty(singleElement)) // "hello"
// Type safety: this function only accepts NonEmptyChunk
// Chunk.lastNonEmpty(Chunk.empty()) // TypeScript error
lastNonEmpty: <function (type parameter) A in <A>(self: NonEmptyChunk<A>): AA>(self: NonEmptyChunk<A>(parameter) self: {
length: number;
right: Chunk<A>;
left: Chunk<A>;
backing: Backing<A>;
depth: number;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
self: interface NonEmptyChunk<out A>A non-empty Chunk guaranteed to contain at least one element.
Example (Working with non-empty chunks)
import { Chunk } from "effect"
const nonEmptyChunk: Chunk.NonEmptyChunk<number> = Chunk.make(1, 2, 3)
console.log(Chunk.headNonEmpty(nonEmptyChunk)) // 1
console.log(Chunk.lastNonEmpty(nonEmptyChunk)) // 3
NonEmptyChunk<function (type parameter) A in <A>(self: NonEmptyChunk<A>): AA>) => function (type parameter) A in <A>(self: NonEmptyChunk<A>): AA = const lastUnsafe: <A>(self: Chunk<A>) => AReturns the last element of this chunk.
When to use
Use when you know the chunk is non-empty and need the last element directly
without handling Option.none.
Gotchas
Throws an error if the chunk is empty.
Example (Getting the last element unsafely)
import { Chunk, Option } from "effect"
const chunk = Chunk.make(1, 2, 3, 4)
console.log(Chunk.lastUnsafe(chunk)) // 4
const singleElement = Chunk.make("hello")
console.log(Chunk.lastUnsafe(singleElement)) // "hello"
// Use Chunk.last when the chunk may be empty
console.log(Option.isNone(Chunk.last(Chunk.empty()))) // true
lastUnsafeReferenced by 1 symbols