Skip to content

Instantly share code, notes, and snippets.

Genericを使った型クラスインスタンスの導出

@ryoppy516


アジェンダ

  1. HListについて
  2. Genericについて
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
}
@ryoppy
ryoppy / scalaz05.scala
Created January 13, 2015 14:15
めも
// 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
// メモ化
// ======
@ryoppy
ryoppy / scalaz04.scala
Created January 13, 2015 09:21
めも
import scalaz._, Scalaz._
val f = (_: Int) + 1
val g = (_: Int) * 2
// Arrow
// ========
locally {
// andThen
assert((f >>> g)(1) === 4)
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
@ryoppy
ryoppy / scalaz03.scala
Created January 12, 2015 23:04
めも
import scalaz._, Scalaz._
// Tree
// Tree構造。TreeLocを使うとDOMのように多彩なアクセスができる。
// ==================
locally {
val tree = 'A'.node(
'B'.node(
'C'.leaf
),
import scalaz._, Scalaz._
val f = (_: Int) + 1
val g = (_: Int) + 2
val h = for {
x <- f
y <- g
} yield {
x + y
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
@ryoppy
ryoppy / vb1.scala
Created December 22, 2014 09:57
view boundがdepなったからcontext boundで書こうと思わず、普通にimplicitで書くべし
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]
scala> def fid[F[_], A](a: F[A]): F[A] = a
warning: there was one feature warning; re-run with -feature for details
fid: [F[_], A](a: F[A])F[A]
scala> fid(List(1))
res0: List[Int] = List(1)
scala> fid(Option(1))
res1: Option[Int] = Some(1)