SlideShare une entreprise Scribd logo
1  sur  21
Flexible Data Representation with Fixpoint Types
Dave Cleaver
January 24th, 2017
Some Simple Data
case class Employee(first: String, last: String, manages: List[Employee] = List.empty)
2
Some Simple Data
case class Employee(first: String, last: String, manages: List[Employee] = List.empty)
3
Employee("Nina", "McInroe", List(
Employee("Bill", "Lumbergh", List(
Employee("Peter", "Gibbons"),
Employee("Samir", "Nagheenanajar"),
Employee("Michael", "Bolton"))),
Employee("Dom", "Portwood")))
Storing Employees
case class StoredEmployee (first: String, last: String, manages: List[String] = List.empty)
4
Storing Employees
case class StoredEmployee (first: String, last: String, manages: List[String] = List.empty)
5
StoredEmployee("Nina", "McInroe", List("blumbergh", "dportwood"))
StoredEmployee("Bill", "Lumbergh", List("pgibbons", "snagheenanajar", "mbolton"))
StoredEmployee("Peter", "Gibbons")
StoredEmployee("Samir", "Nagheenanajar")
StoredEmployee("Michael", "Bolton")
StoredEmployee("Dom", "Portwood")
Abstract out the Type
case class EmployeeF[A](first: String, last: String, manages: List[A] = List.empty)
6
Works for the Simple Type
type StoredEmployee = EmployeeF[String]
7
Works for the Simple Type
type StoredEmployee = EmployeeF[String]
8
EmployeeF("Nina", "McInroe", List("blumbergh", "dportwood"))
EmployeeF("Bill", "Lumbergh", List("pgibbons", "snagheenanajar", "mbolton"))
EmployeeF("Peter", "Gibbons")
EmployeeF("Samir", "Nagheenanajar")
EmployeeF("Michael", "Bolton")
EmployeeF("Dom", "Portwood")
But the Recursive Type…
type Employee = EmployeeF[EmployeeF[EmployeeF[...]]]
9
Generalized Recursion at the Value Level
def fix[A](f: (=> A) => A): A = {
lazy val a: A = f(a)
a
}
10
Generalized Recursion at the Value Level
def fix[A](f: (=> A) => A): A = {
lazy val a: A = f(a)
a
}
val factorial = fix[Int => Int](factorial => n => {
if(n == 1) 1 else n * factorial.apply(n - 1)
} )
11
Fix at the Type Level
case class Fix[F[_]](unfix: F[Fix[F]])
12
Employee Fixed
type Employee = Fix[EmployeeF]
13
Employee Fixed
type Employee = Fix[EmployeeF]
14
Fix[EmployeeF](EmployeeF("Nina", "McInroe",
List(Fix(EmployeeF("Bill", "Lumbergh", List(
Fix(EmployeeF("Peter", "Gibbons")),
Fix(EmployeeF("Samir", "Nagheenanajar")),
Fix(EmployeeF("Michael", "Bolton"))))),
Fix(EmployeeF("Dom", "Portwood"))))
Simple JSON Encoding
implicit def encodeEmployee[A: Encoder]: Encoder[EmployeeF[A]] =
Encoder.forProduct3("first", "last", "manages")(e =>
(e.first, e.last, e.manages))
15
Encoding Fix
def encodeFix[F[_]](fEncoder: Encoder[Fix[F]] => Encoder[F[Fix[F]]]): Encoder[Fix[F]] =
new Encoder[Fix[F]] {
final def apply(fixF: Fix[F]) = fEncoder(this).apply(fixF.unfix)
}
16
Encoding Fix
def encodeFix[F[_]](fEncoder: Encoder[Fix[F]] => Encoder[F[Fix[F]]]): Encoder[Fix[F]] =
new Encoder[Fix[F]] {
final def apply(fixF: Fix[F]) = fEncoder(this).apply(fixF.unfix)
}
implicit val fixedEncoder = encodeFix(encodeEmployee(_: Encoder[Fix[EmployeeF]]))
17
EmployeeF is a Functor
implicit object EmployeeFunctor extends Functor[EmployeeF] {
def map[A, B](fa: EmployeeF[A])(f: A => B): EmployeeF[B] =
fa.copy(manages = fa.manages.map(f))
}
18
Programs and Data
type EmployeeTask[A] = Task[EmployeeF[A]]
def fetch(id: String): EmployeeTask[String] = ???
19
Programs and Data
type EmployeeTask[A] = Task[EmployeeF[A]]
def fetch(id: String): EmployeeTask[String] = ???
def loadEmployee(id: String): Fix[EmployeeTask] =
Fix[EmployeeTask](fetch(id).map(_.map(loadEmployee)))
20
Flexible Data Representation with Fixpoint Types
• Abstract your data
- Reduce duplicate data types
- While gaining extra power
• Other useful Recursive Types
- case class Free[F[_], A](run: Either[A, F[Free[F, A]])
- case class Cofree[F[_], A](head: A, tail: F[Cofree[F, A]])
• Twitter: @dscleaver
21

Contenu connexe

Dernier

KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfrs7054576148
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 

Dernier (20)

KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 

En vedette

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

En vedette (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Flexible Data Representation with Fixpoint Types

  • 1. Flexible Data Representation with Fixpoint Types Dave Cleaver January 24th, 2017
  • 2. Some Simple Data case class Employee(first: String, last: String, manages: List[Employee] = List.empty) 2
  • 3. Some Simple Data case class Employee(first: String, last: String, manages: List[Employee] = List.empty) 3 Employee("Nina", "McInroe", List( Employee("Bill", "Lumbergh", List( Employee("Peter", "Gibbons"), Employee("Samir", "Nagheenanajar"), Employee("Michael", "Bolton"))), Employee("Dom", "Portwood")))
  • 4. Storing Employees case class StoredEmployee (first: String, last: String, manages: List[String] = List.empty) 4
  • 5. Storing Employees case class StoredEmployee (first: String, last: String, manages: List[String] = List.empty) 5 StoredEmployee("Nina", "McInroe", List("blumbergh", "dportwood")) StoredEmployee("Bill", "Lumbergh", List("pgibbons", "snagheenanajar", "mbolton")) StoredEmployee("Peter", "Gibbons") StoredEmployee("Samir", "Nagheenanajar") StoredEmployee("Michael", "Bolton") StoredEmployee("Dom", "Portwood")
  • 6. Abstract out the Type case class EmployeeF[A](first: String, last: String, manages: List[A] = List.empty) 6
  • 7. Works for the Simple Type type StoredEmployee = EmployeeF[String] 7
  • 8. Works for the Simple Type type StoredEmployee = EmployeeF[String] 8 EmployeeF("Nina", "McInroe", List("blumbergh", "dportwood")) EmployeeF("Bill", "Lumbergh", List("pgibbons", "snagheenanajar", "mbolton")) EmployeeF("Peter", "Gibbons") EmployeeF("Samir", "Nagheenanajar") EmployeeF("Michael", "Bolton") EmployeeF("Dom", "Portwood")
  • 9. But the Recursive Type… type Employee = EmployeeF[EmployeeF[EmployeeF[...]]] 9
  • 10. Generalized Recursion at the Value Level def fix[A](f: (=> A) => A): A = { lazy val a: A = f(a) a } 10
  • 11. Generalized Recursion at the Value Level def fix[A](f: (=> A) => A): A = { lazy val a: A = f(a) a } val factorial = fix[Int => Int](factorial => n => { if(n == 1) 1 else n * factorial.apply(n - 1) } ) 11
  • 12. Fix at the Type Level case class Fix[F[_]](unfix: F[Fix[F]]) 12
  • 13. Employee Fixed type Employee = Fix[EmployeeF] 13
  • 14. Employee Fixed type Employee = Fix[EmployeeF] 14 Fix[EmployeeF](EmployeeF("Nina", "McInroe", List(Fix(EmployeeF("Bill", "Lumbergh", List( Fix(EmployeeF("Peter", "Gibbons")), Fix(EmployeeF("Samir", "Nagheenanajar")), Fix(EmployeeF("Michael", "Bolton"))))), Fix(EmployeeF("Dom", "Portwood"))))
  • 15. Simple JSON Encoding implicit def encodeEmployee[A: Encoder]: Encoder[EmployeeF[A]] = Encoder.forProduct3("first", "last", "manages")(e => (e.first, e.last, e.manages)) 15
  • 16. Encoding Fix def encodeFix[F[_]](fEncoder: Encoder[Fix[F]] => Encoder[F[Fix[F]]]): Encoder[Fix[F]] = new Encoder[Fix[F]] { final def apply(fixF: Fix[F]) = fEncoder(this).apply(fixF.unfix) } 16
  • 17. Encoding Fix def encodeFix[F[_]](fEncoder: Encoder[Fix[F]] => Encoder[F[Fix[F]]]): Encoder[Fix[F]] = new Encoder[Fix[F]] { final def apply(fixF: Fix[F]) = fEncoder(this).apply(fixF.unfix) } implicit val fixedEncoder = encodeFix(encodeEmployee(_: Encoder[Fix[EmployeeF]])) 17
  • 18. EmployeeF is a Functor implicit object EmployeeFunctor extends Functor[EmployeeF] { def map[A, B](fa: EmployeeF[A])(f: A => B): EmployeeF[B] = fa.copy(manages = fa.manages.map(f)) } 18
  • 19. Programs and Data type EmployeeTask[A] = Task[EmployeeF[A]] def fetch(id: String): EmployeeTask[String] = ??? 19
  • 20. Programs and Data type EmployeeTask[A] = Task[EmployeeF[A]] def fetch(id: String): EmployeeTask[String] = ??? def loadEmployee(id: String): Fix[EmployeeTask] = Fix[EmployeeTask](fetch(id).map(_.map(loadEmployee))) 20
  • 21. Flexible Data Representation with Fixpoint Types • Abstract your data - Reduce duplicate data types - While gaining extra power • Other useful Recursive Types - case class Free[F[_], A](run: Either[A, F[Free[F, A]]) - case class Cofree[F[_], A](head: A, tail: F[Cofree[F, A]]) • Twitter: @dscleaver 21