We talk a lot about composition, but what does it mean? Each domain (action, calculation, data) has its own way to compose. In this quick episode, I explain each one.
Eric Normand: What do we mean when we talk about composition about composing things? My name is Eric Normand. These are my thoughts on functional programming.
Just one little note, behind me, what you’re seeing is Portland, Oregon. I’m in town for a conference. Just thought you might want to know.
What is composition? Someone was asking me today because I used the term without defining it. I’ve been thinking about how to explain it, explain what I mean. I think it means different things depending on the kind of thing you’re composing. Let’s start with the easy ones. When you compose actions, there’s really just two ways I think, to compose two actions.
Composition is really a binary operation. You take two things and you put them together. When we have two actions and we put them together there’s two ways to do it. One is you can run them in order in a sequence. You do one action and then the other action.
The other thing is you could run them in parallel. You can run them both at the same time and when you run them in sequence you can make it so that the results of one action…
Let’s say reading from the file system is passed to the next action as an argument and that lets you do different things depending on what the results of the first one was. Now, when you’re running in parallel, one thing you could do is get both of the answers back in something like a tuple if there are results that you need to do.
Otherwise, you could just run them in parallel and compose them that way. You can see you can compose up things in sequence and then have two things that you compose up running parallel. You have two different timelines running with different sequences going. You build it up, you compose it up, two things at a time.
Of course, we talked about how two actions, when you compose them, give you a new action. If you compose up only actions, you only have actions.
Now, what about calculations? There’s a thing in math called function composition and it’s similar. I think that it’s more general than the one thing called function composition. That function composition is you run a function and the only reason to run a function is to get its return value. It’s to get the result of that calculation.
You run a calculation, you get the result and then you pass that result to another calculation as one of the arguments. Then you get another result. You’re taking two calculations and you’re running one first and then running the next one on the results of that.
Again, it’s binary. It takes two calculations and returns a new calculation. That calculation runs the first one, gets the output of it and passes it to the second one as one of its argument. The function composition that mathematicians talk about is one where the second function has one argument and that’s the thing that the output goes to.
I really think that’s a very limited view. You could have other arguments in there. That’s function, that’s composing calculations.
Now, what about data? Data is actually the more complex here because in data you can compose in so many different ways. It really depends on the data structure. Whatever structure you’re using, tells you how you can compose. If you’re using a list, you can compose a new thing on to that list using append or maybe you are adding to the front.
You could be adding to the front, you can be adding to the end. You’re composing in that way, you start with an empty one in you compose on a new thing. Now, if you’re using something like a set, you’re composing without order or you have this item potency thing that your compose operation is idempotent.
If you add the same thing twice, it doesn’t add it the second time. It only remembers one time or if you have something like a HashMap or, associative array or whatever you want to call that, the semantics are that you need to keep value pair so, you composed using that.
When you compose data, it’s really about the same thing as a binary operation. You’re taking two pieces of data and you’re putting them together in a new piece of data. How that works depends on the two pieces of data and their semantics. That’s why it’s more complex.
It’s more complicated because data represents relationships and there are a lot of different relationships that you need to represent. There’s different shapes of data with different properties for those shapes.
I think that explains it pretty well. It’s a pretty general concept of composing things together. I should explain it obviously because I talk about it a lot.
If you have any questions or you want to get in touch, I love getting this question. Please feel free to get in touch with me on Twitter, I’m @ericnormand. My email is firstname.lastname@example.org. I love getting emails and responding to them. Please get in touch with me there.
As always, these were my thoughts on functional programming. Please subscribe and like it. I’ll see you later. Bye.