Tag: lambda

如何提高kotlin lambda语法?

我试图在android项目中使用kotlin M12,在工作过程中,我得到了这段代码({onSuccess(it)},{onFailure(it)}) AppObservable.bindActivity(this, api.get(id)).subscribe({onSuccess(it)}, {onFailure(it)}) fun onSuccess(str: String) {} fun onFailure(tr: Throwable) {} 这不是那么糟糕,但我认为它应该会更好。 我该如何改进呢?

在Kotlin的`forEach`中`break`和`continue`

Kotlin有非常好的迭代函数,比如forEach或者repeat ,但是我不能让break和continue它们(local和non-local): repeat(5) { break } (1..5).forEach { continue@forEach } 目标是尽可能接近地使用函数式语法来模仿通常的循环。 这在Kotlin的一些老版本中是绝对有可能的,但我很难重现这个语法。 问题可能是一个标签(M12)的错误,但我认为第一个例子应该工作。 在我看来,我已经读了一些关于特殊技巧/注释的地方,但我找不到任何关于这个主题的参考。 可能看起来像下面这样: public inline fun repeat(times: Int, @loop body: (Int) -> Unit) { for (index in 0..times – 1) { body(index) } }

在Kotlin中传递接口作为参数

我想通过一个接口作为这样的参数: class Test { fun main() { test({}) // how can I pass here? } fun test(handler: Handler) { // do something } interface Handler { fun onCompleted() } } 在Java中,我可以使用匿名函数如test(new Handler() { ………. }) ,但是我不能在Kotlin中这样做。 任何人都知道如何做到这一点?

使用java lambda调用kotlin函数时,Kotlin无法访问kotlin.jvm.functions.Function1

我正在尝试从Java调用以下Kotlin函数 override fun First(list: LinqList<ElementType>, condition: (ElementType) -> Boolean) : ElementType 喜欢这个 int first = list.First(list,(x) -> x == 5); 但我得到以下错误 Error java: cannot access kotlin.jvm.functions.Function1 class file for kotlin.jvm.functions.Function1 not found 我试图用Google搜索,但我无法找到答案的任何地方 提前致谢

kotlin lambda表达式作为可选参数

如何在Kotlin语言中将lambda表达式作为可选参数传递 val max = { a: Int, b: Int -> if (a > b) a else b } 我必须通过上面的东西就像可选参数

Kotlin:安全的lambdas(没有内存泄漏)?

在阅读了关于内存泄漏的这篇文章之后,我想知道在Kotlin Android项目中使用lambdas是否安全。 确实,lambda语法使得我的程序更加轻松,但是内存泄漏呢? 作为一个有问题的例子,我从我的一个项目中取得了一段代码,在那里构建了一个AlertDialog。 这段代码在我的项目的MainActivity类中。 fun deleteItemOnConfirmation(id: Long) : Unit { val item = explorerAdapter.getItemAt(id.toInt()) val stringId = if (item.isDirectory) R.string.about_to_delete_folder else R.string.about_to_delete_file val dialog = AlertDialog.Builder(this). setMessage(String.format(getString(stringId), item.name)).setPositiveButton( R.string.ok, {dialog: DialogInterface, id: Int -> val success = if (item.isDirectory) ExplorerFileManager.deleteFolderRecursively(item.name) else ExplorerFileManager.deleteFile(item.name) if (success) { explorerAdapter.deleteItem(item) explorerRecyclerView.invalidate() } else Toast.makeText(this@MainActivity, R.string.file_deletion_error, Toast.LENGTH_SHORT).show() }).setNegativeButton( R.string.cancel, […]

Mocktito ArgumentCaptor Kotlin lambda与参数

我试图在Kotlin上测试这个: verify(myInterface).doSomething(argumentCaptor.capture()) capture.value.invoke(0L) 哪里做什么: doSomething((Long) -> Unit) 我怎样才能为此创建一个ArgumentCaptor? 现在我正在做这个 inline fun <reified T : Any> argumentCaptor() = ArgumentCaptor.forClass(T::class.java)!! val captor = argumentCaptor<(Long) -> Unit>() verify(mainApiInterface!!).downloadUserProfilePicture(captor.capture()) captor.value.invoke(0L) 但是我得到java.lang.IllegalStateException:captor.capture()不能为空 我也尝试整合mockito-kotlin,但我得到一个PowerMockito错误: 在org.mockito.internal.MockitoCore的类层次结构中找不到名为“reported”的实例字段。

Kotlin – 如何递归调用一个lambda函数

我试图从Kotlin重新实现linrec函数。 以下是目前在Kotlin中的样子: fun <A, B> linrec(indivisible: (List<A>) -> Boolean, value: (List<A>) -> B, divide: (List<A>) -> List<List<A>>, combine: (A, B) -> B ) : (List<A>) -> B { val myfunc: (List<A>) -> B = { input -> if (indivisible(input)) { value(input) } else { val split = divide(input) val left = split[0][0] val right = […]

为什么Kotlin不能推断下面的lambda参数(在Java – > Kotlin转换之后)?

我有以下的Java代码: public class DatabaseManager { public interface Predicate<T> { boolean test(T t); } private interface Consumer<T> { void apply(T t); } private void updateLiveLists() { updateLiveLists(liveList -> true); } private void updateLiveLists(Predicate<MutableLiveList<?>> predicate) { forEachLiveList(liveList -> { if (predicate.test(liveList)) { refresh(liveList); } }); } private void forEachLiveList(Consumer<MutableLiveList<?>> consumer) { … } 然后我在Android Studio中使用Java -> Kotlin […]

Kotlin lambda有几个参数

我有点困惑kotlin lambda表达。 找不到合适的答案。 在Java中,我可以用tho参数设置一个监听器: myObject.setListener(new MyListener() { @Override public boolean doSmth(int pos, int value) { switch(..) { …. } } }) 用lambda: myObject.setListener((p1, p2) -> { switch(..) { …. } }) 在Kotlin,我可以这样做: myObject.setListener{p1, p2 -> return@setListener false} 要么 myObject.setListener{{p1, p2 -> if (p1) { return@setListener true } else { return@setListener false } }} 但它真的很丑。 有没有办法做到这一点? […]