Functional programming

 

@joelcorrea_        @humbertostreb















Martin Odersky -> 2003

Current stable version = 2.11.1

http://www.slideshare.net/mslinn/scala-adoption-by-enterprises


James Gosling


"If I were to pick a language today other than Java, it would be Scala"

James Strachan




"I can honestly say if someone had shown me the Programming Scala book back in 2003 I'd probably have never created Groovy"

 scala.png

Concepts

  • Higher order functions

  • Pure function

  • Laziness

  • Pattern matching

  • Partial application

  • State / Side effects

  • Composition

Functions = Transformations over data

How difficult is it to manage state?

- to Debug?
- to Test?
- to Reproduce bugs?

eclipse-debugging-preview.jpg

Side effects


State discipline

Have you seen Null Pointers?

Pure functions



def add(x: Int, y: Int) = x + y

- Same result value given the same arguments
- Has no side effects


Map



Map in Code


> val people = List(Person("a", 20), Person("b", 10))people: List[Person] = List(Person(a,20), Person(b,10))
> people.map(_.age)List[Int] = List(20, 10) 

Reduce

Reduce in Code



> Seq(20, 10) reduce { (accum, current) => accum + current } Int = 30

Even better:

> Seq(20, 10) reduce(_ + _)
Int = 30








How to deal with complexity?

Object Oriented...



  • Types (classes, interfaces, etc)
  • Abstractions
  • Inheritance
  • Polymorphism
  • Design Patterns / Principles

Functional...


Math: Function composition


Transformations...

Example 


scala> def length(str:String) = str.size

scala> def simpleValidation(i:Int) = i<5

scala> def isValid = length _ andThen simpleValidation _

scala> isValid("1")
res1: Boolean = true

scala> isValid("1234567890")
res2: Boolean = false 

What was that?



length _ andThen simpleValidation _

In other words...


simpleValidation( length( input ) )


Remember the transformations


scala> def length(str:String) : Int = str.size
String => Int

scala> def simpleValidation(i:Int) : Boolean = i<5 
Int => Boolean

scala> def isValid = length _ andThen simpleValidation _
String => Boolean

So you'll compose...


andThen
f _ andThen g _
g( f(x) )
compose
f _ compose g _
f( g(x) )
orElse (partial functions)
f orElse g

Higher order functions


> def underAge(person: Person) = person.age < 18
> people.filter(underAge)


Anonymously:

> people.filter( (person:Person) => person.age < 18 ) 


Even better:

> people.filter(_.age < 18) 


HOFs = IOC

* List


filter(predicate: (A) ⇒ Boolean) : List[A] 
Filter list elements based on a predicate

find(predicate: (A) ⇒ Boolean): Option[A] 
Finds the first element in a list that satisfies a predicate, if any





Remember...


  • Functions are transformations over data

  • Composition is the key when dealing with complexity

  • Scala is OO and FP (not that far from you)



Rationale


"It is better to have 100 functions operate on one data structure than to have 10 functions operate on 10 data structures."
 Alan J. Perlis

Thank you!


References:


functional programming - Scala

By Joel Corrêa

functional programming - Scala

  • 1,143