Cómo encadenar las funciones de mapa y filtro en el orden correcto

Me gusta mucho encadenarArray.prototype.map, filter yreduce para definir una transformación de datos. Desafortunadamente, en un proyecto reciente que involucró grandes archivos de registro, ya no pude evitar recorrer mis datos varias veces ...

Mi meta:

Quiero crear una función que encadene.filter y.map métodos, en lugar de mapear sobre una matriz de inmediato, componiendo una función que recorra los datosuna vez. Es decir.:

const DataTransformation = () => ({ 
    map: fn => (/* ... */), 
    filter: fn => (/* ... */), 
    run: arr => (/* ... */)
});

const someTransformation = DataTransformation()
    .map(x => x + 1)
    .filter(x => x > 3)
    .map(x => x / 2);

// returns [ 2, 2.5 ] without creating [ 2, 3, 4, 5] and [4, 5] in between
const myData = someTransformation.run([ 1, 2, 3, 4]); 
Mi intento:

Inspirado poresta respuesta yeste blog Empecé a escribir unTransduce función.

const filterer = pred => reducer => (acc, x) =>
    pred(x) ? reducer(acc, x) : acc;

const mapper = map => reducer => (acc, x) =>
    reducer(acc, map(x));

const Transduce = (reducer = (acc, x) => (acc.push(x), acc)) => ({
    map: map => Transduce(mapper(map)(reducer)),
    filter: pred => Transduce(filterer(pred)(reducer)),
    run: arr => arr.reduce(reducer, [])
});
El problema:

El problema con elTransduce fragmento anterior, es que se ejecuta "hacia atrás" ... El último método que encadena es el primero en ejecutarse:

const someTransformation = Transduce()
    .map(x => x + 1)
    .filter(x => x > 3)
    .map(x => x / 2);

// Instead of [ 2, 2.5 ] this returns []
//  starts with (x / 2)       -> [0.5, 1, 1.5, 2] 
//  then filters (x < 3)      -> [] 
const myData = someTransformation.run([ 1, 2, 3, 4]);

O, en términos más abstractos:

Ir desde:

Transducer(concat).map(f).map(g) == (acc, x) => concat(acc, f(g(x)))

A:

Transducer(concat).map(f).map(g) == (acc, x) => concat(acc, g(f(x)))

Que es similar a:

mapper(f) (mapper(g) (concat))

creo entenderpor qué sucede, pero no puedo entender cómo solucionarlo sin cambiar la "interfaz" de mi función.

La pregunta:

¿Cómo puedo hacer miTransduce cadena de métodofilter ymap operaciones en el orden correcto?

Notas:Solo estoy aprendiendo sobre cómo nombrar algunas de las cosas que estoy tratando de hacer. Avíseme si he utilizado incorrectamenteTransduce término o si hay mejores formas de describir el problema.Soy consciente de que puedo hacer lo mismo usando un anidadofor lazo:

const push = (acc, x) => (acc.push(x), acc);
const ActionChain = (actions = []) => {
  const run = arr =>
    arr.reduce((acc, x) => {
      for (let i = 0, action; i < actions.length; i += 1) {
        action = actions[i];

        if (action.type === "FILTER") {
          if (action.fn(x)) {
            continue;
          }

          return acc;
        } else if (action.type === "MAP") {
          x = action.fn(x);
        }
      }

      acc.push(x);
      return acc;
    }, []);

  const addAction = type => fn => 
    ActionChain(push(actions, { type, fn }));

  return {
    map: addAction("MAP"),
    filter: addAction("FILTER"),
    run
  };
};

// Compare to regular chain to check if 
// there's a performance gain
// Admittedly, in this example, it's quite small...
const naiveApproach = {
  run: arr =>
    arr
      .map(x => x + 3)
      .filter(x => x % 3 === 0)
      .map(x => x / 3)
      .filter(x => x < 40)
};

const actionChain = ActionChain()
  .map(x => x + 3)
  .filter(x => x % 3 === 0)
  .map(x => x / 3)
  .filter(x => x < 40)


const testData = Array.from(Array(100000), (x, i) => i);

console.time("naive");
const result1 = naiveApproach.run(testData);
console.timeEnd("naive");

console.time("chain");
const result2 = actionChain.run(testData);
console.timeEnd("chain");
console.log("equal:", JSON.stringify(result1) === JSON.stringify(result2));

Aquí está mi intento en un fragmento de pila:

const filterer = pred => reducer => (acc, x) =>
  pred(x) ? reducer(acc, x) : acc;

const mapper = map => reducer => (acc, x) => reducer(acc, map(x));

const Transduce = (reducer = (acc, x) => (acc.push(x), acc)) => ({
  map: map => Transduce(mapper(map)(reducer)),
  filter: pred => Transduce(filterer(pred)(reducer)),
  run: arr => arr.reduce(reducer, [])
});

const sameDataTransformation = Transduce()
  .map(x => x + 5)
  .filter(x => x % 2 === 0)
  .map(x => x / 2)
  .filter(x => x < 4);
  
// It's backwards:
// [-1, 0, 1, 2, 3]
// [-0.5, 0, 0.5, 1, 1.5]
// [0]
// [5]
console.log(sameDataTransformation.run([-1, 0, 1, 2, 3, 4, 5]));

Respuestas a la pregunta(2)

Su respuesta a la pregunta