Last active
April 25, 2016 06:51
-
-
Save nesheep5/8332cb9d69767c113724 to your computer and use it in GitHub Desktop.
最初にハマるScalaの省略記法(無名関数編) ref: http://qiita.com/nesheep5/items/a73345109eceef14df86
This file contains hidden or 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
| object AnonymousFunctionSample { | |
| def main(args: Array[String]) { | |
| val strs = Array("foo", "bar") | |
| //以下、全て同じ結果を返す | |
| // 基本となる無名関数の文法「(変数名 :型) => { 処理 }」 | |
| strs.foreach((str :String) => { println(str) }) | |
| // 変数の型は省略可能。省略した場合は型推論が行われる | |
| strs.foreach(str => { println(str) }) | |
| // 処理ブロックの{}は省略可能 | |
| strs.foreach(str => println(str)) | |
| // 変数名に「_」を利用すると変数宣言が省略可能 | |
| strs.foreach(println(_)) | |
| // 処理内での引数が1つとなる場合、変数そのものを省略可能 | |
| strs.foreach(println) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment