Do you think if pipeline programming is good to pu...
# codereview
e
Do you think if pipeline programming is good to put everywhere like this? I'm just so obsessed with it. It makes the code so clear, simple and straightforward. IMO. Like this: do steps one, two, three. And that's it. But one can say that the 3 usages of
.let
are redundant here (because there is no
null
to handle using
?.let
). Still this disadvantage does not outweight. IMO
o
You can even do
.let(::File)
and
.let(::println)
🔥 1
e
agree, looks cleaner
a
It’s a matter of personal taste. I don’t like this because it is harder to place breakpoints when debugging. On the other hand, you don’t have to come up with intermediate names . It depends a bit on the context. There’s a place and a time for it, as is with all these things ;)
👍 1
c
I think it's more readable as long as it's only simple function calls.
.let(::File)
is fine to me, but it starts getting smelly as soon as a
.let
is inside another
.let
. To me that's a sign that the outer one should be a regular function call, not chained.
👍 2
1
k
On a related note, I find it slightly unstylish that the final
.let
is not in the same column position as the previous `.let`s, due to the braces.
👍 1
By the way, in general it's inadvisable to turn a
URI
into a
File
. If the URI points to an entry in a jar file, then it's not a
File
and you'll get an exception if you try to read from it like that. You can instead read it directly using
getResourceAsStream()
and turning it into a
Reader
. You should then ensure it gets closed after reading, by using the
use
function. Also, there are some ways to simplify the code, for example using
sumOf
. So your initial code can be written like this:
Copy code
({}.javaClass.classLoader.getResourceAsStream("input3.txt") ?: return)
    .reader()
    .use { it.readText() }
    .let { regex.findAll(it) }
    .map { it.destructured }
    .sumOf { (a, b) -> a.toInt() * b.toInt() }
    .let { println(it) }
🔥 2
e
Thank you @Klitos Kyriacou Your approach is super elegant I didn't even thought of denesting my code like this