Skip to content

Instantly share code, notes, and snippets.

seraph seraphr

Block or report user

Report or block seraphr

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@seraphr
seraphr / OptionTraverse.scala
Created Jul 12, 2015
fp in scala EXERCISE 4.5
View OptionTraverse.scala
def traverse[A, B](a: List[A])(f: A => Option[B]): Option[List[B]] = a match {
case x :: xs =>
for {
y <- f(x)
ys <- traverse(xs)(f)
} yield y :: ys
case _ =>
Some(Nil)
}
@seraphr
seraphr / TreeFold.scala
Created Jul 11, 2015
Treeのfoldと、それを用いた各種関数実装
View TreeFold.scala
trait Tree[A]
case class Leaf[A](v: A) extends Tree[A]
case class Branch[A](l: Tree[A], r: Tree[A]) extends Tree[A]
def fold[A, B](t: Tree[A])(map: A => B, construct: (B, B) => B): B = t match {
case Leaf(v) => map(v)
case Branch(l, r) => construct(fold(l)(map, construct), fold(r)(map, construct))
}
@seraphr
seraphr / foldRight.scala
Created Jul 11, 2015
foldRightのfoldLeftを用いた実装。 ついでにstackless
View foldRight.scala
def foldRightSimple[A, B](l: List[A])(z: B)(f: (A, B) => B): B = l match {
case Nil => z
case x :: xs => f(x, foldRightSimple(xs)(z)(f))
}
trait Trampoline[T]
case class Done[T](v: T) extends Trampoline[T]
case class Suspend[T](f: () => Trampoline[T]) extends Trampoline[T]
@annotation.tailrec
View repl.scala
scala> case class Base(id: String, data: String)
defined class Base
scala> case class Merged(id: String, dataList: Seq[String])
defined class Merged
scala>
scala> val tSeq = Seq(Base("c", "c2"), Base("a", "a1"), Base("c", "c3"), Base("a", "a2"), Base("b", "b"), Base("c", "c1"))
tSeq: Seq[Base] = List(Base(c,c2), Base(a,a1), Base(c,c3), Base(a,a2), Base(b,b), Base(c,c1))
@seraphr
seraphr / CtorMacro.scala
Created Feb 18, 2014
scalaのdefマクロでコンストラクタ呼び出しコード生成
View CtorMacro.scala
import scala.reflect.macros.Context
import scala.language.experimental.macros
object InitializeMacro{
def init[T, A, B](a: A, b: B) = macro initImpl[T, A, B]
def initImpl[T: c.WeakTypeTag, A: c.WeakTypeTag ,B: c.WeakTypeTag](c: Context)(a: c.Expr[A], b: c.Expr[B]): c.Expr[T] = {
import c.universe._
val tType = implicitly[WeakTypeTag[T]].tpe
@seraphr
seraphr / hasCtor.scala
Created Feb 17, 2014
scalaのリフレクションで、コンストラクタの有無を確認するコード
View hasCtor.scala
import scala.reflect.runtime.universe._
class TypeCheck[T: WeakTypeTag]{
val mClassType = implicitly[WeakTypeTag[T]].tpe
val mConstructors = mClassType.members.filter(_.isMethod).map(_.asMethod).filter(_.isConstructor)
val mConstructorsParams = mConstructors.map(_.paramss.flatten.map(_.typeSignature))
def of[A: WeakTypeTag](a: A): Boolean = {
@seraphr
seraphr / Structural.scala
Created Nov 29, 2013
scala 構造的部分型
View Structural.scala
trait Hoge{def hoge: String}
def h(a: Hoge) = a.hoge
val hogeInstance: Hoge = new Hoge{def hoge = "hoge"}
val hogeLikeInstance: {def hoge: String} = new {def hoge = "fuga"}
h(hogeInstance) // OK
h(hogeLikeInstance) // NG
@seraphr
seraphr / 00readme
Last active Dec 24, 2015
Generics使って実装したGraph
View 00readme
Generics使って実装したGraph
TypeMember版 https://gist.github.com/seraphr/6858219
@seraphr
seraphr / 00readme
Last active Dec 24, 2015
TypeMember使って実装したGraph
View 00readme
TypeMember使って実装したGraph
generics版 https://gist.github.com/seraphr/6858351
@seraphr
seraphr / CanCompile.scala
Created Sep 26, 2013
Scalaのimplicitなものをimportした時のコンパイラの挙動が謎い。 仕様確認してないけど、仕様通りなの…? CompileErrorになるコードを2.8 / 2.9 で試してもコンパイルエラーだった。
View CanCompile.scala
// Implicitを上に書くとコンパイルできる
object Implicits {
implicit val v = 10
}
object ImplicitTest {
import Implicits._
def main(args: Array[String]): Unit = {
You can’t perform that action at this time.