¿Podría explicarnos la divertida requestByZipCode?

Soy un principiante de Kotlin.

El siguiente código es de Kotlin-for-Android-Developers en https://github.com/antoniolg/Kotlin-for-Android-Developers/tree/master-june-2017

¿Podría explicarnos la divertida requestByZipCode ? Es difícil de entender

Parece que " fun requestByZipCode (zipCode: Long, days: Int): ForecastList = requestToSources { " es un código conveniente, no sé si un código completo de la diversión " fun requestByZipCode (zipCode: Long, days: Int). .. "es fácil de entender.

class ForecastProvider(val sources: List<ForecastDataSource> = ForecastProvider.SOURCES) { companion object { val DAY_IN_MILLIS = 1000 * 60 * 60 * 24 val SOURCES by lazy { listOf(ForecastDb(), ForecastServer()) } } fun requestByZipCode(zipCode: Long, days: Int): ForecastList = requestToSources { val res = it.requestForecastByZipCode(zipCode, todayTimeSpan()) if (res != null && res.size >= days) res else null } private fun <T : Any> requestToSources(f: (ForecastDataSource) -> T?): T = sources.firstResult { f(it) } } interface ForecastDataSource { fun requestForecastByZipCode(zipCode: Long, date: Long): ForecastList? fun requestDayForecast(id: Long): Forecast? } data class ForecastList(val id: Long, val city: String, val country: String, val dailyForecast: List<Forecast>) { val size: Int get() = dailyForecast.size operator fun get(position: Int) = dailyForecast[position] } interface ForecastDataSource { fun requestForecastByZipCode(zipCode: Long, date: Long): ForecastList? fun requestDayForecast(id: Long): Forecast? } 

Esto básicamente está haciendo:

 fun requestByZipCode(zipCode: Long, days: Int): ForecastList { return sources.firstResult { val res = it.requestForecastByZipCode(zipCode, todayTimeSpan()) if (res != null && res.size >= days) res else null } } 

Y al mirar el repository, la firstResult function de extensión de resultado sería:

 fun requestByZipCode(zipCode: Long, days: Int): ForecastList { for (element in sources) { val res = element.requestForecastByZipCode(zipCode, todayTimeSpan()) val result = if (res != null && res.size >= days) res else null if (result != null) return result } throw NoSuchElementException("No element matching pnetworkingicate was found.") } 

Es posible que tenga problemas para entenderlo debido a la Extension Function en la list: https://kotlinlang.org/docs/reference/extensions.html