Check out a free preview of the full Hardcore Functional Programming in JavaScript, v2 course
The "Ramda Generalized Currying" Lesson is part of the full, Hardcore Functional Programming in JavaScript, v2 course featured in this preview video. Here's what you'd learn in this lesson:
Brian explains how to use the Ramba library while currying a function, and demonstrates how to write point free functions.
Transcript from the "Ramda Generalized Currying" Lesson
[00:00:00]
>> So let's go ahead and define one last function here before we go into some exercises. We'll call it replace, right? So we'll take our argument order should be the thing we're gonna some like regex, right? The replacement and then finally, the string. The data happens last right?
[00:00:24]
And we'll just go ahead and say string.replace, and I'm just wrapping the internal methods at this point. But you can see the where we're going with this is we go. We have this function, and what's happening here is I can actually say, well why don't I say we have a function now that replaces all, how about the HTML slug from before right?.
[00:00:55]
I can never remember if it's like to look back at this thing. [LAUGH] Yeah, capital W+, you'll have a problem, I'm always like lowercase or uppercase. Actually let's do something even easier. Let's replace vowels right. So we'll just carry replace with a regex that takes AEIOU and it doesn't matter about the case.
[00:01:28]
And we'll replace that with, I don't know, exclamation point. Great. We've curried this function. It's remembering its arguments and we get a new function called replaceVowels. We'll call replaceVowels, on hey, I have words. See if that works does not work [LAUGH] always need to install ram duh. My goodness.
[00:01:54]
Okay, yarn, add ram uou all catch that yarn add randa.
>> Inexplicably, your regex works the first time.
>> There we go. Yeah. Now I got two problems. [LAUGH] So, there we go. That unfortunately doesn't look very nice, but you'all can see that we're replacing the vows. Terrific.
[00:02:17]
And so instead of writing all these functions explicitly working on their data, one thing that you might have noticed is we're not even mentioning data here or here. So we got rid of the argument all together. That's, actually the data we're operating on. And that has big implications for your program.
[00:02:39]
If you're naming all the data with which you're operating on. You're tying all these functions specifically to that domain, incidentally. And you could work on making these abstract names all over your application or you could just get rid of the argument and all together. We'll talk about that more.
[00:02:55]
But that style is hinting at what we call point free, where we actually don't talk about the points or the data in our program. We just combined functions, and then when we actually call it, so there's no variable called string here. I just passed it in and it ran the function.
[00:03:13]
So I didn't have to come up with a name and oftentimes I end up naming things specific to my domain, which is why you end up in this situation with very specific functions. Unless we use-
>> What's your point of view on like the bind method?
>> Yes,
[00:03:28]
>> Binding arguments in.
>> So that's a good that's a good call. It's a way to do currying, as you can use, bind to create a curried function. Let me try to think of a situation where we'd wanna do that. But if you're a reacts programmer, you're probably familiar with binding arguments and it is a way to curry.
[00:03:53]
We don't actually have this almost, in all this stuff we'll see today we're not gonna use this keyword once. So it doesn't actually come up often.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops