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