SlideShare a Scribd company logo
1 of 33
Download to read offline
Recursion
schemes
in Scala
and also fixed point data types
Today we will speak about freaky
functional stuff, enjoy :)
Arthur Kushka // @Arhelmus
Yo
a method for structuring programs mainly
as sequences of possibly nested function
procedure calls.
programmingFunctional
((x: Array[Byte]) => x)
   .andThen(encodeBase64)
   .andThen(name => s"Hello $name")
   .andThen(println)
everywhere!
Recursive schemas
Lists and trees
Filesystems
Databases
list match {
   // Cons(1, Cons(2, Cons(3, Nil)))
   case 1 :: 2 :: 3 :: Nil =>
   case Nil =>
 }
Example
01
Sum
Multiply
Divide
Square
Math equasionevaluation
Lets define our DSL
sealed trait Exp
case class IntValue(value: Int) extends Exp
case class DecValue(value: Double) extends Exp
case class Sum(exp1: Exp, exp2: Exp) extends Exp
case class Multiply(exp1: Exp, exp2: Exp) extends Exp
case class Divide(exp1: Exp, exp2: Exp) extends Exp
case class Square(exp: Exp) extends Exp
Example
equation
Sum(
   Square(
     IntValue(4)
   ),
   Multiply(
     DecValue(3.3),
     IntValue(4)
   )
 )
4^2 + 3.3 * 4
val evaluate: Exp => Double = {
   case DecValue(value) => value
   case IntValue(value) => value.toDouble
   case Sum(exp1, exp2) =>
      evaluate(exp1) + evaluate(exp2)
   case Multiply(exp1, exp2) =>
      evaluate(exp1) * evaluate(exp2)
   case Divide(exp1, exp2) =>
      evaluate(exp1) / evaluate(exp2)
   case Square(exp) =>
     val v = evaluate(exp)
     v * v
 }
val stringify: Exp => String = {
   case DecValue(value) => value.toString
   case IntValue(value) => value.toString
   case Sum(exp1, exp2) =>
     s"${stringify(exp1)} + ${stringify(exp2)}"
   case Square(exp) =>
     s"${stringify(exp)} ^ 2"
   case Multiply(exp1, exp2) =>
     s"${stringify(exp1)} * ${stringify(exp2)}"
   case Divide(exp1, exp2) =>
     s"${stringify(exp1)} / ${stringify(exp2)}"
 }
It will work!
but...
evaluate(expression) // prints 29.2
stringify(expression) // prints 4 ^ 2 + 3.3 * 4
There are a lot of
mess
   case Sum(exp1: Exp, exp2: Exp) =>
      evaluate(exp1) + evaluate(exp2)
And not only...
Problem of partial
interpretation
val optimize: Exp => Exp = {
   case Multiply(exp1, exp2) if exp1 == exp2 =>
     Square(optimize(exp1))
   case other => ???
}
Lets improve
going to
result
types02
Generalize it
sealed trait Exp[T]
case class IntValue[T](value: Int) extends Exp[T]
case class DecValue[T](value: Double) extends Exp[T]
case class Sum[T](exp1: T, exp2: T) extends Exp[T]
case class Multiply[T](exp1: T, exp2: T) extends Exp[T]
case class Divide[T](exp1: T, exp2: T) extends Exp[T]
case class Square[T](exp: T) extends Exp[T]
val expression: Exp[Exp[Exp[Unit]]] =
Sum[Exp[Exp[Unit]]](
   Square[Exp[Unit]](
     IntValue[Unit](4)
   ),
   Multiply[Exp[Unit]](
     DecValue[Unit](3.3),
     IntValue[Unit](4)
   )
 )
Nesting types issue
its like a hiding of part that doesn't matter
fixed point types
F[_] is a generic type with a hole, after
wrapping of Exp with that we will have
Fix[Exp] type.
let's add
typehack
case class Fix[F[_]](unFix: F[Fix[F]])
val expression: Fix[Exp] = Fix(Sum(
   Fix(Square(
     Fix(IntValue(4))
   )),
   Fix(Multiply(
     Fix(DecValue(3.3)),
     Fix(IntValue(4))
   ))
 ))
Hacked code
case class Square[T](exp: T) extends Exp[T]
object Square {
  def apply(fixExp: Exp[Fix[Exp]]) = Fix[Exp](fixExp)
}
val expression: Fix[Exp] = Square(
   Square(
     Square(
       IntValue(4)
     )))
