Transcript from the "Lodash/FP" Lesson
>> Kyle Simpson: All right, everyone take a big deep breath because we have come quite a long way in this course together. Think back to where we started from the function, the idea of what a function is, and what function purity is all about and how to manage that sort of purity.
[00:00:15] And then we talked about closure, and we talked about value immutability, and recursion, and lifting operations, the data structures, and A Synchrony and so many other sorts of topics. All along the way, we've assumed that the presence of a variety of helpers to let us do that work.
[00:00:36] And I thought it was important for us to finish our discussion together by pointing you at a couple of resources along those lines. This is certainly not an exhaustive list, but I wanted to briefly intro or briefly orient you to a couple of the major functional programming libraries that are out there.
[00:00:53] So that's what we are gonna talk about here for just the last couple of minutes together. The first one that we're going to look at is called LODASH/FP. And what you need to know about LODASH/FP it is not low dash, it is low dash where they have adapted all of the functions to behave as if they should in a functional programming library.
[00:01:14] And there are a number of places that I have discovered over time. Where there's some interesting little quirks, and so, if you are going to pick a library, don't just pick a library and then throw the code snippets from these slides out it and just expect it to always work exactly the right way.
[00:01:30] It's really important to read the documentation for whatever library you pick. And LODASH/FP is very lodash/esque but in a in a functional programming flavor. And if you already use Lodash, it makes a lot of sense to switch to using something like LODASH/FP. If you don't use a functional programming library at all, I'm not really sure that this is the one I'd recommend you switch to.
[00:01:53] But if you've already used a lot of Lodash before, this is absolutely the next logical step, okay? So let's take a look, there's a way you can find information, the documentation about Lodash/FP. Let's take a look at a quick example of how we use Lodash/FP. So, we're gonna require and, this is like if we're using something node JS, we're gonna require and Lodash/FP.
[00:02:16] So it's a separate name space in Lodash in the package, that has all of the same methods copied over but then, reshaped to look the way that they're supposed to look for a functional library. So for example, we have something like fp.reduce, and we give it the reducer and the initial value in the array, and then we get a reduced value out of it.
[00:02:36] Exactly the way you'd expect out of a normal reduce. Remember, we've talked about curry a number of times. In Lodash/fp, and this will be the same for the next slide we will look at. If you want to tell it how many values to curry for, the function name is curryN.
[00:02:52] And curryN takes as its first input, the number that you want to curry for, so in this case it's three. And then it takes the function that you wanna curry. Interestingly, if you use curry without the curry in, like if you just use curry. It will automatically try to detect how many arguments the function is expecting, and there are mixed results with whether or not that auto-detection will work or not.
[00:03:19] So most people that use curry use the explicit form where they tell it, no, no, no, I want you to curry for three inputs. So you'll more often than not use something like curryN, but just be aware, if you see curry, it's doing the sort of auto detection kind of thing, okay?
[00:03:34] But f is now, in a three already curry version of our function f. And g is the composition of calling that add(1). So fp.add(1), that add utility that Lodash provides is curried, so we can provide it with only one input, and we're gonna get back what? A unary function, so we're gonna get back an increment that's waiting.
[00:04:05] So we're making a composition between an increment and whatever came back from f (1,4). So we would essentially call f (1,4), and we get back a function that's waiting for one more input, the z input. And we give it that z input the value ten. So 10 is the value z, that goes in, it's multiplied by the four, and then we have the one in there.
[00:04:29] So we have 41 that flows into the increment on line 13, and that's why we get the value 42, okay? So you see all the bits and pieces that we've been talking about through this whole course. You see curried functions, you see reduce, you see compose. All of those same things, the way we would expect, they're provided to with lodash/fp.
[00:04:51] Any questions about that particular library? Definitely check out the documentation to get more familiar with it.