Scala Tutorialsを読む 17 Loops using while

http://scalatutorials.com/tour/interactive_tour_of_scala_loops_using_while.html

よし追いついた。今日は17のwhileを使ったループだ。

17 Loops using while

Scala doesn't have a "classic" equivalent to the imperative for loop construct that exists in c, Java, etc. For imperative "for" like loops, use while.

ScalaはclassicなcやJava等に存在するforループと同じような機能を持ちません。 命令的なforの様なループをするには、whileを使います。

Scala doesn't support (out of the box) the ++ and -- operators, use += 1 or -= 1 for the same effect.

Scalaは++や--オペレータはサポートしておらず(boxの外)、同じ効果のある+= 1か -=1を使います。

Scala Tutorialsを読む 16 Assign multiple variables

http://scalatutorials.com/tour/interactive_tour_of_scala_assign_multiple_variables.html

今日は、16 Assign multiple variables 複数の変数の代入かな?

Using Tuples, it is possible to assign multiple values to multiple variables (either var or val).

多値を使うと、複数の変数を代入する事が可能です。 (var かvalで)

var (x, y, z, c, python, java) = (1, 2, 3, true, false, "no!")  

って書いて、まとめて変数に代入出来ると。

Scala Tutorialsを読む 15 Declare multiple variables

今日は15 Declare mltiple variables です。 http://scalatutorials.com/tour/interactive_tour_of_scala_declare_multiple_variables.html

2日遅れてる。どうも、2週間で何かしらの脳内の限界が来るような気がする。半月といったらいいのかな。辛いっていうことは、長期記憶するか、忘れるかしないとっていう境界なのかもしれない。ここで頑張れば覚えられるのかもしれないが、脳の記憶容量は限界があるからなにかを忘れないといけないのかもしれない。嫌な事を忘れて欲しいぜ。

15 Declare multiple variables 15 複数変数の宣言

It is possible to declare and assign multiple variables to the same value.

複数の変数を宣言して同じ値を代入する事が可能です。

The types of the variables in this example are inferred.

例の変数の型は推論されます。

var a,b,c=0

ってやると、推論されてaもbもcもゼロになるって話ですね。

Scala Tutorialsを読む 14 Return multiple variables

http://scalatutorials.com/tour/interactive_tour_of_scala_return_multiple_variables.html

今日はこの、14 Return multiple variablesを読みます。 複数の値を返すかな?

It is possible to return multiple variables using Tuples これはTuple(多値)を使って複数の値を返す事が出来ます。

See Also opens in new page 新しいページを開いて参照

  • More on Tuples
  • 多値の詳細

Scala Tutorialsを読む 13 Anonymous Functions 2

http://scalatutorials.com/tour/interactive_tour_of_scala_anonymous_functions_2.html

今日は、13 Anonymous Functions 2 を読みます。今日といいつつ今日は14日なので1日忘れてしまったので、2日分書かないといけない。

The first example is a method definition as we've seen before The second, 最初の例はメソッドの定義です我々が前の2つで見た、 is like the previous slide, only assigned to a val, this is very, very roughly like the difference between これは前のスライドのようで、valで割り当てしただけで、これはとても大ざっぱに言うとJavaScriptの以下の違いに似ています。

function foo(x, y) {
  return x + y;
}

and と

var foo = function(x, y) {
  return x + y;
}

in JavaScript.

The third, was briefly demonstrated in the previous slide, uses the shorter _ placeholder syntax. 三つ目は前のスライドでさらっとデモしていて、_の短いプレースホルダ構文を使ってます。 However the usage on the left is rare in Scala, 一方で、左の使い方はScalaではレアで、 the _ notation for anomymous functions is mostly useful when _記法の無名関数は高階関数(他の関数を受け取るか返す関数)にパラメータとして渡す時によく使われます。

Scala Tutorialsを読む 12 Anonymous Functions

http://scalatutorials.com/tour/interactive_tour_of_scala_anonymous_functions.html

今日は Scala Tutorials の12 Anonymous Functions を読みます。 AnonymousっていうとFTP接続する時とかに名前無しでログインするのに使ったりする名無しって意味があるよね。

Anonymous functions in Scala are of the following forms: Scalaでの無名関数は以下のような形式です:

(x:Int) => x * x // 型は:例 Int => IntはIntを受け取りIntを返す
(x:Int,y:Int)=>x+y // 型は (Int,Int) => Intで2つのIntを受け取りIntを返す例

Which is basically a "syntactic sugar" for this form: これは基本的に以下の形式のシンタックスシュガーです:

new Function1[Int,Int] {
  def apply(x: Int): Int = x * x
}
new Function2[Int, Int, Int] {
  def apply(x: Int, y:Int): Int = x + y
}

Type 型

The type of the 2nd anonymous function is (Int, Int) => Int and reads: "A function that map from two integers (Int, Int) to (=>) an integer (Int)" 2番目の無名関数の型は(Int, Int) => Intで"関数は2つの整数(Int,Int)から(=>)整数(Int)への写像である"と読みます。 The method doWithOneAndTwo expects a parameter of that type doWithOneAndoTwoメソッドはその型のパラメータを期待しているので我々はパラメータとして(x, y) => x + yを渡すことができます。

Parameter type inference 引数型推論

Note that we were able to drop the type declarations for x and y here, 我々はここのxとyから型宣言を取り外す事が可能であることに注意してください、なぜならばコンパイラdoWithOneAndTwoは2つのIntパラメータを受け取る関数を期待している事を既に知っているからです 、つまり我々は左の例の2つ目の例の呼び出しで型情報をxとyのパラメータを省略出来ます。

Shorter syntax 短い構文

Furthermore, there is even a shorter syntax for anonymous functions, 追加機能で、無名関数の短い構文があります,

(with a limitation that each variable is used exactly once). (制限ありで、関数本体中で各変数が正確に1回ずつ使われている場合)。

A place holder is used _ to replace each parameter, 各パラメータの置き換えるプレースホルダーは_が使われ、

the first occurrence of _ stands for the first parameter, 最初の_の出現は最初のパラメータを表し、 the second for the second parameter and so forth. 2つ目は2つ目のパラメータを等と表します。

See Also opens in new page 新しいページを開いて参照

  • More one anonymous functions
  • 無名関数の詳細

今日は長かったー。 明日も分量が結構あります。単語の意味を調べときましょう。

  • roughly 大雑把
  • briefly 簡潔に
  • However しかしながら,けれども,とはいえ

明後日は、楽なのでこのくらいで大丈夫そうです。

To be continued 13 Anonymous Functions 2.

Scala Tutorialsを読む 11 Method definition 3

http://scalatutorials.com/tour/interactive_tour_of_scala_method_definition_3.html

今日は11 Method definition 3です。 Curly braces are optional on single line method bodies.

カーリーブレース {}はシングルラインメソッドの本体ならオプションです。

def add(x:Int, y:Int) = x + y  

ということで、省略出来ますと言う話でした。瞬殺じゃないかぁ。 明日は12 Anonymous Functionsなのだけど、結構長いので分からない意味を調べておきましょう。

  • following: 以下の
  • therefore: それ故に
  • omit: 除外
  • Furthermore: さらに
  • exactly: 正確に
  • place holder: 仮に確保した場所

これで明日はちょっと楽と。