Skip to content

Instantly share code, notes, and snippets.

View y-yu's full-sized avatar

YOSHIMURA Yuu y-yu

View GitHub Profile
@y-yu
y-yu / gacha.puml
Last active September 15, 2015 02:14
@startuml
participant ユーザー as User
participant アプリケーション as App
participant サーバー as Server
participant データベース as DB
Server -> User: マスクを公開
User -> App: ガチャを引くボタンを押す
@y-yu
y-yu / contravariant.scala
Last active August 29, 2015 14:28
contravariant
case class Function[-A](f: A => Unit)
def function[A](x: A): Unit = ()
trait SuperClass
trait SubClass extends SuperClass
val superClass = new SuperClass {}
val subClass = new SubClass {}
PartialFunction[SubClass, Unit]((x: SuperClass) => ())
@y-yu
y-yu / DatabaseAndHttpException.scala
Last active May 23, 2016 01:03
階層構造を容易に拡張できる例外 ref: http://qiita.com/yyu/items/07d56112bc42938aee05
object DatabaseAndHttpException {
implicit val databaseException = new :->[DatabaseException, DatabaseAndHttpException] {
def cast(a: DatabaseException): DatabaseAndHttpException =
DatabaseAndHttpException(s"database: ${a.m}")
}
implicit val httpException = new :->[HttpException, DatabaseAndHttpException] {
def cast(a: HttpException): DatabaseAndHttpException =
DatabaseAndHttpException(s"http: ${a.m}")
}
@y-yu
y-yu / Coproduct.scala
Last active August 29, 2015 14:26
FreeモナドとTagless FinalによるDependency InjectionのためのDSL ref: http://qiita.com/yyu/items/377513f17fec536b562e
object Coproduct {
implicit def coproductFunctor[F[_], G[_]](implicit F: Functor[F], G: Functor[G]) =
new Functor[({type L[A] = Coproduct[F, G, A]})#L] {
def map[A, B](a: Coproduct[F, G, A])(f: A => B): Coproduct[F, G, B] = a.value match {
case Left(e) => Coproduct[F, G, B](Left(F.map(e)(f)))
case Right(e) => Coproduct[F, G, B](Right(G.map(e)(f)))
}
}
}
@y-yu
y-yu / applicative2.scala
Created June 7, 2015 14:37
Applicative2
trait Applicative[F[_]] {
def pure[A](a: A): F[A]
def apply[A, B](f: F[A => B], a: F[A]): F[B]
}
trait Traversable[T[_]] {
def traverse[A, B, F[_] ](a: T[A])(g: A => F[B])(implicit f: Applicative[F]): F[T[B]]
}
trait Monoid[A] {
@y-yu
y-yu / applicative.scala
Created June 7, 2015 12:03
Applicative.scala
trait Applicative[F[_]] {
def pure[A](a: A): F[A]
def apply[A, B](f: F[A => B], a: F[A]): F[B]
}
trait Traversable[T[_], F[_]] {
def traverse[A, B](f: A => F[B], a: T[A]): F[T[B]]
}
trait Monoid[A] {
package hlist
sealed trait List[A]
case class Cons[A](h: A, t: List[A]) extends List[A]
case object Nil extends List[Nothing]
sealed trait HList
case class :*:[+A, +B <: HList](h: A, t: B) extends HList {
def :*:[C](x: C): C :*: A :*: B = hlist.:*:(x, this)
}
@y-yu
y-yu / RegexToLLVM.scala
Created May 11, 2015 14:09
Compiler Regular Expression to LLVM
sealed trait Regex
case object Empty extends Regex
case class Let(c: Char) extends Regex
case class Con(a: Regex, b: Regex) extends Regex
case class Alt(a: Regex, b: Regex) extends Regex
case class Star(a: Regex) extends Regex
sealed trait Value
case class RInt(n: Int) extends Value
case class RStr(s: String) extends Value
@y-yu
y-yu / file10.scala
Last active August 29, 2015 14:20
VM型の正規表現エンジンを実装する ref: http://qiita.com/yyu/items/84b1a00459408d1a7321
def compile(re: Regex): List[VMInst] = {
def loop(re: Regex, n: Int): (List[VMInst], Int) = re match {
case Empty => (Nil, n)
case Let(c) => (List(C(c)), n + 1)
case Con(a, b) =>
val (c1, i1) = loop(a, n)
val (c2, i2) = loop(b, i1)
(c1 ++ c2, i2)
case Alt(a, b) =>
val (c1, i1) = loop(a, n + 1)
@y-yu
y-yu / Counting.scala
Last active August 29, 2015 14:19
トランポリン化とアキュムレータの性能比較 ref: http://qiita.com/yyu/items/cd689bf8e61d103c72aa
import fpinscala.tailrec._
object Counting {
def normal (n : Int) : Int =
if (n == 0) 0
else 1 + normal(n - 1)
def cps (n : Int) : Int = {
def loop (i : Int, k : Int => TailRec[Int]) : TailRec[Int] =
if (i == 0) k(0)