Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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
trait Cat {
type A
type B
def f:A => B
}
trait Cat {
type A
type B
type C
def f:A => B
def g:B => C
def f_g:A => C = f andThen g
}
case class Address(zip:String)
object Addr {
type 住所 = Address
type 郵便番号 = String
def zip:住所 => 郵便番号 = _.zip
}
Addr.zip(Address("100-0000")) //-> Addr.郵便番号 = 100-0000
List(Address("100-0000"),Address("200-0000")) map Addr.zip 
//-> List[Addr.郵便番号] = List(100-0000, 200-0000)
Some(Address("100-0000")) map Addr.zip
//-> Option[Addr.郵便番号] = Some(100-0000)
import Addr._
def lift(f: 住所 => 郵便番号): Option[住所] => Option[郵便番号] = _ map f
lift(Addr.zip)(Some(Address("100-1000")))
// -> Option[Addr.郵便番号] = Some(100-1000)