'

The Importance of Functional Programming

Понравилась презентация – покажи это...





Слайд 0

There’s no charge for FUNCTIONAL awesomeness


Слайд 1

#CHILLAXDUDE


Слайд 2


Слайд 3

There’s no charge for awesomeness…


Слайд 4

… nor attractiveness


Слайд 5

Object Oriented Programming


Слайд 6

OH-OH programming


Слайд 7

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


Слайд 8

Most common language?


Слайд 9


Слайд 10


Слайд 11

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


Слайд 12

Functional Programming


Слайд 13

FP is a way of thinking


Слайд 14

λ-calculus 1936


Слайд 15

Lisp 1958


Слайд 16

Boo! More memory


Слайд 17


Слайд 18

What language you wish you had created?


Слайд 19

Syntactic sugar causes cancer of the semicolon Alan Jay Perlis


Слайд 20

#streetcred


Слайд 21

Venkat Subramaniam @venkat_s


Слайд 22

Bob Martin @unclebobmartin


Слайд 23

Neal Ford @neal4d


Слайд 24

Rich Hickey @richhickey


Слайд 25

Martin Odersky @odersky


Слайд 26

Scott Wlaschin @ScottWlaschin


Слайд 27

Jose Valim @josevalim


Слайд 28


Слайд 29

#MOAREXPRESSIVE


Слайд 30


Слайд 31

Functions are first class citizens


Слайд 32

Functions can be declared anywhere


Слайд 33

Can you do that in Java or C#?


Слайд 34

Higher order functions


Слайд 35

Functions that take other functions as parameters


Слайд 36

Or return functions as results


Слайд 37

Can you do that in Java or C#?


Слайд 38

Piping


Слайд 39

Chaining operations


Слайд 40

Passing the result to the next function


Слайд 41

LINQ in C# Streams in Java


Слайд 42

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



Слайд 43

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


Слайд 44

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


Слайд 45

Tell a story


Слайд 46

Meaning is king


Слайд 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")



Слайд 48

Filter Map Print


Слайд 49

#MOARCONSISTENCY


Слайд 50

Comprehensions


Слайд 51

Well known functions


Слайд 52

Clear meaning


Слайд 53

Work with collections


Слайд 54

Lists, arrays, dictionaries, sets, etc


Слайд 55

map Converts each element using a fn


Слайд 56

reduce Accumulates elements applying a fn


Слайд 57

filter Selects only the elements matching


Слайд 58

zip Combines two lists into pairs


Слайд 59

#HIGHERQUALITY


Слайд 60

Null References The Billion Dollar Mistake Tony Hoare


Слайд 61

NULL lacks meaning


Слайд 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}");



Слайд 63

What’s default?


Слайд 64

Short circuit?


Слайд 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


Слайд 66

Better… still need to check


Слайд 67

Something or Nothing


Слайд 68

Maybe type


Слайд 69

Java 8 Optional


Слайд 70

F# Option type


Слайд 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


Слайд 72

String option != String


Слайд 73

How many times have you parsed an int and failed?


Слайд 74

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


Слайд 75

#MOARCONCISE


Слайд 76

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


Слайд 77

Declarative


Слайд 78

Type inference


Слайд 79

See more code


Слайд 80

Easy to extract common functionality


Слайд 81

Pattern matching


Слайд 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()



Слайд 83

#MOARCONCURRENCY


Слайд 84

Pure functions


Слайд 85

Immutable by default


Слайд 86

Same input => Same out


Слайд 87

Replace function with the implementation


Слайд 88

No side effects


Слайд 89

State introduces time as a variable


Слайд 90

#MINDBLOWN


Слайд 91

Parser combinators


Слайд 92

Test data generation and quick check


Слайд 93

Railway oriented programming


Слайд 94

Monads / Builders


Слайд 95

#MOARACTION


Слайд 96

Great match with Agile


Слайд 97

No magic pill


Слайд 98

Fear to change


Слайд 99

More fun


Слайд 100

Start somewhere


Слайд 101

FP is a way of thinking


Слайд 102

Testing


Слайд 103

Scripting


Слайд 104

Small Library


Слайд 105


Слайд 106

THANK YOU!


Слайд 107

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


Слайд 108

amir@barylko.com @abarylko http://bit.ly/abarylkoslides http://orthocoders.com http://westerndevs.com


Слайд 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


Слайд 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


Слайд 111


×

HTML:





Ссылка: