Skip to content

Instantly share code, notes, and snippets.

@rirakkumya
Last active December 13, 2015 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rirakkumya/4977080 to your computer and use it in GitHub Desktop.
Save rirakkumya/4977080 to your computer and use it in GitHub Desktop.
「関手的データモデル入門 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)
def num: Employee => EmployeeNum = _.num
def office: Employee => Office = _.office
//http://www.chimaira.org/img3/funcdata-sample-3.gif
//officeNum = office;Office.num : Employee→OfficeNum
//式と同じように書ける
val officeNum = office andThen Office.num: Employee => OfficeNum
//http://www.chimaira.org/img3/funcdata-sample-4.gif
//officeAddr = office;addr : Employee→String
val officeAddr = office andThen Office.addr: Employee => String
}
type Office = Office.OfficeInfo
type OfficeNum = Int
object Office {
case class OfficeInfo(num: Int, chief: Employee, addr: String)
def num: Office => OfficeNum = _.num
def chief: Office => Employee = _.chief
def addr: Office => String = _.addr
//http://www.chimaira.org/img3/funcdata-sample-3.gif
//chiefNum = chief;Employee.num : Office→EmployeeNum
val chiefNum = chief andThen Employee.num: Office => EmployeeNum
//http://www.chimaira.org/img3/funcdata-sample-6.gif
//chief;office = idOffice : Office→Office
val id_Office = chief andThen Employee.office: Office => Office
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment