Skip to content

Instantly share code, notes, and snippets.

View gakuzzzz's full-sized avatar

Manabu Nakamura gakuzzzz

View GitHub Profile
@gakuzzzz
gakuzzzz / pole2.hs
Last active August 29, 2015 14:04
pole2
import Control.Monad.State
type Birds = Int
type Pole = (Birds, Birds)
landLeft' :: Birds -> Pole -> Either Pole Pole
landLeft' n (left, right)
| abs ((left + n) - right) < 4 = Right (left + n, right)
| otherwise = Left (left + n, right)
@gakuzzzz
gakuzzzz / tx.scala
Last active August 29, 2015 14:04
ScalikeTx
import scalikejdbc._
case class Tx[+A] private (underlying: DBSession => A) {
def apply(s: DBSession): A = underlying(s)
def map[B](f: A => B): Tx[B] = Tx(underlying andThen f)
def flatMap[B](f: A => Tx[B]): Tx[B] = Tx(s => f(underlying(s))(s))
@gakuzzzz
gakuzzzz / _.scala
Created August 25, 2014 07:51
method to function
scala> def add(a: Int, b: Int): Int = a + b
add: (a: Int, b: Int)Int
scala> add(_)
<console>:9: error: missing parameter type for expanded function ((x$1) => add(x$1))
add(_)
^
<console>:9: error: not enough arguments for method add: (a: Int, b: Int)Int.
Unspecified value parameter b.
add(_)
@gakuzzzz
gakuzzzz / ActionBuilderZipper .scala
Last active August 29, 2015 14:09
ActionBuilderZipper
import play.api.mvc._
import scala.concurrent.Future
case class ZippedRequest[A, R1[_] <: Request[_], R2[_] <: Request[_]](_1: R1[A], _2: R2[A]) extends WrappedRequest[A](_1.asInstanceOf[Request[A]])
object ActionBuilderZipper {
private def zip[R1[_] <: Request[_], R2[_] <: Request[_]](b1: ActionBuilder[R1], b2: ActionBuilder[R2]): ActionBuilder[({type L[A] = ZippedRequest[A, R1, R2]})#L] = {
new ActionBuilder[({type L[A] = ZippedRequest[A, R1, R2]})#L] {
@gakuzzzz
gakuzzzz / 0_diff.scala
Last active August 29, 2015 14:10
文字列比較
@annotation.tailrec
def diff(s1: Seq[Char], s2: Seq[Char], acc: StringBuilder = new StringBuilder()): String = {
(s1, s2) match {
case (Nil , Nil) => acc.toString
case (x +: _ , Nil) => acc.append(s"[$x][$$END$$]").toString
case (Nil , y +: _) => acc.append(s"[$$END$$][$y]").toString
case (x +: xs, y +: ys) if x == y => diff(xs, ys, acc.append(x))
case (x +: _ , y +: _) => acc.append(s"[$x][$y]").toString
}
}
@gakuzzzz
gakuzzzz / play2-auth-social-sample.md
Last active August 29, 2015 14:11
play2-auth で OpenID とか Twitter OAuth とか OAuth2.0 とか

play2-auth で OpenID とか Twitter OAuth とか OAuth2.0 とか

この記事は Scala Advent Calendar 2014 の 12日目です。

昨日は @tototoshi さんの sbt でファイル変更をフックしてコンソールをクリアしつつコンパイルする でした。

明日は @qtamaki さんの 「関数プログラミング 珠玉のアルゴリズムデザイン」をScalaで実装してみる です。

というわけで play2-auth で OpenID とか Twitter OAuth とか OAuth2.0 とかをやるサンプルを作ってみたのですが、だいぶ時間切れになりました。

@gakuzzzz
gakuzzzz / fooo.txt
Created December 18, 2014 05:21
MalformedInputException
[fooo] $ test
[info] Compiling 1 Scala source to C:\Users\ma-nakamura\workspace\fooo\target\scala-2.11\classes...
[trace] Stack trace suppressed: run last *:doctestGenTests for the full output.
[error] (*:doctestGenTests) java.nio.charset.MalformedInputException: Input length = 1
[error] Total time: 5 s, completed 2014/12/18 14:06:38
[fooo] $ test
@gakuzzzz
gakuzzzz / hoge.md
Created December 19, 2014 15:11
doctest import
  /**
   * {{{
   * prop> import org.scalacheck.{Gen, Arbitrary}
   * prop> import org.scalacheck.Arbitrary._
   * prop> import foo.BarType
   * prop> val itemTypeGen = Gen.oneOf(BarType.values)
   * prop> implicit val barTypeArbitrary = Arbitrary(barTypeGen)
   * prop> (i: Int, t: BarType) => i > 10
   * }}}
@gakuzzzz
gakuzzzz / ci.md
Created February 24, 2015 11:59
CI サービス比較
機能\サービス Travis CI Circle CI Shippable Wercker BuildHive
Private Repo 有料 無料 無料 無料
@gakuzzzz
gakuzzzz / REPL.scala
Created March 3, 2015 17:58
Option.apply の使いどころ
scala> val f: (Option[Int], Int) => Option[Int] = _ orElse Some(_)
f: (Option[Int], Int) => Option[Int] = <function2>
scala> val seq: Seq[Int] = Seq(1,2,3)
seq: Seq[Int] = List(1, 2, 3)
scala> seq.foldLeft(Some(0))(f)
<console>:10: error: type mismatch;
found : (Option[Int], Int) => Option[Int]
required: (Some[Int], Int) => Some[Int]