Brian explains the foundations of functional programming, and talks about why using functional programming can help you create code that's more maintainable, composable, and easier to reason about. He also talks about using higher-order functions, and how to avoid side effects.
- Now we're gonna do some functional programming. And in my opinion, most of the stuff that I have shown you so far is very useful for interviews and little else for the most part. Like it is useful to know because its useful to make those trade-offs in your head but they're not actually gonna help you in your day-to-day programming. However, this functional programming stuff that I'm about to show you does help you everyday. This made me a much better programmer once I learned how to do this stuff. Or even if you're doing stuff like MongoDB, MapReduce that's all based on the same concepts that we're doing here.
Well to be totally honest with you the key is both the key and the value because we're doing a collection. This isn't a map. This is a collection/set so the key and the value in this particular case end up being the same thing. Good question though. Okay so functional programming. Functional programming is a wide, wide, idea that I personally have only scratched the surface of.
That's the expectation with avoiding side effects. This is really, really nice because now this function is very testable. I can rip out this function and test it as much as I want and I can be guaranteed that this function does what it is supposed to do and that if I inserted in some other scenario its only taking in input and giving out output. It is not doing anything else so thus its very easy to know what goes on with the function. So that's why avoiding side effects is very important.
Second we're gonna focus on higher order functions which basically the idea that we're gonna have functions that apply other functions. So if you remember back, we were doing the that test method when we were doing the tree. That's actually a higher order function that's taking in that test method and applying it. So at that point we can pass in different testing functions and get the same results. That's a higher order function that's taking in one function and applying it.
This will make more sense once we start talking about the actual higher order functions that we're gonna deal with. And thirdly we're gonna be transforming lists of data which is something very, very common to functional and eventually reactive programming that all you're doing is taking the one list, doing something to it and giving you back a new list of transformed values. What's really cool about functional programming is that we begin to describe what we want as opposed to how we want it to happen and I think that's a really powerful concept.
Its declarative versus imperative programming. We're declaring that this is gonna happen and then this is gonna happen then this gonna happen and then this is what I'm gonna get, as opposed to say step one is this, step two is this, step three is that. That's kind of the idea behind functional programming is its what I have right here. Its maintainable. Its composable and its easy to reason about. That I make these different kind of building blocks and the building blocks can be rearranged and used in different order and used in different ways and recomposed with each other and then I can test each building block super well so that I know when I go back to reach for this other component its already built and its already super well tested and its just very maintainable and very happy.
Note: This course was created by Frontend Masters. It was originally released on 07/12/2016. We're pleased to host this training in our library.
- Big O
- Sort: Bubble, insertion, and merge,
- Median values
- Set, map, stack, and queue
- Array lists and linked lists
- Binary search tree
- AVL tree
- Single rotation and double rotation
- Hash table
- Functional programming
- Map, reduce, and filter