Chunk
A Chunk<A>
represents an ordered, immutable collection of values of type A
. While similar to an array, Chunk
provides a functional interface, optimizing certain operations that can be costly with regular arrays, like repeated concatenation.
-
Immutability: Unlike standard JavaScript arrays, which are mutable,
Chunk
provides a truly immutable collection, preventing data from being modified after creation. This is especially useful in concurrent programming contexts where immutability can enhance data consistency. -
High Performance:
Chunk
supports specialized operations for efficient array manipulation, such as appending single elements or concatenating chunks, making these operations faster than their regular JavaScript array equivalents.
Create an empty Chunk
with Chunk.empty
.
Example (Creating an Empty Chunk)
To create a Chunk
with specific values, use Chunk.make(...values)
. Note that the resulting chunk is typed as non-empty.
Example (Creating a Non-Empty Chunk)
You can create a Chunk
by providing a collection, either from an iterable or directly from an array.
Example (Creating a Chunk from an Iterable)
Chunk.unsafeFromArray
creates a Chunk
directly from an array without cloning. This approach can improve performance by avoiding the overhead of copying data but requires caution, as it bypasses the usual immutability guarantees.
Example (Directly Creating a Chunk from an Array)
To combine two Chunk
instances into one, use Chunk.appendAll
.
Example (Combining Two Chunks into One)
To remove elements from the beginning of a Chunk
, use Chunk.drop
, specifying the number of elements to discard.
Example (Dropping Elements from the Start)
To check if two Chunk
instances are equal, use Equal.equals
. This function compares the contents of each Chunk
for structural equality.
Example (Comparing Two Chunks)
Convert a Chunk
to a ReadonlyArray
using Chunk.toReadonlyArray
. The resulting type varies based on the Chunk
’s contents, distinguishing between empty, non-empty, and generic chunks.
Example (Converting a Chunk to a ReadonlyArray)