Skip to content

Instantly share code, notes, and snippets.

@ngsw-taro
Created May 6, 2012 00:38
Show Gist options
  • Save ngsw-taro/2606595 to your computer and use it in GitHub Desktop.
Save ngsw-taro/2606595 to your computer and use it in GitHub Desktop.
Kotlin 関数定義の方法に悩む…
// 拡張関数で定義。
// たぶん一番Kotlinらしい書き方
// kotlin friendly
fun String.isFourCharacters() = this.size == 4
// static関数として定義。
// Javaっぽい書き方?
// like java?
//fun isFourCharacters(str : String) = str.isFourCharacters()
// 関数リテラルを変数に代入
// 関数型言語っぽい?
// like functional language?
val isFourCharacters = {
(str : String) -> str.isFourCharacters()
}
fun main(args : Array<String>) {
val strs = array("java", "kotlin", "groovy")
val result1 = strs filter {
it.isFourCharacters()
// or
// isFourCharacters(it)
}
// like functional language
val result2 = strs filter isFourCharacters
}
@abreslav
Copy link

abreslav commented May 6, 2012

You seem to be looking for member literals:

http://youtrack.jetbrains.com/issue/KT-1183

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment