17
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Scala

17
Last updated at Posted at 2015-04-11

基本型

説明
String 文字列
Char 文字
Boolean 真偽値
Byte 8bit整数
Double 倍精度浮動小数
Float 単精度浮動小数
Long 64bit整数
Int 32bit整数
Short 16bit整数

リテラル

文字列

val msg = "World"
val hello = s"Hello, ${msg}"
val longMsg = """
こんにちは
"aiu"
"""
println(hello)

関数定義

def msg(str:String): String = "Hello, "+str

msg("world") // "Hello, world"

関数リテラル

Ruby の -> に近い

val printMsg = (msg: String) => println(s"Hello, ${msg}")
printMsg("World") // Hello, world

クラス定義

class Hoge {
  val constant:Boolean = false
  var member:Int = _
  var member2:Option[String] = _
}

new Hoge

キャスト


Any#asInstanceOf[]

object

クラスの定義とインスタンスの作成を行う(シングルトンオブジェクト)

object HelloWorld {
  def msg {
    println("Hello, World")
  }
  def main(args: Array[String]) {
    msg
  }
}

main メソッドがエントリポイント。
インタプリタを実行すると次のとおり。

$ scala HelloWorld.scala
Hello, World

if

3項演算子として使う

if (x) 1 else 0

パターンマッチ

var x = "a"
x match {
   case name: String => println("name = " + name)
   case num: Int => println("num = " + value)
   case _ => println("any = " + x)
}

コレクション

List

immutable なリスト

List("foo", "bar") // List[String] = List(foo, bar)
List("1", 2, 3.0)  // List[Any] = List(1, 2, 3.0)
"1"::2::3.0::Nil // List[Any] = List(1, 2, 3.0)

head, tail, init, last, ::, +: , :+, apply

val sc = List(1, 2)
sc.head // 1
sc.tail // List(2)
sc.init // List(1)
sc.last // 2
3::sc // List(3, 1, 2)
3+:sc // List(3, 1, 2)
sc :+ 3 // List(1, 2, 3)
sc.apply(1) // 1
sc(1) // 1
sc ::: sc // List(1, 2, 1, 2)
sc.reverse // List(2, 1)

sum, product, max, min

sc.sum // 3
sc.product // 2
sc.min // 1

Array

muttable なリスト

Ruby の Array と同じイメージ

val array = Array(1, 2, 3) // Array[Int] = Array(1,2,3)
array(1) = 0
array // Array[Int] = Array(1, 0, 3)

Range

Ruby の 1..10 と同じ

1 to 10 // Range(1, 10)
for ( v <- 1 to 3 ) {
  println("v = " + v)
}

filter

リストから条件に合致した値のリストを作成する

Ruby の select に近い

val list = List(1, 2, 3, 4, 5)
list.filter(n => n % 2 == 1) // List[Int] = List(1, 3, 5)

map

map内の関数値を含むコレクションを返す

var list = List("1", "2", "3")
list.map(_.toInt)  // List[Int] = List(1, 2, 3)

foreach

値は返さない

(1 to 10).filter(n => n % 2 == 1).foreach(n => println(n))

Options

値を持つか持たないかをわからない状態を管理するもの。
ORマッパーの型定義として使われることがおおい

Option[型]

Option[型] = Some(値)
Option[型] = None = Option[Any](null)
foo match {
   case Some(v) => v
}

変数vにSomeの中身が束縛されて取り出せる

もしくは getOrElse を使う。

scala> val foo = Option[Int](1)
foo: Option[Int] = Some(1)
scala> foo.getOrElse(0)
res0: Int = 1

Noneは値が設定されていない状態をしめす
Some(x) は値が設定されている状態をしめす。

scala> None.getOrElse(1)
res2: Int = 1

scala> Option[Any](null)
foo2: Option[Any] = None

scala> Some(1000).getOrElse(1)
res3: Int = 1000
17
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?