rubico
🏞 a shallow river in northeastern Italy, just south of Ravenna
functional programming for humans
Why?
- monads are hard to explain
- but I still want to program functionally
well you can, and rubico can help you do it. And no, you do not need to know what a monad is.
Okay, show me some stuff
rubico's flow
chains functions, sync or async, together. Let's get some json data.
flow(
fetch,
response => response.json(),
console.log,
)('https://jsonplaceholder.typicode.com/todos/1')
// > { userId: 1, id: 1, title: 'delectus aut autem', completed: false }
No variables. the above is a more expressive version of
void (async (url) => {
const response = await fetch(url)
const data = await response.json()
console.log(data)
})('https://jsonplaceholder.typicode.com/todos/1')
// > { userId: 1, id: 1, title: 'delectus aut autem', completed: false }
Chaining functions is cool, but you don't need a whole library to do that.
Let's make a post request. This time, rubico is namespaced to _
.
// url (string) => body (object) => response (object)
const makePostRequest = url => _.flow(
JSON.stringify,
_.diverge([
url,
_.diverge({
method: 'POST',
body: _.id,
headers: {
"Content-type": "application/json; charset=UTF-8",
},
}),
]),
_.spread(fetch),
response => response.json(),
)
const postToHttpBin = makePostRequest('https://httpbin.org/post')
_.flow(postToHttpBin, _.get('json'), console.log)({ a: 1 })
What's going on at a high level:
- make a post request with body
{ a: 1 }
to http bin (postToHttpBin
) - get the
json
prop of that response (_.get('json')
) - log that value out to the console
What's going on in postToHttpBin
:
- partially apply 'https://httpbin.org/post' to
makePostRequest
,
a higher order function that takes a url
What's going on in makePostRequest after applying url
:
- json stringify the input body (
JSON.stringify
) - create a structure that resembles
[url, { method, body, headers }]
(_.diverge
) - spread that structure as arguments into fetch (
_.spread(fetch)
) - format the response payload (
response => response.json()
)
The powerful idea here is functions as modules; that you can solve a problem once,
name it something you can remember, and use it anywhere you see fit.
These examples are but the tip of the iceburg. For more examples, see the examples
folder.
For the full documentation, please visit https://rubico.land