-
Notifications
You must be signed in to change notification settings - Fork 5
Debugging and tap callbacks #8
Comments
This would be valuable and, being a function, it composes well. I suspect the overwhelming majority of uses of this function will be for Status quo: .map(x => { debugger; return x })
.map(Function.aside(() => { debugger })) Specialised helpers (hopefully with better names!) built atop .map(Function.asideDebug) |
I figure that I’ll add both a |
Not sure if OOP language precedents matter much here. But here goes nothing... In Java 8, we have Stream Processing API, which introduces Stream.of("one", "two", "three", "four")
.filter(e -> e.length() > 3)
.peek(e -> out.println("Filtered value: " + e))
.map(String::toUpperCase)
.peek(e -> out.println("Mapped value: " + e))
.collect(toList()); In Scala, back in 2018, besides introducing val x = 1.pipe(plus1)
.pipe(double)
.tap(res => println(s"DEBUG: x = $res"))
.pipe(double) In Kotlin..., besides reusing
listOf("01", "02", "03")
.asSequence()
.onEach { println(it) }
.filter { it.endsWith("3") }
.any()
// run() + also() if you're `this` lover,
"16384"
.run(String::toDouble)
.run(::sqrt)
.also(::println)
.run(Double::toInt)
// let() + also() if you prefer `it` syntax
"16384"
.let { it.toDouble() }
.also { doSomethingImpure() }
.let { sqrt(it) }
.let { it.toInt() }
.also { println("The end result is: $it") } |
Decided not to include a `debug` helper function `input => { debugger; return input; }` due to lack of library precedent and overlap with `tap`. Closes #8.
@runarberg had a good idea in tc39/proposal-pipeline-operator#225 (comment).
Debugging in callback-heavy code is a pain. This convenience function would be generally useful. Maybe this should be called
Function.debug
or something.My off-work time is currently limited, but I plan to add a section for this sometime in the next few weeks.
Please feel free to comment with more precedents from other languages and libraries here.
The text was updated successfully, but these errors were encountered: