Skip to content

Instantly share code, notes, and snippets.

@rirakkumya
rirakkumya / style.scala
Created March 17, 2013 14:27
coding style
//少人数スタイル
Some(140000) map (_ + 5000) map (_ - 80000)
//多人数スタイル
val rentSupport:Int => Int = _ + 5000
val dormFee:Int => Int = _ - 80000
val salaryCalc = rentSupport andThen dormFee
Some(140000) map salaryCalc
@rirakkumya
rirakkumya / salad.scala
Last active December 14, 2015 05:49
サラダの圏のトマト姫をscalaで実装してみた
trait サラダ圏 {
//対象を定義
sealed trait Salad
//マヨサラダ
case class MaSalad(sa: Salad) extends Salad
//ツナサラダ
case class TuSalad(sa: Salad) extends Salad
//ツナマヨサラダ
case class TuMaSalad(sa: Salad) extends Salad
@rirakkumya
rirakkumya / FunctorialDataModel2.scala
Last active December 13, 2015 21:28
「関手的データモデル入門 2:統一的に制約を書く方法」をscalaで実装してみた
// ネタ元:
// 関手的データモデル入門 2:統一的に制約を書く方法
// http://d.hatena.ne.jp/m-hiyama/20130218/1361145879
//http://www.chimaira.org/img3/funcdata-sample-2.gif
object FunctorialDataModel{
type Employee = Employee.EmployeeInfo
type EmployeeNum = Int
object Employee {
case class EmployeeInfo(num: Int, office: Office)
@rirakkumya
rirakkumya / FunctorialDataModel.scala
Created February 14, 2013 12:19
「衝撃的なデータベース理論・関手的データモデル」をscalaで実装してみた
// ネタ元:
// 衝撃的なデータベース理論・関手的データモデル 入門
// http://d.hatena.ne.jp/m-hiyama/20130211/1360577040
//データ保持用
object Strage {
trait Strage[A] {
def get: A
}
}
@rirakkumya
rirakkumya / sample.scala
Created February 2, 2013 13:13
型クラスを使って契約種別に従い計算を行うビジネスロジックを実装してみた
/*
概要:入力されたCSVの契約種別コードに従って計算を行い、結果を出力する
仕様:
CSV入力パターン: [名前],[契約種別コード(A or B or C)],[基本料金],[使用量1段階],[使用量2段階]
※文字列はクォート無し
sample: "foo,A,3000,,","bar,B,1000,200,","foobar,C,1000,200,300"
契約種別コード別料金計算仕様:
A: 基本料金
B: 基本料金 + 使用量1段階 * 0.1
C: 基本料金 + 使用量1段階 * 0.02 + 使用量2段階 * 0.3
@rirakkumya
rirakkumya / gist:4619788
Created January 24, 2013 10:36
いつものやつ
def toDate[A:DateMagnet](x:A) = implicitly[DateMagnet[A]].convert(x)
trait DateMagnet[A]{
type Result
def convert(x:A):Result
}
object DateMagnet {
implicit def fromInt = new DateMagnet[(Int, Int, Int)] {
type Result = java.util.Calendar
@rirakkumya
rirakkumya / gist:4166753
Created November 29, 2012 04:05
scala Error
Caused by: java.lang.RuntimeException: cannot serialize an immutable.HashSet where all items have the same 32-bit hash code
at scala.sys.package$.error(package.scala:27)
at scala.collection.immutable.HashSet$HashSetCollision1.writeObject(HashSet.scala:168)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
@rirakkumya
rirakkumya / Application.scala
Created May 23, 2012 13:26
[scala][play2] monadic fizzbuzz
package controllers
import play.api._
import play.api.mvc._
import EA._
object Application extends Controller {
implicit def either2Bind(s:Either[String,Int]) = new Bind(s)
implicit def int2Bind(x:Int) = new Bind[String,Int](Right(x))
@rirakkumya
rirakkumya / gist:2724922
Created May 18, 2012 12:06
List[Either[Throwable,_]]のリスト中にLeftが1個以上あったら最初のLeftを返し、Leftが1個もなければ、Rightを1個のリストにまとめて返すコード
def f[a] = (d:List[Either[Throwable,a]]) =>
d.collect{case Left(x) => x}.headOption.toLeft(d.map{case Right(x) => x})
scala> Some(List(Right("4"),Right("3"))) map f
res11: Option[Product with Either[Throwable,List[java.lang.String]] with Serializable] = Some(Right(List(4, 3)))
scala> Some(List(Right("4"),Left(new RuntimeException),Right("3"))) map f
res12: Option[Product with Either[Throwable,List[java.lang.String]] with Serializable] = Some(Left(java.lang.RuntimeException))
scala> Some(List(Right("4"),Left(new RuntimeException("a")),Right("3"),Left(new RuntimeException("b")))) map f
@rirakkumya
rirakkumya / sample.scala
Created May 16, 2012 15:35
[scala] Eitherのサンプル
scala> val l = Left(3)
l: Left[Int,Nothing] = Left(3)
scala> val r = Right(3)
r: Right[Nothing,Int] = Right(3)
scala> def calc = (x:Either[Int,Int]) => x.right.map(_ + 2).left.map(_ * 2).right.map(_ + 3)
culc: Either[Int,Int] => Product with Either[Int,Int] with Serializable
scala> calc(r)