Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Functional programming seems super cool but ...

12 views

Published on

A good understanding of different paradigms is not an easy issue - it is like changing the culture chip and even more about some kind of mind-shift.

Once we got it, your programming toolbox in terms of abstraction will be much richer than ever before.

Let's see how we can slightly move into the functional paradigm world:

- how we can express all well-known abstractions from OOP

- classes and their relations and communication, polymorphism and some important design patterns as well

Let's compare and combine the best things from these two paradigms. Let's do it together and become better programmers!

Published in: Software
  • Be the first to comment

  • Be the first to like this

Functional programming seems super cool but ...

  1. 1. OOP -> FPOOP -> FP MINDSHIFTMINDSHIFT GRASP THE BASIC IN LESS THANGRASP THE BASIC IN LESS THAN 10 MINUTES10 MINUTES 1
  2. 2. THE ANONYMOUS FUNCTIONTHE ANONYMOUS FUNCTION POWERED BYPOWERED BY The λ expression allows us dynamically create: functions and lexical scope RESULT (x: Int) => x + 1 val plusOne : Int => Int = (n: Int) => n + 1 Run Reset λ 2
  3. 3. FUNCTIONS AS A FIRST-CLASSFUNCTIONS AS A FIRST-CLASS CITIZENCITIZEN First-class function is a function that can be: passed | returned | stored as ANY other data without ANY syntactic restrictions RESULT val lessThen = new Function2[Int, Int, Boolean] { def apply(a: Int, b: Int) = a < b } //import scala.collection.immutable._ Run Reset 3
  4. 4. HIGHER ORDER FUNCTIONSHIGHER ORDER FUNCTIONS Technically is a function that takes as a argument other function/s We gain: Computation extracting (Divide & Conquer) and Function composition availability (DRY) RESULT val addOne = (x: Int) => x + 1 val double = (x: Int) => x * 2 val hiOrderFunction = (aFirstClassFunction: Int => Int, x: Int) => aFirstClassFunction(x) Run Reset 4
  5. 5. CLOSURES AND PARTIALCLOSURES AND PARTIAL APPLICATION TOGETHERAPPLICATION TOGETHER Enclosing ANY data inside the lambda function. RESULT val aClosure = (firstClassF : Int => Int) => (x: Int) => firstClassF(x) Run Reset 5
  6. 6. PARTIAL APPLICATION - GIVE APARTIAL APPLICATION - GIVE A SECOND THOUGHTSECOND THOUGHT result = f(x)(y)(z) <=> f1 = f(x) => f2 = f1(y) => f2(z) = result 6
  7. 7. DATA MODELING FP APPROACHDATA MODELING FP APPROACH ADT - Algebraic Data Types case class Status(code: Int, statusMessage: String) sealed trait Result object Success extends Result object Failed extends Result type aFunctionalType = Status => Result // ADT :: Product Type1 2 // ADT :: Sum Type3 4 5 6 // ADT :: Exponential Type7 8 7
  8. 8. ALGEBRAIC STRUCTURESALGEBRAIC STRUCTURES THE BRIDGE BETWEENTHE BRIDGE BETWEEN Mathematics Programming Domain modeling Sets Types Entities, value objects Operators Functions Business behavior Axioms Properties Business rules 8
  9. 9. "Good OOP is nothing more then FP in disguise." "Design patterns are for fill the gap of the OOP abstractions lack." "With the power paradigm you don`t need any framework for DI." "In FP it is a common practice to compose and build computation pipelines and defere evaluation until the end." "In FP it is easy, and expected, to factor out common functionality into generic, reusable components that can be composed each other." "Data structures just are. They don`t do anything." But... Where IS the power of FP that we talk about?! 9 . 1
  10. 10. TAKEAWAYSTAKEAWAYS => lambda and lexical scope => first class functions => higher order functions => partial application, currying, closures => function composition/chain => ADT are power for domain/dsl modeling 10 . 1
  11. 11. ¡MUCHAS GRACIAS!¡MUCHAS GRACIAS! THANKS!THANKS! 11

×