Top Banner
Introduction to Functional Programming
38
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Functional programming

Introduction toFunctional Programming

Page 2: Introduction to Functional programming

◦ Motivations◦ Definitions

▫ Values▫ Types▫ Functions

◦ Resources

AGENDA

Page 3: Introduction to Functional programming

MotivationsWhy you might want to use functional programming

1

Page 4: Introduction to Functional programming

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

Page 5: Introduction to Functional programming

Object-Oriented Programming

◦ Reasoning about the code becomes complex quickly

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

Page 6: Introduction to Functional programming

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.

Page 7: Introduction to Functional programming

ValuesBuilding blocks of Functional Programming

2

Page 8: Introduction to Functional programming

“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/

Page 9: Introduction to Functional programming

ImmutabilityA value or data structure cannot be modified after its initialization

Page 10: Introduction to Functional programming

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.

Page 11: Introduction to Functional programming

Values are immutable - Java

Page 12: Introduction to Functional programming

Values are immutable - Scala

Page 13: Introduction to Functional programming

Data structures are immutable - Java

Page 14: Introduction to Functional programming

Data structures are immutable - Scala

Page 15: Introduction to Functional programming

◦ Immutability eliminates state transition▫ Passing values and data structure

reference is safe

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

Immutability

Page 16: Introduction to Functional programming

TypesBuilding blocks of Functional Programming

2

Page 17: Introduction to Functional programming

Types

◦ Classification of values◦ The compiler can verify the correctness

of values with types

Page 18: Introduction to Functional programming

Types

Page 19: Introduction to Functional programming

FunctionsBuilding blocks of Functional Programming

3

Page 20: Introduction to Functional programming

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

Page 21: Introduction to Functional programming

◦ 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

Page 22: Introduction to Functional programming

“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

Page 23: Introduction to Functional programming

◦ 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

Page 24: Introduction to Functional programming

Functions example in Java

Page 25: Introduction to Functional programming

Functions example in Scala

Page 26: Introduction to Functional programming

Optional type example

Page 27: Introduction to Functional programming

Avoid loopsRecursion can do the job.

Page 28: Introduction to Functional programming

Tail-recursion example

Page 29: Introduction to Functional programming

Not tail-recursion example

What is the last executed statement ?

Page 30: Introduction to Functional programming

Tail-recursion example

* This one comes from my editor

Page 31: Introduction to Functional programming

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

Page 32: Introduction to Functional programming

High-order functions in Java

Page 33: Introduction to Functional programming

High-order functions in Java

Page 34: Introduction to Functional programming

High-order functions in Scala

Page 35: Introduction to Functional programming

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.

Page 36: Introduction to Functional programming

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/

Page 37: Introduction to Functional programming

Thanks!

ANY QUESTIONS?

You can find me at

@NyloAndry

[email protected]

Page 38: Introduction to Functional programming

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