Date post: | 10-Aug-2015 |
Category: |
Software |
Upload: | ny-fanilo-andrianjafy-beng |
View: | 85 times |
Download: | 3 times |
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.
“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/
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.
◦ Immutability eliminates state transition▫ Passing values and data structure
reference is safe
For lists: Functional languages reuse data and will not duplicate values.
Immutability
◦ 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
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/
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