在高阶函数中使用Lambda时无法删除重复项

时间:2019-06-20 11:56:19

标签: android kotlin kotlin-coroutines

这是我的android应用

 suspend fun getTraidersList(isCustomtHandle: Boolean = false): Any {
            if (isCustomtHandle) {
                return runOperationWithoutHandle {
                    traderMonitorRestClient.getTraidersList()
                }
            } else {
                return runOperationWithDefaultHandle {
                    traderMonitorRestClient.getTraidersList()
                }    
            }
        }

 suspend fun executeTraderOperation(traderOperation: Trader.Operation, base: String, quote: String, isCustomtHandle: Boolean = false): Any {
            val sender = BuildConfig.APPLICATION_ID + "_" + BuildConfig.VERSION_NAME
            val key = DateUtil.getDateAsString(Date(), "mmHHddMMyyyy")
            if (isCustomtHandle) {
                return runOperationWithoutHandle {
                    traderMonitorRestClient.executeTraderOperation(traderOperation.toString().toLowerCase(), base.trim(), quote.trim(), sender, key)
                }
            } else {
                return runOperationWithDefaultHandle {
                    traderMonitorRestClient.executeTraderOperation(traderOperation.toString().toLowerCase(), base.trim(), quote.trim(), sender, key)
                }
            }
        }

        suspend private fun runOperationWithoutHandle(func: suspend () -> Response<*>): Response<*> = withContext(Dispatchers.IO) {
            val response: Response<*> = func() // in runtime replace by method body (e.g. traderMonitorRestClient.getTraidersList())
            response
        }

        suspend private fun runOperationWithDefaultHandle(func: suspend () -> Response<*>): TransportResponse = withContext(Dispatchers.IO) {
            try {
                val response: Response<*> = func() // in runtime replace by method body (e.g. traderMonitorRestClient.getTraidersList())
                if (response.isSuccessful) { // status (200-299)
                    onSuccess(response)
                } else {// error - status (300-599)
                    val errorResponse: ErrorResponse = ErrorUtils.parseError(response)
                    onError(errorResponse)
                }
            } catch (e: Throwable) {
                val errorResponse = ErrorResponse()
                errorResponse.setCode(SERVICE_UNAVAILABLE_CODE)
                errorResponse.message = MyApplication.getAppContext().getString(R.string.service_unavailable)
                onError(errorResponse)
            }
}

isCustomtHandle true 时,将lambda称为“ runOperationWithoutHandle”。此lambda 执行http请求而不处理。

isCustomtHandle false 时,将lambda称为“ runOperationWithDefaultHandle”。此lambda执行http请求处理响应。

好。这项工作正常。

但是正如您所见,在线上有重复

traderMonitorRestClient.executeTraderOperation(traderOperation.toString().toLowerCase(), base.trim(), quote.trim(), sender, key)

traderMonitorRestClient.getTraidersList()

如何删除此重复项?我想打线

traderMonitorRestClient.executeTraderOperation(traderOperation.toString().toLowerCase(), base.trim(), quote.trim(), sender, key)

traderMonitorRestClient.getTraidersList()

仅一次。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

您可以在ws = wb['sheet1'] # why people persist in using long deprecated syntax is beyond me flag = None for row in ws.iter_rows(max_row=10508, min_col=6, max_col=6): cell = row[0] sign = cell.value > 0 and "positive" or "negative" if flag is not None and sign != flag: print(cell.row) flag = sign 函数内部本地添加一个匿名挂起函数,并从executeTraderOperation语句的两个分支中调用该函数吗?例如

if