@applicaster/zapp-react-native-utils
Version:
Applicaster Zapp React Native utilities package
24 lines (20 loc) • 634 B
text/typescript
import { curry } from "lodash";
/**
* A native reimplementation of Ramda's mapAccum.
*
* @template A, B, C
* @param {(acc: A, value: B) => [A, C]} fn - Function returning [newAcc, mappedValue]
* @param {A} acc - Initial accumulator
* @param {B[]} list - List to process
* @returns {[A, C[]]} - Tuple of [final accumulator, mapped array]
*/
export const mapAccum = curry((fn, acc, list) => {
const result = [];
let currentAcc = acc;
for (let i = 0; i < list.length; i++) {
const [nextAcc, mapped] = fn(currentAcc, list[i]);
currentAcc = nextAcc;
result.push(mapped);
}
return [currentAcc, result];
});