pragmatic-fp-ts
Version:
Opinionated functional programming library with easy use in mind
20 lines (15 loc) • 448 B
text/typescript
import { equals, getValueOr } from "./main.ts";
// Remove successive repeats in a list, leaving only one copy
export function dropRepeats<A>(coll: A[]) {
const theColl = getValueOr([], coll);
const result: A[] = [];
let lastVal: A;
for (let i = 0; i < theColl.length; ++i) {
const nextVal = theColl[i];
if (i === 0 || !equals(nextVal, lastVal!)) {
result.push(nextVal);
}
lastVal = nextVal;
}
return result;
}