<A>(self: NonEmptyChunk<A>): AReturns the first element of this non empty chunk.
Example (Getting the first element of a non-empty chunk)
import { Chunk } from "effect"
const nonEmptyChunk = Chunk.make(1, 2, 3, 4)
console.log(Chunk.headNonEmpty(nonEmptyChunk)) // 1
const singleElement = Chunk.make("hello")
console.log(Chunk.headNonEmpty(singleElement)) // "hello"
// Type safety: this function only accepts NonEmptyChunk
// Chunk.headNonEmpty(Chunk.empty()) // TypeScript errorelements
Source effect/Chunk.ts:14611 lines
export const const headNonEmpty: <A>(
self: NonEmptyChunk<A>
) => A
Returns the first element of this non empty chunk.
Example (Getting the first element of a non-empty chunk)
import { Chunk } from "effect"
const nonEmptyChunk = Chunk.make(1, 2, 3, 4)
console.log(Chunk.headNonEmpty(nonEmptyChunk)) // 1
const singleElement = Chunk.make("hello")
console.log(Chunk.headNonEmpty(singleElement)) // "hello"
// Type safety: this function only accepts NonEmptyChunk
// Chunk.headNonEmpty(Chunk.empty()) // TypeScript error
headNonEmpty: <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 headUnsafe: <A>(self: Chunk<A>) => AReturns the first element of this chunk.
When to use
Use when you know the chunk is non-empty and need the first element directly
without handling Option.none.
Gotchas
Throws an error if the chunk is empty.
Example (Getting the first element unsafely)
import { Chunk, Option } from "effect"
const chunk = Chunk.make(1, 2, 3, 4)
console.log(Chunk.headUnsafe(chunk)) // 1
const singleElement = Chunk.make("hello")
console.log(Chunk.headUnsafe(singleElement)) // "hello"
// Use Chunk.head when the chunk may be empty
console.log(Option.isNone(Chunk.head(Chunk.empty()))) // true
headUnsafeReferenced by 1 symbols