Skip to content

Instantly share code, notes, and snippets.

View shapeless-study.md

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

@ryoppy516


アジェンダ

  1. HListについて
  2. Genericについて
View file0.scala
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
}
View scalaz05.scala
// 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
// メモ化
// ======
View scalaz04.scala
import scalaz._, Scalaz._
val f = (_: Int) + 1
val g = (_: Int) * 2
// Arrow
// ========
locally {
// andThen
assert((f >>> g)(1) === 4)
View file0.scala
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
View scalaz03.scala
import scalaz._, Scalaz._
// Tree
// Tree構造。TreeLocを使うとDOMのように多彩なアクセスができる。
// ==================
locally {
val tree = 'A'.node(
'B'.node(
'C'.leaf
),
View scalaz01.scala
import scalaz._, Scalaz._
val f = (_: Int) + 1
val g = (_: Int) + 2
val h = for {
x <- f
y <- g
} yield {
x + y
View file0.scala
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 Dec 22, 2014
view boundがdepなったからcontext boundで書こうと思わず、普通にimplicitで書くべし
View vb1.scala
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]
View hk_sample.scala
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)