Let's do it cleaner
time to traverse
our structure
03
def map[F[_], A, B](fa: F[A])(f: A=>B): F[B]
Functor 911
Scalaz example
case class Container[T](data: T)
implicit val functor = new Functor[Container] {
override def map[A, B](fa: Container[A])
(f: A => B): Container[B] =
Container(f(fa.data))
}
functor.map(Container(1))(_.toString) // "1"
For cats you will have same code
implicit val functor = new Functor[Exp] {
override def map[A, B](fa: Exp[A])(f: A => B): Exp[B] =
fa match {
case IntValue(v) => IntValue(v)
case DecValue(v) => DecValue(v)
case Sum(v1, v2) => Sum(f(v1), f(v2))
case Multiply(v1, v2) => Multiply(f(v1), f(v2))
case Divide(v1, v2) => Divide(f(v1), f(v2))
case Square(v) => Square(f(v))
}
}
catamorphism
anamorphism
hylomorphism04
“Functional Programming with Bananas,
Lenses, Envelopes and Barbed Wire”, by
Erik Meijer
generalized folding operation
catamorphism
Lets define
Algebra
type Algebra[F[_], A] = F[A] => A
val evaluate: Algebra[Exp, Double] = {
   case IntValue(v) => v.toDouble
   case DecValue(v) => v
   case Sum(v1, v2) => v1 + v2
   case Multiply(v1, v2) => v1 * v2
   case Divide(v1, v2) => v1 / v2
   case Square(v) => Math.sqrt(v)
 }
So as a result
val expression: Fix[Exp] = Sum(
   Multiply(IntValue(4), IntValue(4)),
   Multiply(DecValue(3.3), IntValue(4))
 )
import matryoshka.implicits._
expression.cata(evaluate) // 29.2
do you remember about partial interpretation?
Extra profit
val optimize: Algebra[Exp, Fix[Exp]] = {
   case Multiply(Fix(exp), Fix(exp2)) if exp == exp2 =>      
      Fix(Square(exp))
   case other => Fix[Exp](other)
 }
import matryoshka.implicits._
expression.cata(optimize).cata(stringify) // 4 ^ 2 + 3.3 * 4
val evaluate: Exp => Double = {
   case DecValue(value) => value
   case IntValue(value) => value.toDouble
   case Sum(exp1, exp2) =>
      evaluate(exp1) + evaluate(exp2)
   case Multiply(exp1, exp2) =>
      evaluate(exp1) * evaluate(exp2)
   case Divide(exp1, exp2) =>
      evaluate(exp1) / evaluate(exp2)
   case Square(exp) =>
     val v = evaluate(exp)
     v * v
 }
type Algebra[F[_], A]
val evaluate: Algebra[Exp, Double] = {
   case IntValue(v) => v.toDouble
   case DecValue(v) => v
   case Sum(v1, v2) => v1 + v2
   case Multiply(v1, v2) => v1 * v2
   case Divide(v1, v2) => v1 / v2
   case Square(v) => Math.sqrt(v)
 }
catamorphism: F[_] => A 
anamorphism: A => F[_] 
hylomorphism: F[_] => A => F[_] 
Just good to
know
Fixed point types is perfect to create
DSLs
Recursion schemas is composable
All you need to use that stuff, you
already know from Scala
Summary
thank you.
any questions?

More Related Content

Similar to Recursion schemes in Scala

R short-refcard
R short-refcardR short-refcard
R short-refcard
conline
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
Tomasz Wrobel
 

Similar to Recursion schemes in Scala (20)

The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
 
Spark workshop
Spark workshopSpark workshop
Spark workshop
 
R short-refcard
R short-refcardR short-refcard
R short-refcard
 
Meet scala
Meet scalaMeet scala
Meet scala
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
 
Scala training workshop 02
Scala training workshop 02Scala training workshop 02
Scala training workshop 02
 
Files,blocks and functions in R
Files,blocks and functions in RFiles,blocks and functions in R
Files,blocks and functions in R
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Short Reference Card for R users.
Short Reference Card for R users.Short Reference Card for R users.
Short Reference Card for R users.
 
Reference card for R
Reference card for RReference card for R
Reference card for R
 
Scala Bootcamp 1
Scala Bootcamp 1Scala Bootcamp 1
Scala Bootcamp 1
 
Introducing scala
Introducing scalaIntroducing scala
Introducing scala
 
Functional programming with_scala
Functional programming with_scalaFunctional programming with_scala
Functional programming with_scala
 
R Programming Reference Card
R Programming Reference CardR Programming Reference Card
R Programming Reference Card
 
A bit about Scala
A bit about ScalaA bit about Scala
A bit about Scala
 
R command cheatsheet.pdf
R command cheatsheet.pdfR command cheatsheet.pdf
R command cheatsheet.pdf
 
@ R reference
@ R reference@ R reference
@ R reference
 
R language introduction
R language introductionR language introduction
R language introduction
 
C# programming
C# programming C# programming
C# programming
 

Recently uploaded

Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
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...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
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
dharasingh5698
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
 

Recently uploaded (20)

(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
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
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
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
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
 
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...
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
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
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
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
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
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 Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 

Recursion schemes in Scala