Skip to content

Instantly share code, notes, and snippets.

@lyricallogical
lyricallogical / rpscala69
Created February 15, 2012 10:23
rpscala69
!SLIDE
sclaz7 の scalaz.Iteratee の紹介
================================
!SLIDE
発端
----
> @halcat0x15a: @lyrical_logical 7のiterateeが別物になってて読めないです・・・・ 11:38 PM - 14 Feb 12
parseOpt(request.body) map { json =>
(json \\ "events" \\ "message").values
} collect { case msg : Map[String, String] =>
hoge
} getOrElse {
huga
}
@lyricallogical
lyricallogical / daigakusei.v
Created February 26, 2012 04:40 — forked from erutuf/daigakusei.v
ボクには ; 使ったのは気持ちはなんとなくわかるけど imihu だったので一つずつ証明した。
Inductive even : nat -> Prop :=
| even_O : even 0
| even_S : forall n, odd n -> even (S n)
with odd : nat -> Prop :=
| odd_S : forall n, even n -> odd (S n).
Scheme even_mut := Induction for even Sort Prop
with odd_mut := Induction for odd Sort Prop.
Theorem daigakuseinosuugakuryoku : forall n m,
@lyricallogical
lyricallogical / cakepattern.scala
Created April 13, 2012 06:14
単に delegate するような何かを作っておけば cake pattern でも何かできるみたいな気持ち
trait Abs { def f(): Int }
trait ConA extends Abs { def f() = 1 }
trait ConB extends Abs { abstract override def f() = super.f + 1 }
trait Delegate extends Abs {
val abs: Abs
def f() = abs.f
}
class A { this: Abs =>
@lyricallogical
lyricallogical / gist:2414620
Created April 18, 2012 16:13
なんともならない
def withRight[A, B] = new { def apply[R](f: (Either[A, B] => Either.RightProjection[A, B]) => R) = f(_.right) }
def withLeft[A, B] = new { def apply[R](f: (Either[A, B] => Either.LeftProjection[A, B]) => R) = f(_.left) }
withRight[Int, Int] { implicit ev =>
for {
x <- Right(1)
y <- Right(2)
} yield x + y // => Right(3)
for {
x <- Right(1)
@lyricallogical
lyricallogical / gist:2415136
Created April 18, 2012 17:13
特に面白みはない
trait Zero[A] { val zero: A }
implicit val IntZero = new Zero[Int] { val zero = 0 }
implicit def RightProjectionWithFilter[A, B](r: Either.RightProjection[A, B])(implicit azero: Zero[A]) = new {
class WithFilter(p: B => Boolean) {
def map[C](f: B => C): Either[A, C] = r.filter(p).getOrElse(Left(azero.zero)).right.map(f)
def flatMap[AA >: A, C](f: B => Either[AA, C]): Either[AA, C] = r.filter(p).getOrElse(Left(azero.zero)).right.flatMap(f)
def withFilter(p0: B => Boolean): WithFilter = new WithFilter(b => p(b) && p0(b))
}
def withFilter(p: B => Boolean): WithFilter = new WithFilter(p)
@lyricallogical
lyricallogical / rank2polymorphism_and_existentialttype.ml
Created August 26, 2012 08:55
なんか rank2-polymorphism と existential type が欲しかったけど無かったので first-class module を使った結果がこれだよ
type 'a node =
| A: int -> int node
| B: string -> string node
module type Exists = sig
type t
val ret: t node
end
module type PolyFun = sig
@lyricallogical
lyricallogical / parfun1.scala
Created August 31, 2012 08:49
難しいです
lazy val fab: PartialFunction[Base, Int] = {
case A(base) => 1 + fabc(base) // PartialFunction#apply の呼び出し。危ない!
case B(left, right) => 1 + fabcd(left) + fabcd(right) // 同様
}
// 全域関数だと scala コンパイラにはわからない!
lazy val fabcd: PartialFunction[Base, Int] = fab orElse {
case C => 1
case D => 2
}
@lyricallogical
lyricallogical / rpscala88
Created September 26, 2012 09:50
rpscala88
!SLIDE
Play 2.0 の Action と BodyParser について学ぼう
----------------
!SLIDE
自己紹介
----------------
* 望月 慎也
* @lyrical_logical
* 株式会社レピダム
@lyricallogical
lyricallogical / 1_bad.scala
Created September 29, 2012 12:27
ocaml like module(functor) programming in scala
imoprt Iteratee._
lazy val takeOdds = {
val aux = for {
one <- headOption[Byte[Array]] // ダサい
_ <- headOption[Byte[Array]] // ダサい
} yield one
for {
odds <- aux