This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> def s[A: ({type λ[A] = A => Ordered[A]})#λ](xs: List[A]): List[A] = xs.sorted | |
s: [A](xs: List[A])(implicit evidence$1: A => scala.math.Ordered[A])List[A] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implicit class RichFunction1[A, B](val f: A => B) extends AnyVal { | |
def |>[C](g: B => C) = f andThen g | |
def <|[C](g: C => A) = f compose g | |
} | |
val plus = (_: Int) + 1 | |
val minus = (_: Int) - 1 | |
val suffix = (_: Int) + "!" | |
val f = plus |> minus |> suffix |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scalaz._, Scalaz._ | |
val f = (_: Int) + 1 | |
val g = (_: Int) + 2 | |
val h = for { | |
x <- f | |
y <- g | |
} yield { | |
x + y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scalaz._, Scalaz._ | |
// Tree | |
// Tree構造。TreeLocを使うとDOMのように多彩なアクセスができる。 | |
// ================== | |
locally { | |
val tree = 'A'.node( | |
'B'.node( | |
'C'.leaf | |
), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> val size = "300x250" | |
size: String = 300x250 | |
scala> val Array(w, h) = size split "x" map (_.toInt) | |
w: Int = 300 | |
h: Int = 250 | |
scala> val Array(w, h, z) = size split "x" map (_.toInt) | |
scala.MatchError: [I@606d6d2c (of class [I) | |
... 33 elided |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scalaz._, Scalaz._ | |
val f = (_: Int) + 1 | |
val g = (_: Int) * 2 | |
// Arrow | |
// ======== | |
locally { | |
// andThen | |
assert((f >>> g)(1) === 4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// stream | |
// ====== | |
locally { | |
val a: Stream[Int] = unfold(10) { x => if (x == 0) none else (x, x - 1).some } | |
assert(a.toList === List(10,9,8,7,6,5,4,3,2,1)) | |
} | |
// Memo | |
// メモ化 | |
// ====== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Bench { | |
@Benchmark | |
def set2seq(): Unit = (1 to 100).combinations(2).toList.flatten.toSet.toSeq | |
@Benchmark | |
def distinct(): Unit = (1 to 100).combinations(2).toList.flatten.distinct | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package util | |
import org.specs2.mutable._ | |
class BenchmarkSpec extends Specification with Before with Benchmark { | |
def before() = isPrint = false | |
"loop" should { | |
"10 times" in { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package util | |
import compat.Platform | |
/** | |
* ベンチマーク用trait | |
* | |
* 継承して、loop(ループ回数, 実行するFunction) してあげると | |
* 指定回数実行して、結果がprintされる。 | |
* |