Check out a free preview of the full Hardcore Functional Programming in JavaScript, v2 course

The "Creating Programs with Curry & Compose" 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 demonstrates how to build a simple program using currying and compose, and argues that this way of writing code leads to consistent state mutations, but not efficient programming because of the absence of a clean data pipeline.

Preview
Close

Transcript from the "Creating Programs with Curry & Compose" Lesson

[00:00:00]
>> As you can see a composer is prettypracticed full. We had add right? Add takes an x and a y and each of this functions are in composition, it needs to be a function that takes one argument. And so if you actually need to call add in the middle of this thing, you're going to have to preload it with something.

[00:00:23]
So we'll add like the exclamation point here instead of exclaim, we can get rid of exclaim or we could define it in terms of add. Of course we'll have to curry add to be able to do that. And there we go, now currying is paying for itself, right?

[00:00:38]
It's gonna start with the left side first, right? So, let's flip the order here. Actually let's just go ahead and define a concat, a proper concat for all of us to use. [LAUGH] So, which we can do a flip add again. Love that flip, usually it doesn't come in handy that much.

[00:00:55]
[LAUGH] So, we'll concat it on to the end. There we go. So you can see, using currying and composition they play together because you're able to make every function into a unary function, a function that takes one argument instead of a binary or more. And so as our data flows through our pipeline, we make everything to one arg at a time.

[00:01:22]
So when we look at this, we can quickly see kind of what's happening here is we're going to hit the YouTube API and grab that property off the Jason, and grab the first, grab the URL, pass that into the feed. You can kinda see how these functions, it looks like nothing's happening, but this just giant pipelines of data to flow through each taking one argument and that's your data.

[00:01:48]
So you have this data structure that you're maintaining that's flowing through your pipe. That's kind of the fundamental shift from the other paradigms. And if we kinda look at this, so here's a composition, I call it do your stuff. It's, out on the App Store. You can download it.

[00:02:07]
[LAUGH] But basically, we're gonna start down at the bottom, working our way up, right, it's right to left. So we're gonna turn something to lowercase and then split it and then trim it and reverse it and grab anything greater than three and then join it all together. Fun.

[00:02:23]
So down to the bottom, these little blocks, you think our data flowing in and then it you know gets lowercased, each of these blocks would be like a function call. I didn't have enough to really match it up. And you can see as it flows through our program, the data is being manipulated and we get to our result, right?

[00:02:45]
So, one thing to keep in mind is, if I break the chain, that′s it, we′re done. Everything must be composed, everything must be connected. If I don′t actually flow through this pipeline, by definition of a function that takes input to output, if I don′t then pass that output into another input, that's the end of the programme.

[00:03:09]
That'll be the return value. So, in this way, what you're doing is taking a very complex, you know graph of procedures and operations and kind of wrangling it into a pipeline of data processing. And we can get more advanced and do really interesting things with this once we'll start after lunch on that.

[00:03:30]
But this is kind of fundamentally what we're gonna do is pipe data through. This is the same function [LAUGH] written as, I would say like as bad, well it's not bad, as performance as I could write it. [LAUGH] But I see this stuff like every single day. I work with programmers that come from the C++ background and whatever else and this is very comfortable but what's happening is, and if you want to get into performance, we can make this extremely performant, probably way more performant than this.

[00:04:06]
And we could have a coffee and talk that out. But what you're seeing here is state manipulations, through and through, nothing is connected, nothing can be connected. It's like all we're doing is introducing variables and mutating variables. This whole state of California and flipped it for mutation, and the idea is that like you know, we got more state and more state, first we define lower then we define words and then we reverse the words we mutate them.

[00:04:32]
Then we go through and change them and as we reach the end of our program, we're just like throwing trash out into the global environment like all these are still available for me and they're even changing. So this is kind of harder to deal with than the data flowing through, because state is defined as one of the hardest things in programming to manage, and keep in your head.

[00:05:05]
This is really poorly, don't worry about this one. It's just a class as an app mailer, but if you want to look at how this class would be defined in an object oriented sense, it's a graph of state changes. Your object oriented programming takes all that state, blows it up for encapsulation and then it mutates the state in all different places, so you think it looks really clean and nice but it still a [LAUGH] tiny graph of state mutations, right?

[00:05:28]
Its not a clean data pipeline through functions, its a graph of data changes. Now you could always write an object oriented programme not in terms of functions, and that's totally fine. It's a false dichotomy there. You could totally use object oriented stuff for this. But that's what you'll typically see, right?

[00:05:49]
I should know. Yes.
>> Going back to your diagram, would it be fair to say since we're dealing with an undirected graph that we're essentially looking at the possibility of state mutation. So the problem with the output is it's gonna fall somewhere in this massive interconnections.
>> That's a great to put it cuz the composition could totally be a graph.

[00:06:12]
It's just a directed graph.
>> Yes.
>> And yes, [CROSSTALK] Exactly. And we'll talk about kind of wrangling things into a linear control flow with phuncters, the identity phuncters soon, but yes, totally agree.

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