I have a doubt related to transduction in functional programming
If I have an operation -
arr.map(A).map(B).filter(C).filter(D).map(E).map(F)
To remove the intermediate creation of arrays, can I rewrite it as -
import _ from "lodash"
const mapReducer = (mapperFn) => (combinerFn) => (acc, val) =>
combinerFn(acc, mapperFn(val));
const filterReducer = (predicateFn) => (combinerFn) => (acc, val) =>
predicateFn(val) ? combinerFn(acc, val) : acc;
const push = (arr, x) => { arr.push(x); return arr }
const transducer = _.flowRight(
mapReducer(_.flowRight(B, A)),
filterReducer(_.flowRight(D, C)),
mapReducer(_.flowRight(F, E))
)
arr.reduce(transducer(push), [])