Today my colleague Federico introduced me to the functional programming library of lodash. We were using fp.compose
.
Here’s how we used it:
We had a JavaScript object which first needed to be transformed into a different shape and then we needed to transform the result of that again, into data that our frontend could display.
With fp.compose
we could pass it the two transformation functions and it would apply one after the other.
Without lodash fp.compose, it would be this:
secondTransformation(firstTransformation(theData))
With lodash fp.compose, we can do this:
fp.compose(secondTransformation, firstTransformation)(theData)
So that’s cool! I just found the order a bit counter intuitive.
Luckily I came across fp.pipe
which does the same thing but in the opposite direction. So the data flows from left to right.
fp.pipe(firstTransformation, secondTransformation)(theData)
I think that’s easier to understand.
Kind of like piping data through a list of functions in Elixir.
Or even piping in bash for that matter!
That’s probably something that most developers will be familiar with, so I feel more comfortable using the fp.pipe
function in our code. Easier to understand!