Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 你能解释一下Zipcode的乐趣吗?_Android_Kotlin - Fatal编程技术网

Android 你能解释一下Zipcode的乐趣吗?

Android 你能解释一下Zipcode的乐趣吗?,android,kotlin,Android,Kotlin,我是Kotlin的初学者 以下代码来自Kotlin for Android开发者 你能解释一下ZipCode请求的乐趣吗?这很难理解 class ForecastProvider(val sources: List<ForecastDataSource> = ForecastProvider.SOURCES) { companion object { val DAY_IN_MILLIS = 1000 * 60 * 60 * 24 val SO

我是Kotlin的初学者

以下代码来自Kotlin for Android开发者

你能解释一下ZipCode请求的乐趣吗?这很难理解

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?

}
似乎“fun requestByZipCode(zipCode:Long,days:Int):ForecastList=requestToSources{”是一个方便的代码,我不知道fun的完整代码“fun requestByZipCode(zipCode:Long,days:Int)…”是否容易理解

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?

}
class-ForecastProvider(val-sources:List=ForecastProvider.sources){
伴星{
val DAY_IN_MILLIS=1000*60*60*24
lazy{listOf(ForecastDb(),ForecastServer())}
}
fun requestByZipCode(zipCode:Long,days:Int):ForecastList=requestToSources{
val res=it.requestForecastByZipCode(zipCode,todayTimeSpan())
如果(res!=null&&res.size>=days)res-else为null
}
private-fun-requestToSources(f:(ForecastDataSource)->T?:T=sources.firstResult{f(it)}
}
接口预测数据源{
fun requestForecastByZipCode(zipCode:Long,date:Long):ForecastList?
fun requestDayForecast(id:Long):预测?
}
数据类预测列表(val id:Long,val city:String,val country:String,val dailyForecast:List){
val大小:Int
get()=dailyForecast.size
操作员乐趣获取(位置:Int)=每日预测[位置]
}
接口预测数据源{
fun requestForecastByZipCode(zipCode:Long,date:Long):ForecastList?
fun requestDayForecast(id:Long):预测?
}

这基本上是在做:

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
    }
}
通过查看存储库,
firstResult
扩展函数将是:

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 predicate was found.")
 }

由于列表中的
扩展函数
,您可能无法理解它:

如果您提出更具体的问题,那么我们可以给出更好的答案。