Mixing functional programming approaches in an object oriented language
functional principles for object-oriented development
-
Upload
candace-roberson -
Category
Documents
-
view
54 -
download
3
description
Transcript of functional principles for object-oriented development
![Page 1: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/1.jpg)
functional principles for
object-oriented development
@jessitron
![Page 2: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/2.jpg)
![Page 3: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/3.jpg)
![Page 4: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/4.jpg)
Imperative
Procedural
Object-Oriented Functional
Aspect-Oriented Logic
![Page 5: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/5.jpg)
![Page 6: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/6.jpg)
![Page 7: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/7.jpg)
Data In, Data Out
Immutability
Verbs Are People Too
Declarative Style
Specific Typing
Lazy Evaluation
![Page 8: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/8.jpg)
What is it?What is it?
PrinciplePrinciple
What is it?What is it?
We already do itWe already do it
What’s the point?What’s the point?
What is it?What is it?
Scala / F#Scala / F#
Java / C#Java / C#
![Page 9: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/9.jpg)
Data In, Data Out
![Page 10: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/10.jpg)
access global state
modify input
change the world
![Page 11: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/11.jpg)
Data In -> ? -> Data out
inputoutput
![Page 12: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/12.jpg)
Testable
Easier to understand
![Page 13: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/13.jpg)
the flow of datathe flow of data
![Page 14: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/14.jpg)
![Page 15: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/15.jpg)
List<Deposit> List<Donation>
List<Match<Deposit,Donation>>
List<Donation>
![Page 16: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/16.jpg)
Problem easier, because we know each step of the way of data.
![Page 17: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/17.jpg)
public string CreateAccount(UserDbService db, String email, String password) { …}
![Page 18: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/18.jpg)
String password;String email;
private boolean invalidPassword() { return !password.contains(email);}
![Page 19: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/19.jpg)
private boolean invalidPassword(String password, String email) { return !password.contains(email);}
![Page 20: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/20.jpg)
Immutability
![Page 21: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/21.jpg)
modify anything
![Page 22: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/22.jpg)
Concurrency
fewer possibilities
![Page 23: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/23.jpg)
NPENPE
![Page 24: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/24.jpg)
c c
![Page 25: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/25.jpg)
String
Effective JavaEffective C#
![Page 26: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/26.jpg)
ScalaScala
val qty = 4 // immutable
var n = 2 // mutable
![Page 27: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/27.jpg)
let qty = 4 // immutable
let mutable n = 2 // mutable
n = 4 // false
n <- 4 // destructive update
F#F#
![Page 28: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/28.jpg)
C#: easyC#: easy
public struct Address { private readonly string city;
public Address(string city) : this() { this.city = city; }
public string City { get { return city; } }
![Page 29: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/29.jpg)
Java: defensive copyJava: defensive copy
private final ImmutableList<Phone> phones;
public Customer (Iterable<Phone> phones) { this.phones = ImmutableList.copyOf(phones);}
![Page 30: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/30.jpg)
Java: copy on modJava: copy on mod
public Customer addPhone(Phone newPhone) { Iterable<Phone> morePhones = ImmutableList.builder() .addAll(phones) .add(newPhone).build(); return new Customer(morePhones);}
![Page 31: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/31.jpg)
fruit
fruit.add(tomato)
![Page 32: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/32.jpg)
F#: copy on modF#: copy on mod
member this.AddPhone (newPhone : Phone) { new Customer(newPhone :: phones)}
![Page 33: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/33.jpg)
C#: shallow copyC#: shallow copy
public Customer AddPhone(Phone newPhone) { IEnumerable<Phone> morePhones = // create list return new Customer(morePhones, name, address, birthday, cousins);}
![Page 34: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/34.jpg)
this talk is brought to you by… the Option type!
NPENPE Thing
doStuff()
NullThing
doStuff() {}
SomeThing
doStuff() {…}
Option<T>Option<T>
NoneNoneSome<T>Some<T>
![Page 35: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/35.jpg)
… because null is not a valid object reference.
Optional<T>
FSharpOption<T>
![Page 36: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/36.jpg)
Verbs Are People Too
![Page 37: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/37.jpg)
![Page 38: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/38.jpg)
Imperative
Procedural
Object-Oriented
Functional
Aspect-Oriented Logic
!=
![Page 39: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/39.jpg)
Strategy
CommandOnClick()
release ( )
![Page 40: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/40.jpg)
class CostInflation implements FunctionOverTime { public float valueAt(int t) { return // some calculated value; }}
JavaJava
![Page 41: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/41.jpg)
val costInflation = Variable ( “Cost Inflation”, t => Math.pow(1.15,t))
val seasonalVolume = Array(0.95,0.99,1.01,…)
val seasonalAdjustment = Variable(“Season”, t => seasonalVolume(t))
ScalaScala
![Page 42: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/42.jpg)
C#C#
private readonly Func<int, double> calc;
public Func<int, double> ValueAt{ get { return calc; }}
![Page 43: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/43.jpg)
C#C#
var inflation = new Variable(“Inflation”, t => Math.Pow(1.15,t));
inflation.ValueAt(3);
![Page 44: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/44.jpg)
JavaJava
Variable inflation = new Variable (“Inflation”, new Function<Integer, Double>() { @Override public Double apply(Integer input) { return Math.pow(1.15, input); } });
![Page 45: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/45.jpg)
Java 8Java 8
Variable inflation = new Variable (“Inflation”, t => Math.pow(1.15, t) );
![Page 46: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/46.jpg)
Declarative Style
![Page 47: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/47.jpg)
say what you’re doing, not how you’re doing it
![Page 48: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/48.jpg)
Select ROLE_NAME, UPDATE_DATEFrom USER_ROLESWhere USER_ID = :userId
![Page 49: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/49.jpg)
smaller pieces
readable code
![Page 50: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/50.jpg)
familiar != readable
familiar != readable
![Page 51: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/51.jpg)
JavaJava
public List<String> findBugReports(List<String> lines){ List<String> output = new LinkedList(); for(String s : lines) { if(s.startsWith(“BUG”)) { output.add(s); } } return output;}
![Page 52: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/52.jpg)
ScalaScala
lines.filter( _.startsWith(“BUG”))
lines.par.filter( _.startsWith(“BUG”))
![Page 53: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/53.jpg)
JavaJava
final Predicate<String> startsWithBug = new Predicate<String>() { public boolean apply(String s) { return s.startsWith("BUG"); } };
filter(list, startsWithBug);
![Page 54: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/54.jpg)
C#C#
lines.Where(s => s.StartsWith(“BUG”)).ToList
![Page 55: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/55.jpg)
this talk is brought to you by… the Tuple type!
function
new Tuple<string,int>(“You win!”, 1000000)
![Page 56: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/56.jpg)
When one return value is not enough!
Tuple<T1,T2,…>
![Page 57: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/57.jpg)
Specific Typing
![Page 58: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/58.jpg)
expressiveness
catch errors early
![Page 59: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/59.jpg)
![Page 60: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/60.jpg)
![Page 61: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/61.jpg)
The beginning of wisdom is to call things by their right names.
![Page 62: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/62.jpg)
F#F#
[<Measure>] type cents
let price = 599<cents>
![Page 63: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/63.jpg)
ScalaScala
type FirstName = String // type alias
Customer(name : FirstName, home : EmailAddress)
![Page 64: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/64.jpg)
ScalaScala
case class FirstName(value : String)
let friend = FirstName(“Ivana”);
![Page 65: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/65.jpg)
JavaJava
public User(FirstName name, EmailAddress login)
public class FirstName { public final String stringValue;
public FirstName(final String value) { this.stringValue = value; }
public String toString() {...} public boolean equals() {...} public int hashCode() {...}}
![Page 66: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/66.jpg)
State everything you
need
State only what you
need
![Page 67: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/67.jpg)
ScalaScala
List [+A]
def indexOf [B >: A] (elem: B): Int
def sameElements (that: GenIterable[A]): Boolean
![Page 68: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/68.jpg)
C#: weakC#: weak
public boolean Valid(Customer cust) { EmailAddress email = cust.EmailAddress; // exercise business logic return true; }
![Page 69: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/69.jpg)
C#: weakC#: weak
public boolean Valid(IHasEmailAddress anything) { EmailAddress email = anything.EmailAddress; // exercise business logic return true; }
interface IHasEmailAddress { string EmailAddress {get; } }
![Page 70: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/70.jpg)
Lazy Evaluation
![Page 71: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/71.jpg)
delay evaluation until the last responsible moment
![Page 72: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/72.jpg)
save work
separate “what to do” from “when to stop”
![Page 73: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/73.jpg)
int bugCount = 0;String nextLine = file.readLine();while (bugCount < 40) { if (nextLine.startsWith("BUG")) { String[] words = nextLine.split(" "); report("Saw "+words[0]+" on "+words[1]); bugCount++; } waitUntilFileHasMoreData(file); nextLine = file.readLine();}
JavaJava
![Page 74: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/74.jpg)
for (String s : FluentIterable.of(new RandomFileIterable(br)) .filter(STARTS_WITH_BUG_PREDICATE)
.transform(TRANSFORM_BUG_FUNCTION)
.limit(40)
.asImmutableList()) { report(s); }
JavaJava
Iterable: laziness is a virtueIterable: laziness is a virtue
![Page 75: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/75.jpg)
C#C#
IEnumerable<string> ReadLines(StreamReader r) { while (true) { WaitUntilFileHasMoreData(r); yield return r.ReadLine(); }}
![Page 76: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/76.jpg)
Data In, Data Out
Immutability
Verbs Are People Too
Declarative Style
Specific Typing
Lazy Evaluation
![Page 77: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/77.jpg)
I promise not to exclude from consideration any idea
based on its source, but to consider ideas across schools and heritages
in order to find the ones that best suit the current situation.
![Page 78: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/78.jpg)
![Page 79: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/79.jpg)
![Page 80: functional principles for object-oriented development](https://reader036.fdocuments.us/reader036/viewer/2022081506/5681324e550346895d98c6eb/html5/thumbnails/80.jpg)
Man, the living creature, the creating individual, is always more important than any established style or
system. – Bruce Lee
@jessitron@jessitron
Jessica Kerrblog.jessitron.com
github.com/jessitron/fp4ood