Kotlin函数参考 [英] Kotlin function reference

查看:94
本文介绍了Kotlin函数参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

记录为流/集合,提取函数将数据转换为这种集合的一个元素。 p>

Kotlin有没有一种方法可以写出:

  records.map {extract (it)} 

未明确应用(it)



例如 records.map(extract) records.map {extract}

extract 是一个值(局部变量,属性,参数)功能类型(T) - > R T() - >对于某些 T R ,R ,那么您可以直接将它传递给<$ c
$ b

  records.map(提取)
map



$ b $ p $ lt; $ p $ <$ p $ <$ p $ <$ p code> val upperCaseReverse:(String) - > String = {it.toUpperCase()。reversed()}

listOf(abc,xyz)。map(upperCaseReverse)// [CBA,ZYX]
extract 是一个顶级的单参数函数,或者是一个顶级的单参数函数本地单参数函数,您可以将函数引用为 :: extract 并将它传递给 map

  records.map(:: extract)

示例: / b>

  fun rotate(s:String)= s.drop(1)+ s.first()

listOf(abc,xyz)。map(:: rotate)// [bca,yzx]


  • 如果它是一个类的成员或扩展函数 SomeClass 不接受任何参数或者 SomeClass ,你可以用它作为 SomeClass :: extract 。在这种情况下, records 应该包含 SomeType 的项目,它将被用作 extract

      records.map(SomeClass :: extract)

    示例:
    $ b

      fun Int.rem2()= this%2 

    listOf(abc,defg)。map(String :: length).map(Int :: rem2)/ / [1,0]


  • extract 是一个类的成员或扩展函数 SomeClass 接受一个参数,您可以用一些接收者创建绑定可调用引用。 foo

      records.map(foo :: extract)
    records.map this :: extract)//调用`this`接收者

    示例: sup>

      listOf(abc,xyz)。map(prefix:: pl我们)// [[prefixabc,prefixxyz] 




  • (上面所有代码示例的可运行演示)


    Let records be stream/collection and extract function which transforms data form an element of such collection.

    Is there a way in Kotlin to write

    records.map {extract(it)} 
    

    without explicitely applying(it) ?

    E.g. records.map(extract) or records.map {extract}

    解决方案

    • If extract is a value (local variable, property, parameter) of a functional type (T) -> R or T.() -> R for some T and R, then you can pass it directly to map:

      records.map(extract)
      

      Example:

      val upperCaseReverse: (String) -> String = { it.toUpperCase().reversed() }
      
      listOf("abc", "xyz").map(upperCaseReverse) // [CBA, ZYX]
      

    • If extract is a top-level single argument function or a local single argument function, you can make a function reference as ::extract and pass it to map:

      records.map(::extract)
      

      Example:

      fun rotate(s: String) = s.drop(1) + s.first()
      
      listOf("abc", "xyz").map(::rotate) // [bca, yzx]
      

    • If it is a member or an extension function of a class SomeClass accepting no arguments or a property of SomeClass, you can use it as SomeClass::extract. In this case, records should contain items of SomeType, which will be used as a receiver for extract.

      records.map(SomeClass::extract)
      

      Example:

      fun Int.rem2() = this % 2
      
      listOf("abc", "defg").map(String::length).map(Int::rem2) // [1, 0]
      

    • Since Kotlin 1.1, if extract is a member or an extension function of a class SomeClass accepting one argument, you can make a bound callable reference with some receiver foo:

      records.map(foo::extract)
      records.map(this::extract) // to call on `this` receiver
      

      Example:

      listOf("abc", "xyz").map("prefix"::plus) // [prefixabc, prefixxyz]
      

    (runnable demo with all the code samples above)

    这篇关于Kotlin函数参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