Transcript

Introduction toFunctional Programming

◦ Motivations◦ Definitions

▫ Values▫ Types▫ Functions

◦ Resources

AGENDA

MotivationsWhy you might want to use functional programming

1

Object-Oriented Programming

Everything is an object◦ State◦ Behavior

You can consider objects as (badly designed) state machines with a wide space of states and transitions

Object-Oriented Programming

◦ Reasoning about the code becomes complex quickly

◦ Unit tests to the rescue▫ 100% coverage▫ Always more tests !

Functional Programming

◦ Write expressive code.◦ Write less code.◦ Write performant code.◦ Write code that we can easily

reason about.

The main goals are to reduce the input and state space and ease reasoning.

ValuesBuilding blocks of Functional Programming

2

“If you say “a” is 5, you can’t say it’s something else later because you just said it was 5. What are you, some kind of liar?

Miran Lipovaca - http://learnyouahaskell.com/

ImmutabilityA value or data structure cannot be modified after its initialization

Values

Immutable◦ You assign a value and can’t

reassign another one.

Data structure are immutable too. To update them, you return a new instance with the updated state.

Values are immutable - Java

Values are immutable - Scala

Data structures are immutable - Java

Data structures are immutable - Scala

◦ Immutability eliminates state transition▫ Passing values and data structure

reference is safe

For lists: Functional languages reuse data and will not duplicate values.

Immutability

TypesBuilding blocks of Functional Programming

2

Types

◦ Classification of values◦ The compiler can verify the correctness

of values with types

Types

FunctionsBuilding blocks of Functional Programming

3

f(x) = yAlways. Always. Always. Always.

◦ Take an argument and produce a result◦ Pure functions do not have side-effects

▫ Change external state▫ Modify a value▫ Throw an exception▫ Read from sdtin▫ Write to disk

Functions

“Functional programming is a restriction on how we write programs, but not on what we express.

Paul Chiusano and Rúnar Bjarnason - Functional Programming in Scala

◦ Change external state▫ Just don’t do it

◦ Modify a value▫ Remember: immutable

◦ Throwing exception▫ Return them as sentinel values▫ Return an [Option] value▫ Return an [Either] value

◦ I/O▫ Most functional languages have the IO

monad1

Alternative to side-effects

1 http://en.wikipedia.org/wiki/Monad_%28functional_programming%29#The_I.2FO_monad

Functions example in Java

Functions example in Scala

Optional type example

Avoid loopsRecursion can do the job.

Tail-recursion example

Not tail-recursion example

What is the last executed statement ?

Tail-recursion example

* This one comes from my editor

High order functionsFunctions that return a function and/or take function(s) as parameters.

High-order functions in Java

High-order functions in Java

High-order functions in Scala

Useful combinators/High-order functions

map: Applies a function to each element of a list.flatMap: Applies a function to a collection and flattens the result by one level.fold: Combines elements of a collection by applying a given function recursively.filter: Returns only the element that passes a truth test.

Resources

◦ Félix-Étienne Trépanier - Confoo slides: http://www.slideshare.net/felixtrepanier/intro-to-functional-programming-confoo

◦ Martin Odersky - Functional Programming Principles in Scala: https://www.coursera.org/course/progfun

◦ Miran Lipocava - Learn you a Haskell for Great Good !: http://learnyouahaskell.com/

◦ JavaScript allongé: https://leanpub.com/javascriptallongesix

◦ Paul Chiusano and Runr Bjarnason - Functional Programming in Scala: http://www.manning.com/bjarnason/

Thanks!

ANY QUESTIONS?

You can find me at

@NyloAndry

ny.fanilo@gmail.com

CREDITS

Special thanks to all the people who made and released these awesome resources for free:◦ Presentation template by SlidesCarnival◦ Photographs by Unsplash

Code screenshots: http://www.slideshare.net/felixtrepanier/intro-to-functional-programming-confoo

top related