import type { Option } from "./Option.js" import * as Chunk from "@fp-ts/data/Chunk" export * from "@fp-ts/data/Chunk" /** * @tsplus pipeable fp-ts/data/Chunk sortWith */ export function ChunksortWith( ...ords: NonEmptyArguments> ): (a: Chunk.Chunk) => Chunk.Chunk { // TODO return as => as.toArray.sortWith(...ords).toChunk } /** * @tsplus fluent fp-ts/data/Chunk groupByT */ export function groupByTChunk_(c: Chunk.Chunk, f: (a: A) => Key) { return c.toReadonlyArray().groupByT(f).toChunk } /** * Returns the first element that satisfies the predicate. * * @tsplus static fp-ts/data/Chunk.Ops findFirstMap * @tsplus pipeable fp-ts/data/Chunk findFirstMap */ export function findFirstMap( f: (a: A) => Option ) { return (as: Chunk.Chunk) => { const ass = as.toReadonlyArray() const len = ass.length for (let i = 0; i < len; i++) { const v = f(ass[i]!) if (v.isSome()) { return v } } return Opt.none } } /** * @tsplus getter fp-ts/data/Chunk toArray */ export function toArray(c: Chunk.Chunk) { return c.toReadonlyArray() } /** * Remove duplicates from an array, keeping the first occurrence of an element. * * @tsplus static fp-ts/data/Chunk.Ops uniq * @tsplus pipeable fp-ts/data/Chunk uniq */ export function uniq(E: Equal) { return (self: Chunk.Chunk): Chunk.Chunk => { let out = ([] as A[]).toChunk for (let i = 0; i < self.length; i++) { const a = self.unsafeGet(i) if (!out.elem2(E, a)) { out = out.append(a) } } return self.length === out.length ? self : out } } /** * Test if a value is a member of an array. Takes a `Equivalence` as a single * argument which returns the function to use to search for a value of type `A` * in an array of type `Chunk`. * * @tsplus static fp-ts/data/Chunk.Ops elem2 * @tsplus pipeable fp-ts/data/Chunk elem2 */ export function elem(E: Equal, value: A) { return (self: Chunk.Chunk): boolean => { const predicate = (element: A) => E.equals(element, value) for (let i = 0; i < self.length; i++) { if (predicate(self.unsafeGet(i)!)) { return true } } return false } } /** * @tsplus pipeable fp-ts/data/Chunk partition */ export const ChunkPartition = Chunk.partition