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

The "FPO" 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 introduces his FPO library, which is meant to be a similar library to Ramda with a different way of dealing with function signatures: by using a named arguments style.

Preview
Close

Transcript from the "FPO" Lesson

[00:00:00]
>> Kyle Simpson: The last library I'm gonna show you is a bit self-serving cuz I'm gonna show you one that I wrote. But you notice that I didn't even recommended that you use this library, I've recommended that you use something like Ramda or Lodash. This library is called FPO and has a very specific use case that it was built for.

[00:00:16]
It was, actually my goal was to create a ramper on top of Ramda. I set out trying to adapt the Ramda functions to do a very specific thing that I wanted. And I got about a third of the way through that work. And I found some implementation details of Ramda that we're gonna make it impossible for me to go forward, and that is the only reason why I and ended up creating a separate thing.

[00:00:40]
So it's really not designed to be radically different from Ramda but just a different way of dealing with the function signatures. So I just wanna talk about this particular used case. If you ever run across this particular problem, maybe you might wanna go look at FPO, but otherwise use something that's much more well-established like a Ramda.

[00:00:58]
The use case I'm talking about is if you are dealing with a function where there is an ordering of the arguments that is inconvenient for you and find yourself doing a bunch of flips or reverses or if you have functions that take parameters that you want to skip over and it's really annoying to use undefined in those parameter positions, there is a different style of programming.

[00:01:26]
It's not as common in JavaScript yet although it's growing and becoming more common these days. There's a style of programming called named arguments. This is different from the parameters that are named. Remember, parameters are in the function signature. Named arguments mean at the call site annotating, I want this argument to go to a parameter of this specific name, that is a feature of some programming languages like objective C and Scala.

[00:01:55]
You literally, at the function call site, can say this value goes to this parameter. And one of the benefits of a named argument is that you can skip over them by just not listing them and you can also list them in whatever order is convenient to you. So you get rid of all those juggling that you often have to do with argument ordering.

[00:02:14]
So what I wanted to do was adapt functional-programming utilities to use the named-argument style that we do in JavaScript. And the way we do named arguments in JavaScript is to pass in an object literal with property names, the way you see on line 10. So what FPO does is exposes all of the common functional programming utilities that you would expect, like the reduces and composes and curries and all that, but it exposes them in a way where it expects you to pass in a single object with named properties, named arguments instead of passing in positional arguments

[00:02:50]
>> Kyle Simpson: So here we see that FPO's.reduce allows us to skip over the initial value because we don't need to provide it, it doesn't clutter up the call site. Another example is when we have some argument order juggling that we're doing, like some kinda crazy flipping of a partial right of this utility or something like that.

[00:03:11]
When we have these weird sorts of contortions that we have to go through to rearrange our argument ordering, sometimes a named argument style is better. So here with FPO, I just tell it, listen, I wanna start with the array, I don't need to do like a flip of a partial right to start with the array, and let me provide the function later.

[00:03:31]
So FPO's style of curried implementation is named argument aware, which means you can pass in an object with whatever property named arguments you want and it's waiting for the rest of those arguments on the next column.
>> Kyle Simpson: I released this library a little like a year and a half ago and I got a whole bunch of people that were like, this is awesome, there are people like Red and others like talking about it, that we're all like scholar programmers.

[00:03:57]
Because they were trying to do functional programming in JavaScript and this was like, wow, this is really cool cuz it uses named arguments the same way we do in Scala. So, it's not ever gonna be as popular as like Ramda or a Lodash/fp or something but it's solving a very specific problem, which is if you wanna do functional programming and you want to avoid some of those weirdnesses or quirks around positional arguments, you can use the named argument style with FPO.

[00:04:23]
Just one last little note on it. FPO's namespace has all those methods with this named argument style but it also provides FPO.STD standard, which has all of the same method names but done in the original style you'd expect, just like in Ramda. So you should have, you have both of those in the same library if you ever end up wanting to use that, okay?

[00:04:46]
So, definitely go check out the documentation for something like Lodash/fp, check out the documentation for Ramda, those are great places to start, really great places. If you ever run across a need for a named argument style, you can check out FPO in the documentation on that. But these are a great resource for you to get started playing with your functional programming.

[00:05:06]
Unfortunately, in JavaScript, we don't really have these things built into the language like you would in something like Haskell. So we're, we are relegated to using libraries. But the good news is, that means that it puts the choice back in your court, you get to decide which library works best for your particular scenario.

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