Check out a free preview of the full Functional-Light JavaScript, v3 course

The "Transduction Q&A" Lesson is part of the full, Functional-Light JavaScript, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle fields student questions about whether to use the into helper for multiplication, whether to use transduce for sorting, and whether transduce can be classified as a higher-order reducer.

Preview
Close

Transcript from the "Transduction Q&A" Lesson

[00:00:00]
>> Kyle Simpson: Yes?
>> Speaker 2: So if you were gonna use a less common type of combinator, say for your reducer you wanted to multiply all of them instead of add them, will you just not use into?
>> Kyle Simpson: You wouldn't use into, you would use the general transduce and pass your own in.

[00:00:16]
Yep, into literally calls transduce with the properly selected default combinator. So it's just a shorthand when you're doing a general transducing that it can select for you. Otherwise, you just use the basic transduce method, okay? So what you should pattern match against is, if I start with a couple of slides ago, a couple of maps, maybe a filter, maybe a couple of reduces, pattern match that and say, I need to turn that into a single transduction, okay?

[00:00:44]
And then each of the maps and each of the filters, I need to convert the way that I am converting up here on line 6 and line 7. I need to convert those into this weirdly shaped thing, it's a proto-reducer, it's waiting for a reducer to become a reducer.

[00:00:59]
I need to shape those into that and then compose them together. And that's really actually the hardest part, which is not even that hard at all. But that's the hardest part, is do that and then simply call our transduce helper and it takes care of the rest. Yes?

[00:01:15]
>> Speaker 3: Just how would you compose sorting in something like this? Cuz that's what I always think of is like, filter sort map-
>> Kyle Simpson: Sorting.
>> Speaker 3: Is there something like?
>> Kyle Simpson: You can implement sort as a reduce, but it would be much more complicated. So that wouldn't be typically something that you would transduce over, but it is certainly possible that you could write that as a reducer, yeah.

[00:01:47]
Definitely don't use array.sort, because that's a mutator in place, that one's not FP safe. Essentially the technique that you would use, now that I'm think about it, the technique that you would use for sorting with reduce, is basically the same thing as mapping with reduce. Except that you would be looking at your accumulator and in doing a sort of insertion sort kind of thing, you would be mutating, or you'd be placing stuff in at the appropriate thing.

[00:02:15]
So each time you got a new value, you'd figure out where it went in the new array. And the end result of that would have been like a mapping where everything just got shuffled around. So yeah, that you could absolutely implement it that way.
>> Speaker 4: Is a transducer a higher order reducer then?

[00:02:30]
>> Kyle Simpson: Yes, it is, a transducer is a higher order reducer, that's exactly right. It's probably better than proto reducer, I was struggling to come up with the right way to describe it. It is a higher order reducer, that's exactly what it is.
>> Speaker 4: Or someone else put partial reducer, I don't know?

[00:02:48]
>> Kyle Simpson: I don't think I would call it that.
>> Kyle Simpson: Partial has some baggage in functional programming, I don't know if I would call it that.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now