Skip to content

Instantly share code, notes, and snippets.

@yagince
yagince / BaseSettings.scala
Created March 23, 2013 15:43
sbt-template
import sbt._
import Keys._
object BaseSettings {
val buildName = "MyProject"
val buildOrg = "yagince"
val buildVersion = "0.0.1"
val buildScalaVersion = "2.10.1"
val settings = Seq(
@yagince
yagince / StringInterpolation_01.scala
Created March 24, 2013 13:16
変数埋め込み
package interpolation
object StringInterpolation_01 extends App {
val hoge = "hoge"
println(s"Hello $hoge!")
}
@yagince
yagince / StringInterpolation_02.scala
Created March 24, 2013 13:22
計算式やらメソッド呼び出しやら
package interpolation
object StringInterpolation_02 extends App {
println(s"${1 + 1}")
def test = 1 + 1
println(s"${test}")
println(s"$test")
@yagince
yagince / StringInterpolation_03.scala
Created March 24, 2013 13:24
フォーマット指定
package interpolation
object StringInterpolation_03 extends App{
// format
val pi = 3.14
val hoge = "hogehogehoge"
println(f"$hoge is $pi%2.4f") // => hogehogehoge is 3.1400
//println(f"$hoge is $pi%2.4d") // これはpiがDoubleなので、エラーになる
package interpolation
object StringInterpolation_04 extends App {
println(s"100 + 1 = ${100+1}") // 100 + 1 = 101
println(StringContext("100 + 1 = ", "").s(100+1)) // 100 + 1 = 101
println(StringContext("100 + 1 = ", "%5d").f(100+1)) // 100 + 1 = 101
}
@yagince
yagince / ImplicitClass_01.scala
Created March 26, 2013 12:23
simple implicit class
package implicitclass
object ImplicitClass_01 extends App {
implicit class DoubleInt(i: Int) {
def double = i * 2
}
println(1.double)
class HalfInt(i: Int) {
def half = i / 2
@yagince
yagince / ImplicitClass_02.scala
Created March 26, 2013 12:31
extract implicit class
package implicitclass
object ImplicitClasses {
implicit class TripleInt(i: Int) {
def triple = i * 3
}
}
// こうは書けない
//implicit class TripleInt(i: Int) {
@yagince
yagince / valueclass_Double.scala
Created March 27, 2013 11:31
simple value class
package valueclass
class Double(val i: Int) extends AnyVal with Hoge{
// val tmpDouble = i * 2 // これは定義できない
def double = i*2
// 以下はコンパイルエラー
// object Foo {
// val foo = "foo"
// }
@yagince
yagince / implicitclass_ImplicitConversions.scala
Created March 27, 2013 12:19
implicit class + value class
package valueclass.implicitclass
object ImplicitConversions {
implicit class Triple(val i: Int) extends AnyVal {
def triple = i * 3
}
class Half(val i:Int) extends AnyVal {
def half = i / 2
}
implicit def toHalf(i: Int) = new Half(i)
@yagince
yagince / array_ValueClass_04.scala
Created April 3, 2013 12:56
配列に代入される場合
package valueclass.memory.array
object ValueClass_04 extends App {
case class Hoge(val hoge: String) extends AnyVal
val hoge = Hoge("aaaa").hoge
val array = Array[Hoge](Hoge("str"))
val list = List[Hoge](Hoge("str"))
val seq = Seq[Hoge](Hoge("str"))
val set = Set[Hoge](Hoge("str"))