pragmatic-fp-ts
Version:
Opinionated functional programming library with easy use in mind
21 lines (16 loc) • 588 B
text/typescript
import { getValue, getValueOr } from "./main.ts";
export function intersperse<A>(elem: A, coll: A[]): A[];
export function intersperse<A>(elem: A): (coll: A[]) => A[];
export function intersperse<A>(elem: A, coll?: A[]) {
if (arguments.length === 1) return (_coll: A[]) => intersperse(elem, _coll);
const theElem = getValue(elem);
const theColl = getValueOr([], coll);
const result = [];
for (let i = 0; i < theColl.length - 1; ++i) {
result.push(theColl[i], theElem);
}
if (theColl.length > 0) {
result.push(theColl[theColl.length - 1]);
}
return result;
}