Chunk overview
Extension to the Effect Chunk module providing length and duplicate predicates, indexed search, and slicing operations.
Mental model
Chunk.Chunk<A>is an immutable, persistent sequence of elements of typeA.- All functions return new chunks; chunks are never mutated.
- Functions are curried, data-last — call as
MChunk.fn(arg)(chunk)orpipe(chunk, MChunk.fn(arg)). They are not data-first/data-last dual.
Common tasks
- Query: {@link hasLength}, {@link hasDuplicates}
- Search: {@link findAll}
- Slice: {@link takeBut}, {@link takeRightBut}
Quickstart
Example (Indexed search and duplicate check)
import { Chunk, pipe } from "effect"
import * as MChunk from "@parischap/effect-lib/MChunk"
import * as MPredicate from "@parischap/effect-lib/MPredicate"
const chunk = Chunk.make(1, 2, 3, 2, 4)
console.log(MChunk.hasDuplicates(chunk)) // true
console.log(pipe(chunk, MChunk.findAll(MPredicate.strictEquals(2)))) // Chunk(1, 3)
Table of contents
Models
Type (interface)
Type on which this module’s functions operate.
Signature
export interface Type<out A> extends Chunk.Chunk<A> {}
Predicates
hasDuplicates
Returns true if self contains at least one duplicate, using Equal.equals for comparison.
- Use to validate uniqueness constraints.
- Comparison uses Effect’s
Equal.equals(structural equality forEqual-aware types, otherwise===). - Returns
falsefor empty chunks.
Example (Detecting duplicates)
import { Chunk } from "effect"
import * as MChunk from "@parischap/effect-lib/MChunk"
console.log(MChunk.hasDuplicates(Chunk.make(1, 2, 3))) // false
console.log(MChunk.hasDuplicates(Chunk.make(1, 2, 2, 3))) // true
Signature
export declare const hasDuplicates: <A>(self: Type<A>) => boolean
hasLength
Returns true if the length of self is exactly l.
- Use to assert that a chunk has a specific number of elements.
- Length comparison is
===.
Example (Length check)
import { Chunk, pipe } from "effect"
import * as MChunk from "@parischap/effect-lib/MChunk"
const chunk = Chunk.make(1, 2, 3)
console.log(pipe(chunk, MChunk.hasLength(3))) // true
console.log(pipe(chunk, MChunk.hasLength(2))) // false
Signature
export declare const hasLength: (l: number) => <A>(self: Type<A>) => boolean
Utils
findAll
Returns a Chunk of the indexes of all elements of self satisfying predicate.
- Use to locate every position matching a condition.
- Indexes are returned in ascending order.
- Returns an empty chunk when no element matches.
Example (Indexes of matching elements)
import { Chunk, pipe } from "effect"
import * as MChunk from "@parischap/effect-lib/MChunk"
import * as MPredicate from "@parischap/effect-lib/MPredicate"
const chunk = Chunk.make(1, 2, 3, 2, 4)
console.log(pipe(chunk, MChunk.findAll(MPredicate.strictEquals(2)))) // Chunk(1, 3)
Signature
export declare const findAll: <B extends A, A = B>(
predicate: Predicate.Predicate<A>
) => (self: Type<B>) => Chunk.Chunk<number>
takeBut
Returns a chunk containing all elements of self except the last n.
- Use to drop a suffix of fixed size.
- When
n >= self.length, returns an empty chunk. - When
n <= 0, returnsselfunchanged.
Example (Drop trailing elements)
import { Chunk, pipe } from "effect"
import * as MChunk from "@parischap/effect-lib/MChunk"
const chunk = Chunk.make(1, 2, 3, 4, 5)
console.log(pipe(chunk, MChunk.takeBut(2))) // Chunk(1, 2, 3)
Signature
export declare const takeBut: (n: number) => <A>(self: Type<A>) => Chunk.Chunk<A>
takeRightBut
Returns a chunk containing all elements of self except the first n.
- Use to drop a prefix of fixed size.
- When
n >= self.length, returns an empty chunk. - When
n <= 0, returnsselfunchanged.
Example (Drop leading elements)
import { Chunk, pipe } from "effect"
import * as MChunk from "@parischap/effect-lib/MChunk"
const chunk = Chunk.make(1, 2, 3, 4, 5)
console.log(pipe(chunk, MChunk.takeRightBut(2))) // Chunk(3, 4, 5)
Signature
export declare const takeRightBut: (n: number) => <A>(self: Type<A>) => Chunk.Chunk<A>