Scala Tutorialsを読む 10 Method definition 2
http://scalatutorials.com/tour/interactive_tour_of_scala_method_definition_2.html
今日は10 Method definition 2を読みます。
return
keyword is optional- リターンキーワードはオプショナルです。
- Return type is inferred
- リターン型は推論されます
- (for non recursive methods
- (非再帰な関数で、、、のための)
- and methods that don't have an explicit return value)
- explicitな(明確な)リターン値を持たない)
再帰的でないなら、戻り値の型は省略出来ますって話でした。
Scala Tutorialsを読む 9 Method definition
http://scalatutorials.com/tour/interactive_tour_of_scala_method_definition.html
今日は 9 Method definition メソッドの定義について
- In Scala methods are defined using def
- Scalaではメソッドはdefを使って定義されます
- Methods that return a value must have an equal sign before the body of the method
- メソッドが値を返すときはメソッドの本体の前でイコールを使って指定する必要があります
- Methods that have either a return statement or are recursive must declare a return type
- メソッドはリターンステートメントを持つかまたは、再帰的なメソッドは戻り値の型を宣言する必要があります。
Scala Tutorialsを読む 8 Useful operations
http://scalatutorials.com/tour/interactive_tour_of_scala_useful_operations.html
今日は、8 Useful operations 使える機能ってところかな?
Scala has many "syntactic sugars" for many common operations. Scalaには沢山のシンタックスシュガーがあります。沢山の共通な機能のための
Some useful ones are show on the left. いくつかの使えるやつは左のソースで見れます。
Play with the examples 例で遊んでみてください。
( click the green "Play" button to run) (緑のPlayボタンをクリックして実行だ)
and once you feel comfortable continue to the next step そして あなたは感じるcomfortableってなんだ?快適さか。 そしてあなたが快適さを感じたら次のステップへ続きましょう。
More will be explained later on もっと以降で説明されています。 explainは説明するの意味っと。
レンジは 1 to 10とか 1 until 10とか 2 until 10 by 3とかとかけて、 レンジからリストを作ったりも出来る 数の絶対値はnum.absってかけたり、maxは num.max(7)ってかけたりするし
文字列のreverseやcapitalizeとか文字列のかけ算で複数回の連続にできたり "123".toIntでパースできたり、 range.filter( > 4)で4より大きいってできたり、range2.map( * 2)で2倍に出来たり便利だな ってことですね。
Scala Tutorialsを読む 7 String formatting
今日は、 http://scalatutorials.com/tour/interactive_tour_of_scala_string_formatting.html を読みます。
書くの忘れてたのだけど、C言語のprintf的な物がScalaにもあるよっていう。
printf works as well...
printf は同じように動きます...
瞬殺すぎる。
printf("Now you have %.16f problems.", Math.nextAfter(2.0, 3))
こんな感じで書けるんですね。
See Also opens in new page
新しいページを開いて参照
- Printf cheat sheet
- Printf チートシート
Scala Tutorialsを読む 6 String interpolation
http://scalatutorials.com/tour/interactive_tour_of_scala_string_interpolation.html
今日は、6 String interpolationです。 interpolationは内挿法とかがいいのかな?文字列内挿法? 文字列に式が書ける奴ですね。 interpolationはSwiftでもあったり、Nemerleでもあるぜと、アリスさんが言ってたりしましたが、Scalaにもあるということですな。
From Scala 2.10, string interpolation is supported. Scala2.10から、Stringインターポレーションがサポートされました。
For complex expressions add curly braces, e.g.
例のように複雑な式のために、カーリーブレースを加えます。
s"Two times three: ${2 * 3}"
See Also opens in new page 参照
Tauって、2πのことなのかな?ようわからないけどいいや。
明日は、7 String formattingを見ます。 特に明日は難しくないな。
Scala Tutorialsを読む 5 Printing values
http://scalatutorials.com/tour/interactive_tour_of_scala_printing_values.html
今日は5のPrinting valuesを読みます。
値の出力とかか。
Scala has a "shortcut" to Java's System.out.println ScalaはJavaのSystem.out.printlnのショートカットを持っています。(なのでべんりだぜ
(as well as print, printf etc.) (...はもちろん、print,printfなども)
They are all defined in a class それらはクラスの中ですべて定義されている
that is imported by default インポートされたデフォルトで(scala.Predef)
それらは、デフォルトでインポートされた(scala.Predef)クラスの中ですべて定義されていると。
See Also opens in new page.
参照 新しいページを開いて
- scala.Predef
でリンク貼られていると。
System.out.printlnは長くて面倒くさいので、printlnって書けるのは嬉しいねって話ですね。 明日は、6 String interpolationだ。
予習しておこう
- interpolation 書き入れ 補間, 内挿 改ざん
文字列の中になにか書き入れる機能みたいな話ですね。何かっていうと式だとかって話になるわけだけど、まあ明日に続く。
Scala Tutorialsを読む 4 Final Variables
今日は4 Final Variablesを読みます。
http://scalatutorials.com/tour/interactive_tour_of_scala_final_variables.html
Final変数ですな。Swift騒ぎで忘れてたよ orz...
FInal variables are declared using the val keyword Final変数はvalキーワードを使って宣言される。Swiftはvarはvarでvalはletなんだよな。 どっちがいいのだろうって、言ってもまぁ、似たような物だけど。
(a final variable is a variable that can't be reassigned) ファイナル変数は再代入出来ない変数です。
The code on the left looks almost exactly like in the previous step, 左側のコードは前のステップと同じに見えます、 with one small change 1つの小さな変更をすると。
これ悩んでしまったのだけど、左側のコードってのは、
val x = 1 + 2 //val instead of var println(x) x = 3 * 4 //error: reassignment to val println(x)
このコードの事ね。
The var was changed to val.
varはvalに変更されています。(前のページから、確かに1文字変えただけだ)
Try to run the code on the left, 左のコードで実行してみてください、ってことで実行するとエラーになる。 the compiler should complain on line 3 コンパイルはcomplainをライン3でいうと。complainは苦情とからしい。エラーがでるってことだな。 since we are trying to reassign x which is a val. valなxに再代入しようとしたからだ。
Exercise エクササイズ 練習だよな
Edit the code on the left, 左のコードを編集して、 so it will run. 実行できるようにしてみよう。
(either change the val back to var, valをvarに戻すのではなくて、 remove the reassignment to x or assign the result of the expression in line 3 to a different val or var.
3行目のxへの再代入を消して違うvalかvarの変数にする。)
val x = 1 + 2 //val instead of var println(x) val x2 = 3 * 4 println(x2)
とかですな。
val x = 1 + 2 //val instead of var println(x) val x = 3 * 4 println(x)
これだとエラーだ。
Note: メモ:
Prefer using 'val' over 'var' valを使ったほうがよりよいです。 (and immutable objects over mutable ones). immutableなオブジェクトがmutableなものよりよいですと。 There are many benefits 色んな利点があります。 that are out of the scope of this small tour. この小さなツアーでは範囲を超えるけど。
ってところですね。