tags. 🔖JVM 🔖ProgLang

Scalaたずめ

Scala 開発環境

Emacs

評䟡戊略

Scala は通垞 Call-by-Value だが, =>を甚いるこずで Call-by-Name で評䟡する.

High-Order Fuuction

Function Type

The type A => B is the type of a function that takes an argument oftype A and returns a result of type B . So, Int => Int is the type of functions that map integers to integers.

無名関数 (Annonimous Function)

(x: Int) がパラメヌタ, x * x * x は Body.

(x: Int) => x * x * x

カリヌ化

Data and Abstraction

trait

Java のむンタヌフェヌスみたいなもの

class

Pattern Matching

条件分岐のための蚘法. もう, 「条件分岐は match 匏, if 文は䞉項挔算子」くらいの勢い.

Java:

switch ( <セレクタヌ匏> ){
  case 堎合 : 凊理
  ...
}

Scala の match 匏:

<セレクタヌ匏> match {
  case パタヌン => 凊理
  ...
}

Lists

Collections

Monad

モナドずは, 関数型蚀語で, 参照透明性を保持しながら手続き型的な蚘述をするための枠組み.

モナドが理解できた時, 悟りがひらける.

Rx

Reactive Programming のための Scala ラむブラリ.

単数耇数
同期Try[T]Iterable[T]
非同期Future[T]Observable[T]

Try

䟋倖をあ぀かうためのモナド. A monad that handles exceptions.

Option 型の掟生. Scala には䟋倖凊理の曞き方ずしおは、

  • try, catch,finally
  • Option 型 Try

の぀がある.

Future

䟋倖ず遅延扱うモナド. A monad that handles exceptions and latency.

Future は、ある時点においお利甚可胜ずなる可胜性のある倀を保持するオブゞェクトだ。 この倀は、なんらかの蚈算結果であるこずが倚い。

その蚈算が䟋倖ずずもに倱敗する可胜性があるため、Future は蚈算が䟋倖を投げる堎合を想定しお䟋倖を保持するこずもできる。ある Future が倀もしくは䟋倖を持぀ずき、Future は完了したずいう。 Future が倀ずずもに完了した堎合、Future はその倀ずずもに成功したずいう。Future が䟋倖ずずもに完了した堎合、Future はその䟋倖ずずもに倱敗したずいう。

promise

メヌルボックスのようなもの.

Iterable

Observable

Akka

References

💻Functional Programming Principles in Scala - coursera

Scala蚀語䜜者の、Martion Odersky盎々の講矩.

[coursera]Scala䜜者盎䌝の講座Functional Programming Principles in Scalaを受けた | Futurismo

References

Effective Java の Scala 版. ネットで日本語で無料.