Philipp Mayer
07/08/2020, 10:11 AMval newSmth = listOfObjects.filter { ... }
if (newSmth.isNotEmpty()) {
otherWorkflow.process(newSmth)
}
Jonathan Mew
07/08/2020, 10:39 AMfun <E> List<E>.applyIfNotEmpty(action: (List<E>) -> Unit) {
if(size > 0) {
action(this)
}
}
so you can
objs.filter { it == 'a' }
.applyIfNotEmpty (otherWorkflow::process)
There might well be something nice OOTB thougharaqnid
07/08/2020, 10:52 AMlistOfObjects.filter { … }.let { if (it.isNotEmpty()) otherWorflow.process(it) }
?araqnid
07/08/2020, 10:52 AMlistOfObjects.filter {…}.takeUnless { it.isEmpty() }?.let { otherWorkflow.process(it) }
araqnid
07/08/2020, 10:53 AMMilan Hruban
07/08/2020, 10:55 AMPhilipp Mayer
07/08/2020, 12:10 PMandyg
07/08/2020, 8:51 PMotherWorkflow.process()
? If that method iterates through a list, the nothing should happen when the list is empty, thus no need for the empty check. If the method crashes or does something weird when the parameter is an empty list, it is probably cleaner and safer to do the empty check within the method itself.