111
There’s no charge for FUNCTIONAL awesomeness

There's no charge for (functional) awesomeness

Embed Size (px)

Citation preview

There’s no charge for

FUNCTIONAL awesomeness

#CHILLAXDUDE

There’s no charge for awesomeness…

… nor attractiveness

Object Oriented Programming

OH-OH programming

OO History1967 Simula1971 Smalltalk1983 C++1985 Eiffel1995 Design Patterns1996 JDK 1.02000 Javascript2002 C# 1.0

Most common language?

OO makes code understandable by encapsulating moving

parts. FP makes code understandable by minimizing moving parts.

https://twitter.com/mfeathers/status/29581296216

Functional Programming

FP is a way of thinking

λ-calculus 1936

Lisp 1958

More memory

Boo!

What language you wish you had

created?

Syntactic sugar causes cancer of the

semicolon Alan Jay Perlis

#streetcred

Venkat Subramaniam

@venkat_s

Bob Martin

@unclebobmartin

Neal Ford

@neal4d

Rich Hickey

@richhickey

Martin Odersky

@odersky

Scott Wlaschin

@ScottWlaschin

Jose Valim

@josevalim

#MOAREXPRESSIVE

Functions are first class citizens

Functions can be declared anywhere

Can you do that in Java or C#?

Higher order functions

Functions that take other functions as

parameters

Or return functions as results

Can you do that in Java or C#?

Piping

Chaining operations

Passing the result to the next function

LINQ in C# Streams in Java

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

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

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

Tell a story

Meaning is king

Multiple of 3 or 5public 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); }

let printMultiples numbers = let isMultiple n = n % 3 = 0 || n % 5 = 0 numbers |> List.filter isMultiple |> List.iter (printf "(%d) Is multiple")

F#

Filter Map Print

#MOARCONSISTENCY

Comprehensions

Well known functions

Clear meaning

Work with collections

Lists, arrays, dictionaries, sets,

etc

map Converts each

element using a fn

reduce Accumulates

elements applying a fn

filter Selects only the

elements matching

zip Combines two lists

into pairs

#HIGHERQUALITY

Null References The Billion Dollar

Mistake Tony Hoare

NULL lacks meaning

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

What’s default?

Short circuit?

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

ShortCircuit

Better… still need to check

Something or

Nothing

Maybe type

Java 8 Optional

F# Option type

TryFind first multiplelet printFirstMaster villagePeople = villagePeople |> List.tryFind (getName >> isMaster) |> Option.iter (getName >> printf "The fst master is %s")

Only calls the function if there is something

String option !=

String

How many times have you parsed an

int and failed?

What about a customer that does

not have a secondary address?

#MOARCONCISE

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

Declarative

Type inference

See more code

Easy to extract common

functionality

Pattern matching

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()

#MOARCONCURRENCY

Pure functions

Immutable by default

Same input =>

Same out

Replace function with the

implementation

No side effects

State introduces time as a variable

#MINDBLOWN

Parser combinators

Test data generation and quick check

Railway oriented programming

Monads / Builders

#MOARACTION

Great match with Agile

No magic pill

Fear to change

More fun

Start somewhere

FP is a way of thinking

Testing

Scripting

Small Library

THANK YOU!

The Smartest tool for agile project management

http://smartview.io

[email protected]

@abarylko

http://bit.ly/abarylkoslides

http://orthocoders.com

http://westerndevs.com

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

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