// ets_tracing: off
/**
* adapted from https://github.com/gcanti/fp-ts
*/
import "../../../Operator/index.js"
import type { Predicate, Refinement } from "../../../Function/core.js"
import { identity } from "../../../Function/core.js"
import type { Option } from "../../../Option/index.js"
import { isSome, none, some } from "../../../Option/index.js"
import type { MutableArray } from "../../../Support/Mutable/index.js"
import type { NonEmptyArray } from "../NonEmptyArray/index.js"
import * as Tp from "../Tuple/index.js"
export type Array = ReadonlyArray
/**
* Composes computations in sequence, using the return value of one computation to determine the next computation.
*
* @ets_data_first chain_
*/
export function chain(f: (a: A) => Array) {
return (ma: Array): Array => chain_(ma, f)
}
/**
* Composes computations in sequence, using the return value of one computation to determine the next computation.
*/
export function chain_(fa: Array, f: (a: A) => Array): Array {
let resLen = 0
const l = fa.length
const temp = new Array(l)
for (let i = 0; i < l; i++) {
const e = fa[i]!
const arr = f(e)
resLen += arr.length
temp[i] = arr
}
const r = Array(resLen)
let start = 0
for (let i = 0; i < l; i++) {
const arr = temp[i]
const l = arr.length
for (let j = 0; j < l; j++) {
r[j + start] = arr[j]
}
start += l
}
return r
}
/**
* Splits an array into length-`n` pieces. The last piece will be shorter if `n` does not evenly divide the length of
* the array. Note that `split(n)([])` is `[]`, not `[[]]`. This is intentional, and is consistent with a recursive
* definition of `split`; it satisfies the property that
*
* ```ts
* split(n)(xs).concat(split(n)(ys)) == split(n)(xs.concat(ys)))
* ```
*
* whenever `n` evenly divides the length of `xs`.
*
* @ets_data_first split_
*/
export function split(n: number): (as: Array) => Array> {
return (as) => split_(as, n)
}
/**
* Splits an array into length-`n` pieces. The last piece will be shorter if `n` does not evenly divide the length of
* the array. Note that `split(n)([])` is `[]`, not `[[]]`. This is intentional, and is consistent with a recursive
* definition of `split`; it satisfies the property that
*
* ```ts
* split(n)(xs).concat(split(n)(ys)) == split(n)(xs.concat(ys)))
* ```
*
* whenever `n` evenly divides the length of `xs`.
*/
export function split_(as: Array, n: number): Array> {
const f = chop(splitAt(n))
return as.length === 0 ? empty>() : isOutOfBound(n - 1, as) ? [as] : f(as)
}
/**
* Filter out optional values
*/
export function compact(fa: Array