Going down the functional rabbit hole, I think it would be nice to be able to have the jump condition of control flow be determined via a lambda.
Rather than declaring a ‘condition variable’, instead have it be the last line of a lambda, just like a if/else
statement or call to .let.
This is the piece of code that got me thinking about this:
// val fis: FileInputStream = ...
var length: Int // Keep track of the number of read
val buf = ByteArray(1024)
while (fis.read(buf).also { length = it } > 0){
processBytes(buf, length)
}
This is the functionally I’m going for:
val buf = ByteArray(1024)
while { fis.read(buf).also { length -> processBytes(buf, length) } > 0 } // doesn't work
For the following demonstates what I’m going for
fun during(predicateBlock: () -> Boolean) { while (predicateBlock()) { } }
// now we can just have the lambda return the condition, no declaring a 'useless' variable
during { fis.read(buf).also { processBytes(buf, length) } > 0 }
// technically this is equivalent to a do {} while (predicateBlock()) however
Thoughts/Opinions?