there's no charge for (functional) awesomeness
TRANSCRIPT
OO History1967 Simula1971 Smalltalk1983 C++1985 Eiffel1995 Design Patterns1996 JDK 1.02000 Javascript2002 C# 1.0
OO makes code understandable by encapsulating moving
parts. FP makes code understandable by minimizing moving parts.
https://twitter.com/mfeathers/status/29581296216
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); }
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#
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}"); }
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
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
quicksort :: Ord a => [a] -> [a] quicksort [] = [] quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater) where lesser = filter (< p) xs greater = filter (>= p) xs
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()
@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