この文書の現在のバージョンと選択したバージョンの差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
kotlin:標準関数・スコープ関数 [2019/12/13 01:28] ips [run] |
kotlin:標準関数・スコープ関数 [2019/12/13 01:33] (現在) ips [takeUnless] |
||
---|---|---|---|
ライン 73: | ライン 73: | ||
fun main(){ | fun main(){ | ||
+ | // runによる関数呼び出しの連鎖 | ||
val text = "polarcubis,supreme master5 of nyethack" | val text = "polarcubis,supreme master5 of nyethack" | ||
- | .run(::nameIsLong) | + | .run(::nameIsLong) // >>> true |
- | .run(::playerCreateMessage) | + | .run(::playerCreateMessage) // >>> too long |
- | .run(::print) | + | |
- | >>>fun nameIsLong(name:String) = name.length > 10 | + | |
- | fun playerCreateMessage(nameTooLong:Boolean):String{ | + | |
- | return if (nameTooLong){ | + | |
- | "too long" | + | |
- | }else{ | + | |
- | "welcome!!" | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | fun main(){ | + | |
- | val text = "polarcubis,supreme master5 of nyethack" | + | |
- | .run(::nameIsLong) | + | |
- | .run(::playerCreateMessage) | + | |
.run(::print) | .run(::print) | ||
} | } | ||
ライン 96: | ライン 83: | ||
</code> | </code> | ||
===== with ===== | ===== with ===== | ||
+ | |||
+ | 引数を第一パラメーターとして受け取る必要がある。 | ||
+ | 可能ならばrunで置き換える。 | ||
+ | |||
===== also ===== | ===== also ===== | ||
+ | let とにているが、letはラムダの結果を返す。 | ||
+ | alsoはレシーバーを返す。 | ||
===== takeif ===== | ===== takeif ===== | ||
+ | |||
+ | 真だとレシーバーを返す。 | ||
+ | 偽だとnullを返す | ||
+ | |||
===== takeUnless ===== | ===== takeUnless ===== | ||
+ | 偽だとレシーバーを返す。 | ||
+ | 真だとnullを返す。 | ||
+ | わかりにくいため使用すべきではない。 |