Transduce over any Iteratable Collection

InstructorPaul Frend

Share this video with your friends

Send Tweet

So far we've been transducing by manually calling .reduce() on arrays, but we want to be able to transduce over other collection types as well.

In this lesson we'll create a transduce function to support transducing over any data structure that implements the es2015 iterable protocol. We’ll put it to the test by transducing over strings and maps, as well as going from one collection type as input to another as output.

Anthony Ershov
~ 7 years ago

Hi, Paul! The transduce function works perfectly with arrays but there are some issues with strings: http://take.ms/e7fve

I guess your imported functions from './utils' differ from functions in the previous video. My code is here https://stackblitz.com/edit/react-b77tn4

Bijoy Thomas
~ 7 years ago

Hello Anton, your map and filter functions are missing the return statements. They should be like:

const map = (xf) => reducer => {
    return (acc, val) => {
      // for string concat, the original string is NOT modified,
      // unlike the push reducer which mutates the array
        return reducer(acc, xf(val)); 
    };
};

const filter = (predicate) => reducer => {
    return (acc, val) => {
        if (predicate(val)) return reducer(acc, val); // same reason here
        return acc;
    };
};
Anthony Ershov
~ 7 years ago

Bijoy, thanks a lot! Really helpful!

Bijoy Thomas
~ 7 years ago

Glad it helped!

Paul Frendinstructor
~ 7 years ago

Thanks for responding Bijoy! Yep that's spot on, you should not be relying on the fact that the push reducer mutated the array. I never added the return statement into the map function in lesson 4, but it should be there (and is there in the version imported from utils.js). I thought I flagged this error here already but it must have been in a twitter response, so sorry for the confusion.

sebastian
~ 7 years ago

hi @paul, what editor/plugin do you use to see the answers to your functions using / *? * /

Paul Frendinstructor
~ 7 years ago

hi @paul, what editor/plugin do you use to see the answers to your functions using / *? * / hey @sebastian, it's called quokka.js