inline fun <T : Any, E : Any> batchQuery(
paramsToBeChunked: Collection<T>,
batchSize: Int = 500,
crossinline queryAction: (params: Collection<T>) -> List<E>
) = paramsToBeChunked.chunked(batchSize).flatMap { queryAction(it) }
inline fun <T : Any, E : Any> batchQuery(
paramsToBeChunked: Collection<T>,
batchSize: Int = 500,
crossinline queryAction: (params: Collection<T>) -> List<E>
) = paramsToBeChunked.chunked(batchSize).map { queryAction(it) }.flatten()
Is there any potential problem when using flatMap?
IMHO, map, flatten, flatMap are all inline functions, so these two implementations are equivalent, is this right?
If the fun wasn't inline
then in both cases queryAction
would have to be captured in a closure, because it's coming from outside of the lambda. It means that a class which is generated under the hood holds a reference to the variable, and when the lambda is eventually executed, it uses that reference.
But here, it's inlined just fine. Idea tries it's best to give information about captured variables but in this situation it might be a limitation of Idea's static analysis. The first message is just incorrect.
As @broot suggested, you can replace .map { f(it) }
with just .map(f)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With