The Importance of Functional Programming


The Presentation inside:

Slide 0

There’s no charge for FUNCTIONAL awesomeness


Slide 1

#CHILLAXDUDE


Slide 2


Slide 3

There’s no charge for awesomeness…


Slide 4

… nor attractiveness


Slide 5

Object Oriented Programming


Slide 6

OH-OH programming


Slide 7

OO History 1967 Simula 1971 Smalltalk 1983 C++ 1985 Eiffel 1995 Design Patterns 1996 JDK 1.0 2000 Javascript 2002 C# 1.0


Slide 8

Most common language?


Slide 9


Slide 10


Slide 11

OO makes code understandable by encapsulating moving parts. FP makes code understandable by minimizing moving parts. https://twitter.com/mfeathers/status/29581296216


Slide 12

Functional Programming


Slide 13

FP is a way of thinking


Slide 14

λ-calculus 1936


Slide 15

Lisp 1958


Slide 16

Boo! More memory


Slide 17


Slide 18

What language you wish you had created?


Slide 19

Syntactic sugar causes cancer of the semicolon Alan Jay Perlis


Slide 20

#streetcred


Slide 21

Venkat Subramaniam @venkat_s


Slide 22

Bob Martin @unclebobmartin


Slide 23

Neal Ford @neal4d


Slide 24

Rich Hickey @richhickey


Slide 25

Martin Odersky @odersky


Slide 26

Scott Wlaschin @ScottWlaschin


Slide 27

Jose Valim @josevalim


Slide 28


Slide 29

#MOAREXPRESSIVE


Slide 30


Slide 31

Functions are first class citizens


Slide 32

Functions can be declared anywhere


Slide 33

Can you do that in Java or C#?


Slide 34

Higher order functions


Slide 35

Functions that take other functions as parameters


Slide 36

Or return functions as results


Slide 37

Can you do that in Java or C#?


Slide 38

Piping


Slide 39

Chaining operations


Slide 40

Passing the result to the next function


Slide 41

LINQ in C# Streams in Java


Slide 42

public bool FindDragonWarrior(List<string> masters)
 {
 var found = false;
 foreach (var master in masters)
 {
 if (master == "Po")
 {
 found = true;
 break;
 }
 }
 return found;
 }



Slide 43

Remove loop 
 private static bool TheNameIsPo(string master)
 {
 return master == "Po";
 }
 public bool FindDragonWarrior(List<string> masters)
 {
 return masters.Any(TheNameIsPo);
 }


Slide 44

Local declaration public bool FindDragonWarrior(IEnumerable<string> masters)
 {
 var nameIsPo = new Func<string, bool>(m => m == "Po");
 return masters.Any(nameIsPo);
 }


Slide 45

Tell a story


Slide 46

Meaning is king


Slide 47

Multiple of 3 or 5 public void PrintMultiples(List<int> numbers)
 {
 Func<int, bool> isMultiple = n => n % 3 == 0 || n % 5 == 0;
 numbers
 .Where(isMultiple)
 .Select(n => $"({n}) is multiple")
 .ToList()
 .ForEach(Console.WriteLine);
 } F# let printMultiples numbers =
 let isMultiple n = n % 3 = 0 || n % 5 = 0
 numbers
 |> List.filter isMultiple
 |> List.iter (printf "(%d) Is multiple")



Slide 48

Filter Map Print


Slide 49

#MOARCONSISTENCY


Slide 50

Comprehensions


Slide 51

Well known functions


Slide 52

Clear meaning


Slide 53

Work with collections


Slide 54

Lists, arrays, dictionaries, sets, etc


Slide 55

map Converts each element using a fn


Slide 56

reduce Accumulates elements applying a fn


Slide 57

filter Selects only the elements matching


Slide 58

zip Combines two lists into pairs


Slide 59

#HIGHERQUALITY


Slide 60

Null References The Billion Dollar Mistake Tony Hoare


Slide 61

NULL lacks meaning


Slide 62

Print First Master public void PrintMasterName(List<Person> people)
 {
 var master = villagePeople .FirstOrDefault(p => KFMaster.IsMaster(p.Name));
 
 }
 Console.Write($"The master name is {master.Name}");



Slide 63

What’s default?


Slide 64

Short circuit?


Slide 65

Print First Master public void PrintMasterName(List<Person> people)
 {
 var master = villagePeople .FirstOrDefault(p => KFMaster.IsMaster(p.Name));
 
 }
 Console.Write($"The master name is {master?.Name}");
 Short Circuit


Slide 66

Better… still need to check


Slide 67

Something or Nothing


Slide 68

Maybe type


Slide 69

Java 8 Optional


Slide 70

F# Option type


Slide 71

TryFind first multiple let printFirstMaster villagePeople =
 villagePeople
 |> List.tryFind (getName >> isMaster)
 |> Option.iter (getName >> printf "The fst master is %s")
 Only calls the function if there is something


Slide 72

String option != String


Slide 73

How many times have you parsed an int and failed?


Slide 74

What about a customer that does not have a secondary address?


Slide 75

#MOARCONCISE


Slide 76

quicksort :: Ord a => [a] -> [a]
 quicksort [] = []
 quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater)
 where
 lesser = filter (< p) xs
 greater = filter (>= p) xs


Slide 77

Declarative


Slide 78

Type inference


Slide 79

See more code


Slide 80

Easy to extract common functionality


Slide 81

Pattern matching


Slide 82

let fizzBuzz number =
 match number with
 | i when i % 3 = 0 && i % 5 = 0 -> "FizzBuzz"
 | i when i % 3 = 0 -> "Fizz"
 | i when i % 5 = 0 -> "Buzz"
 | _ -> number.ToString()



Slide 83

#MOARCONCURRENCY


Slide 84

Pure functions


Slide 85

Immutable by default


Slide 86

Same input => Same out


Slide 87

Replace function with the implementation


Slide 88

No side effects


Slide 89

State introduces time as a variable


Slide 90

#MINDBLOWN


Slide 91

Parser combinators


Slide 92

Test data generation and quick check


Slide 93

Railway oriented programming


Slide 94

Monads / Builders


Slide 95

#MOARACTION


Slide 96

Great match with Agile


Slide 97

No magic pill


Slide 98

Fear to change


Slide 99

More fun


Slide 100

Start somewhere


Slide 101

FP is a way of thinking


Slide 102

Testing


Slide 103

Scripting


Slide 104

Small Library


Slide 105


Slide 106

THANK YOU!


Slide 107

The Smartest tool for agile project management http://smartview.io


Slide 108

[email protected] @abarylko http://bit.ly/abarylkoslides http://orthocoders.com http://westerndevs.com


Slide 109

Resources • InfoQ - StrangeLoop 2011 Language Panel http://bit.ly/1l2t5d2 • The joy of functional programming http://bit.ly/1LKTjJE • Functional Programming: What? Why? When? http://bit.ly/1GVZJGN • Oscon 2013 - Functional Thinking http://bit.ly/1kl2QOd • F# for fun and profit http://bit.ly/1Pf1RLk


Slide 110

Photo Credit • Under http://creativecommons.org/licenses/by/2.5/ • Jeremy Keith, Roast beef, http://flic.kr/p/TKUz • Rob Campbell, Field of daisies, http://flic.kr/p/6QJjU4


Slide 111


×

HTML:





Ссылка: