// ets_tracing: off
import * as O from "../../../../Option/index.js"
import * as Chunk from "../core.js"
import * as ChunkDef from "../definition.js"
/**
* Returns a filtered, mapped subset of the elements of this chunk.
*/
export function collectWithIndex_(
self: Chunk.Chunk,
f: (index: number, a: A) => O.Option
): Chunk.Chunk {
ChunkDef.concrete(self)
switch (self._typeId) {
case ChunkDef.ArrTypeId: {
const array = self.arrayLike()
let dest = Chunk.empty()
for (let i = 0; i < array.length; i++) {
const rhs = f(i, array[i]!)
if (O.isSome(rhs)) {
dest = Chunk.append_(dest, rhs.value)
}
}
return dest
}
default: {
return collectWithIndex_(self.materialize(), f)
}
}
}
/**
* Returns a filtered, mapped subset of the elements of this chunk.
*
* @ets_data_first collectWithIndex_
*/
export function collectWithIndex(
f: (index: number, a: A) => O.Option
): (self: Chunk.Chunk) => Chunk.Chunk {
return (self) => collectWithIndex_(self, f)
}