This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//少人数スタイル | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ネタ元: | |
// 関手的データモデル入門 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ネタ元: | |
// 衝撃的なデータベース理論・関手的データモデル 入門 | |
// http://d.hatena.ne.jp/m-hiyama/20130211/1360577040 | |
//データ保持用 | |
object Strage { | |
trait Strage[A] { | |
def get: A | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
概要:入力された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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |