Transcript from the "List Iteration" Lesson
>> Kyle Simpson: List Iteration. I have the exclamation marks here because I wanna remind you that even though this is one of those helpers and it oftentimes gets lumped in with the other functional programming things, most people say, don't do this one and you'll see very quickly why. If I made an iterate function, for example, I wanted to take my array and log out all the values in the array.
[00:00:24] Cuz you notice I'm not actually performing an operation on the list to change something, I'm just performing a operation with the value of the list. But what's the only way that doing that would mean anything? Is if my predicate had a side effect, right?
>> Kyle Simpson: Some side effect, whether it's a console.log, or changing it in the original array, like an in place change or something like that.
[00:00:49] The only reason that a list iteration would make any sense and do anything useful to us at all is if the predicate had a side effect. We can probably spot why that's not the favorite tool in a functional programmer's bag. So most functional programmers would tell you never use the forEach, cuz it's all about side effects.
[00:01:09] Now, the problem with that is that sometimes I do want side effects [LAUGH]. Which is why I call this functional programming light, because I don't wanna be restricted to only doing purely functional stuff. There are times when I want to iterate over a list and have some side effects and that's why I use for agent as a tool in my bag, but I use it knowing that it's not the same thing as my other functional tricks.
[00:01:31] It's different and I use it that way on purpose. There are people that will tell you, no no, you should iterate with a map. Can you iterate with map? Of course you can. You can iterate with map where its predicate returns the same value, right? Whatever function you pass as that predicate, if it returns the same thing it got, that's the same thing as iterating, right?
[00:01:53] That's what a functional programmer tells you that's what you ought to do. No. That's not what I would do. ForEach is the better tool if I want to produce a side effect for each thing. So that's the forEach function.
>> Kyle Simpson: All right, any questions about our list operations?
[00:02:14] Those are other tools that we have. So we go back over it. We've gone over a whole bunch of these little basic things. We haven't really fully covered how we put all of them together. That's what exercise four is gonna try to bring some of that to more concreteness and we'll do that after our lunch break.
[00:02:30] But I just wanted to point out that we've covered quite a bit of ground here in just a couple of hours. From the very beginning, we talked about pure versus unpure, and side effects. And then we talked about function composition and how we can compose the function to take it from impure to pure.
[00:02:47] We can compose two functions so that we're not creating impure side effects. Then we talked about immutability and we talked about the value of, not just the mutable value but the immutable way that we use the value. That's the more important part to a functional programmer. Then we talked about closure, keeping state inside of a function.
[00:03:06] We talked about recursion and why recursion can be useful, a graceful way of expressing our programs. Then we talked about talked about these list operations. Transform, exclude. Transform's called map. Exclude's called filter. What's compose called? Reduce. And finally iterate is called forEach.