Created
May 6, 2012 00:38
-
-
Save ngsw-taro/2606595 to your computer and use it in GitHub Desktop.
Kotlin 関数定義の方法に悩む…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 拡張関数で定義。 | |
// たぶん一番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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You seem to be looking for member literals:
http://youtrack.jetbrains.com/issue/KT-1183