A quick introduction to pipe() and compose() in JavaScript
Functional programming’s been quite the eye-opening journey for me. This post, and posts like it, are an attempt to share my insights and perspectives as I trek new functional programming lands.
Ramda’s been my go-to FP library because of how much easier it makes functional programming in JavaScript. I highly recommend it.
Pipe
The concept of pipe
is simple — it combines n
functions. It’s a pipe flowing left-to-right, calling each function with the output of the last one.
Let’s write a function that returns someone’s name
.
getName = (person) => person.namegetName({ name: 'Buckethead' })
// 'Buckethead'
Let’s write a function that uppercases strings.
uppercase = (string) => string.toUpperCase()uppercase('Buckethead')
// 'BUCKETHEAD'
So if we wanted to get and capitalize person
's name, we could do this:
name = getName({ name: 'Buckethead' })
uppercase(name)// 'BUCKETHEAD'
That’s fine but let’s eliminate that intermediate variable name
.
uppercase(getName({ name: 'Buckethead' }))