It can be difficult to debug by examining intermediate values inside Ramda pipelines with console.log.
The Ramda function tap comes in handy here and is like a wiretap: we can use it to tap into a functional pipeline to insert some behaviour in between steps, while letting the pipeline continue to work as usual. We can insert R.tap in between functions inside pipelines built with R.compose or R.pipe.
In diagram form:
A -> B -> C A -> B -> R.tap(fn) -> C
(pipeline proceeds as usual, passing output from B to the input of C)
The following simple example reverses a list and then takes the average (mean).
const R = require('ramda') const f = R.compose( R.mean, // Take average. R.reverse // Reverse elements. ) const result = f([1, 2, 3]) console.log(result)
This prints:
2
Suppose we want to debug this function composition and print out the intermediate reversed list.
We can insert the “wiretap” with R.tap with a function to log the input to see the intermediate value flowing through:
const R = require('ramda') const f = R.compose( R.mean, // Take average. R.tap(x => console.log(x)), // Print and continue. R.reverse // Reverse elements. ) const result = f([1, 2, 3]) console.log(result)
Result:
[ 3, 2, 1 ] 2
This technique can be very handy to debug more complex code using pipe and compose by inserting R.tap.
References