This course has been updated! We now recommend you take the Functional-Light JavaScript, v3 course.

Check out a free preview of the full Functional-Light JavaScript, v2 course:
The "FPO.js" Lesson is part of the full, Functional-Light JavaScript, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle introduces his FP library, FPO.js, whose methods are all styled to use named-arguments (object parameter destructuring) instead of individual positional arguments.

Get Unlimited Access Now

Transcript from the "FPO.js" Lesson

>> Kyle Simpson: I briefly wanna point out a utility that may be useful in your learning. There are lots of long well-established functional programming libraries out there. Ramda, lodash/fp, and probably a dozen others that you might pick from. So it might seem a little bit of hubris for me to have come along so late to the game and say I can make a new functional programming library.

[00:00:21] What could I possibly have to offer to the game? This library, it looks like it's pronounce F-P-O. It's actually pronounced f-po. It says there on the README, that's how you pronounce this. It's called f-po. I mostly did that because F-P-O has a very specific meaning for designers. It means for placement only and I didn't want that to be confused.

[00:00:43] So this library's pronounced f-po. But there's a very specific problem that I ran into in trying to learn and apply functional programming to my techniques, to my code patterns. And this library was born out of trying to solve that particular problem. So just real quickly I wanna show you some stuff.

[00:00:59] And if you're interested, you might check out the library and see if that's useful to you. So this is a comparison between doing reduce the standard way, the way that a normal functional programming library would provide it, where you have to give it the reducer. You have to give it the initial value and you have to give it the list of values to operate on.

[00:01:22] And that ordering of arguments is set in stone. That's the way the utility's provided to you. If you already know the reducer before the initial value and that before the list, everything's great. But I ran into these situations where I would know the lists now, but I wouldn't know the reducer until later.

[00:01:43] I would want to specialize my reduce function in a different order than the order that the arguments were listed. You might recall earlier in this course, we talked about various argument juggling like flip and reverse args, and stuff like that. And those can get awfully confusing when you string all of those adaptations together just to get the arguments in a different order so that you can specialize them.

[00:02:09] So what FPO does is it provides you a different style of calling which uses an idiom referred to as named-argument style. You'll notice that you pass in an object to the reduce function there on line 10. And you list out the arguments by name according to, in whatever order, because obviously object properties don't matter in terms of order.

[00:02:32] And you notice you can even skip over ones, I don't provide the initial value cuz I don't need it in this scenario. Named-arguments, this idiom, there are some languages that support it natively. JavaScript we can do it syntactically like that and with destructuring. But named-argument style gets around the problem of these arguments are in an inconvenient order for me.

[00:02:55] Or it also gets around the problem of, I wanna skip over this argument entirely. It's a style of programming that allows us to not have that particular problem. The tradeoff is now you need to know what the argument is supposed to be called. Cuz if you call it arr2 instead of arr, the utility isn't gonna find it.

[00:03:13] It's not gonna know what it is. So you do have to know it. But I chose to use a pretty standard set of naming conventions, so that those parameter names are pretty easy to remember. If it's a function, it's almost always called fn. If it's a list, it's almost always called arr for array.

[00:03:30] So it should be fairly straightforward to learn the parameter names. And then you don't have the argument order juggling. This is a comparison here. If I were to do that same thing with a normal functional programming library, we'd have to curry the utility, curry the flip of the partial from the right of this utility.

[00:03:53] We didn't even talk about about partial right, but it's partial from the right-hand side instead of going left to right. So that kind of code works, but man, it's awfully more confusing to me whenever I see that. Whenever you see all that juggling of argument orders. That is what it looks like if you want to do currying with FPO.

[00:04:11] Every function is automatically curried. So here I call FPO.reduce and I provide only the array. And then later I can provide the reducer function and not even provide the initial value. It automatically is curried across object properties. So hopefully that kind of style maybe would be more helpful to you if you find yourself running up against the argument order juggling problem that I did.

[00:04:39] FPO might be a useful library for you.