SlideShare a Scribd company logo
1 of 76
Download to read offline
Functional Operations
#dmconf1521 November 2015
Susan Potter
Lookout
twitter: @SusanPotter
github: mbbx6spp
% whoami
Figure: From developer to (dev)ops engineer
Agenda
1 Motivation for reasoning
2 Review functional programming 101
3 Illustrate forms of reasoning
4 Case study: Reimagine package management
Edsger Dijkstra
Reliability
“Those who want really reliable software will discover
that they must find means of avoiding the majority of
bugs to start with, and as a result the programming
process will become cheaper. If you want more effective
programmers, you will discover that they should not
waste their time debugging, they should not introduce
the bugs to start with.”[?]
Why care now?
1 Economic factors
necessity of distributed systems
Why care now?
1 Economic factors
necessity of distributed systems
2 Human factors
high churn/turnover, low quality of ops life
Why care now?
1 Economic factors
necessity of distributed systems
2 Human factors
high churn/turnover, low quality of ops life
3 Technological factors
programmable infrastructure & FP no longer just for academics
The Problem. . .
Application Delivery
1 Provision infrastructure
2 Configure nodes
3 Orchestrate services
Need to support more
1 Application services
2 Environments
3 Data services
4 Distributed services
Optimize for
1 Scalability solved by on-demand ”cloud”
Optimize for
1 Scalability solved by on-demand ”cloud”
2 Reliability solved by . . .
So what yields reliability?
Reason
The required techniques of effective reasoning are pretty
formal, but as long as programming is done by people
that don’t master them, the software crisis will remain
with us and will be considered an incurable disease. [?]
Functions 101
Functions have inputs (Ruby)
1 # Two input arguments here
2 def add(x, y)
3 x + y
4 end
5
6 # One input argument here
7 def len(s)
8 s.size
9 end
Functions have inputs (Scala)
1 object Functions {
2 // Two typed input arguments here
3 def add(x: Int , y: Int) = x + y
4
5 // One typed input argument here
6 def len(s: String) = s.size
7 }
Functions return a result
1 scala > add(5, 6)
2 res0: Int = 11
3
4 scala > len("Hello ,␣Barcelona")
5 res1: Int = 16
Only depend on inputs 1/2
1 scala > val defaultTimeout : Int = 30
2 scala > val timeout1: Option[Int] = Some (15)
3 scala > val timeout2: Option[Int] = None
4 scala > :paste
5 def defaulter[A](a: => A, ma: Option[A]) =
6 ma match {
7 case Some(x) => x
8 case None => a
9 }
Only depend on inputs 2/2
1 scala > timeout1
2 timeout1: Option[Int] = Some (15)
3
4 scala > timeout2
5 timeout2: Option[Int] = None
6
7 scala > defaulter(defaultTimeout , timeout1)
8 res0: Int = 15
9
10 scala > defaulter(defaultTimeout , timeout2)
11 res1: Int = 30
Return same result given same inputs
1 scala > len("Hello ,␣Barcelona")
2 res0: Int = 16
3
4 scala > len("Hello ,␣Barcelona")
5 res1: Int = 16
6 ...
7 scala > len("Hello ,␣Barcelona")
8 res3333333333: Int = 16
9
10 scala > // Always!
The Big idea
Referential Transparency
Given same inputs, return same result. Always.
Functions can use other values
1 // type aliasing a function
2 type Pred[A] = A => Boolean
3
4 // Passing a function as an input argument
5 def is[A](p: Pred[A])(a: A) = p(a)
6
7 // This uses already defined function +is+
8 def not[A](p: Pred[A])(a: A) = !is(p)(a)
Values can be functions
1 // Returning a function as a value
2 def lessThanN(n: Int): Pred[Int] = _ < n
3
4 // Using two in scope functions
5 def islessThanN(n: Int)(x: Int) =
6 is(ltN(n))(x)
7
8 // Those values can be functions :)
Another important idea
Higher Order Functions
Build useful functions from simpler functions!
Questions so far?
Figure: Awake?
Function Composition
UNIX Pipes 1/4
1 sh > echo -n "Hello ~~~" 
2 | sed ’s/~//g’ 
3 | tr ’[: lower :]’ ’[:upper :]’ 
4 | wc -c
5 5
UNIX Pipes 2/4
1 sh > alias sanitize=’sed "s/~//g"’
2 sh > alias toUpper=’tr "[: lower :]" "[: upper :]"’
3 sh > alias len=’wc -c’
4
5 sh > alias myfun0=’sanitize | toUpper ’
6 sh > alias myfun1=’myfun0 | wc -c’
UNIX Pipes 3/4
1 sh > echo -n "Hello ~~~" 
2 | sed ’s/~//g’ 
3 | tr ’[: lower :]’ ’[:upper :]’ 
4 | wc -c
5 5
6
7 sh > echo -n "Hello ~~~" | myfun1
8 5
UNIX Pipes 4/4
• Character-based
• File descriptors
Function Composition 1/3
1 def toUpper = (s: String) => s.toUpperCase
2 def len = (s: String) => s.size
3 def sanitize = "~".r replaceAllIn (_, "")
Function Composition 2/3
1 scala > def myfun0 = sanitize andThen toUpper
2 scala > def myfun1 = myfun0 andThen len
3
4 scala > myfun0 "Hello ~~~"
5 res0 = HELLO
6
7 scala > myfun1 "Hello ~~~"
8 res1: Int = 5
Function Composition 3/3
• Value-based
• Functions
Questions so far?
Figure: Awake?
Functions as building blocks
Figure: Build solid walls/foundations from simple, regular building blocks
Reasoning
Equational Reasoning
• Simpler testing; reduce test suite complexity
• Substitute repeated expression with name
• Refactoring is trivial; not error prone
Testing non-RT code
Figure: Side effecting, non-RT code requires complex testing setup.
Equational Reasoning
1 scala > f(v1 , v2 , otherfun) == expected
2 res0: Boolean = true
3
• no setup/teardown complexity to test assertions
• no mutation of SUT just for testing
Equational Reasoning
1 scala > val x = "YOLO"
2 x: java.lang.String = YOLO
3
4 scala > val r1 = x.reverse
5 r1: String = OLOY
6
7 scala > "YOLO".reverse
8 res0: String = OLOY
9
Equational Reasoning
1 scala > var age = 27
2 scala > def incr = { age += 1; age }
3
4 scala > incr
5 res0: Int = 28
6
7 scala > incr
8 res1: Int = 29
9
Axiomatic Reasoning
• Axioms are basic assumptions
• Allows us to specify properties
• Infer potential problems due to properties
• What can we guarantee? What can we not?
Axiomatic Reasoning
1 class Vpc < AwsResource
2 def initialize(conf)
3 @cidr = conf[’vpc.cidr ’]
4 end
5 end
6 # Is the Vpc instance created with an
7 # empty conf Hash guaranteeing the
8 # premise of the initialize contract?
9
Axiomatic Reasoning
1 // validate inputs before instantiating
2 case class Vpc(cidr: Cidr)
3
4 def createVpc(ipStr: String , pfx: String) =
5 for {
6 ip <- IpAddress.toIpAddress(ipStr)
7 prefix <- Cidr.toPrefix(pfx)
8 } yield Vpc(Cidr(ip , prefix ))
9
Axiomatic Reasoning
1 // if fun is 1-to -1 or bijective
2 // we know inverse must exist
3 def toLower(u: UpperCaseChar ): LowerCaseChar = ??
4 def toUpper(l: LowerCaseChar ): UpperCaseChar = ??
5
6 // idempotency typically important in ops
7 f(f(x)) == f(x)
8
Axiomatic Reasoning
1 // commutativity important in dist sys
2 // f, binary operator over type A
3 // x and y are of type A then
4 // x @ y = y @ x
5 f(x, y) == f(y, x)
6
7 // associativity important in concurrency
8 // f, binary operator over type A
9 // x, y, and z are of type A then
10 // x @ (y @ z) = (x @ y) @ z
11 f(x, f(y, z)) == f(f(x, y), z)
12
Axiomatic Reasoning - Distributed
Systems
• Simple causal consistency (partial ordering)
• CRDTs (semilattice algebra)
• Invariant confluence (coordination free
execution)
Axiomatic Reasoning - Concurrency
• MVars, TVars, LVars have algebraic properties
• Design for liveness via axiomatic reasoning
Axiomatic Reasoning - More Exhaustive
Testing
• Property-based testing
1 // untested - uses scalacheck
2 def inverse[A]( implicit a: Action[A]) =
3 Prop.forAll { (x: A) =>
4 x === a.inverse(a.inverse(x))
5 }
6
7 implicit def actionArbitrary [A]
8 (implicit a: Arbitrary[A]) = Arbitrary { /*
9
Generic Reasoning
• Reasoning on generic type functions
• Less specific the types the more we know about
the function
• Find design-time bugs
Generic Reasoning
1 def f0[A](a: A): A = ???
2
Generic Reasoning
1 // Only definition
2 def f0[A](a: A): A = a
3
Generic Reasoning
1 def f1[A](a: A)(ma: Option[A]): A = ???
2
Generic Reasoning
1 def f1[A](a: A)(ma: Option[A]): A = a
2
1 def f1[A](a: A)(ma: Option[A]): A = ma match {
2 case Some(x) => x
3 case None => a
4 }
5
Generic Reasoning
1 def f2[A](as: List[A]): A = ???
2
Generic Reasoning
1 // what about an empty list? Yikes!
2 def f2[A](as: List[A]): A = ???
3
Generic Reasoning
1 // Assuming we want the head element!
2 // A more sensible type signature design
3 def f3[A](as: List[A]): Option[A] = as match {
4 case Nil => None
5 case a :: rest => a
6 }
7
Generic Reasoning
1 def f4[A](ma: Option[A]): A = ???
2
Review
Reasoning Process
• Assume as little as possible
• Define all inputs
• Build only on top of RT
• Derive properties from domain
• Infer properties from generic types
Methods / Techniques
• Encode normal error cases in results
• Strive for termination
• Strive for RT where ever possible
Iteration / Improvement
• So much to learn
• Start out informal; add formal reasoning as
needed/learned
• The more we do the easier it gets
Example in the wild
Mainstream Package Management
Based on shared + mutable state (filesystem)
Violates RT
Alternatives
• shared + immutable
• private + mutable
• expensive coarse grained locks
• hybrid without the expense
Define all inputs
• Force clean build env (chroot)
• Requires explicit inputs
• Full dependency definition
Ensure RT
• Use private mutable space
• Different inputs, different result
• Symlink unique results (atomic op)
Different inputs give unique results
• Shared + immutable provides RT
• New inputs result in different path
• Install many versions/configurations
• Means we can do binary substitution
RT foundation
• Update existing node more reliably
• Supports immutable infrastructure
• Supports congruent configuration
Recap
1 Referential Transparency allows us to reason
3 HOFs & Composition is the motar
4 Logical reasoning provides solid foundation
Recap
1 Referential Transparency allows us to reason
2 Functions are simple, regular building blocks
3 HOFs & Composition is the motar
4 Logical reasoning provides solid foundation
Don’t fear the maths!
Figure: Domain specific language of the sciences and engineering
Questions
Figure: Heckle me @SusanPotter later too.

More Related Content

What's hot

OCJP Samples Questions: Exceptions and assertions
OCJP Samples Questions: Exceptions and assertionsOCJP Samples Questions: Exceptions and assertions
OCJP Samples Questions: Exceptions and assertionsHari kiran G
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeGanesh Samarthyam
 
The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202Mahmoud Samir Fayed
 
New Functional Features of Java 8
New Functional Features of Java 8New Functional Features of Java 8
New Functional Features of Java 8franciscoortin
 
Java Generics for Dummies
Java Generics for DummiesJava Generics for Dummies
Java Generics for Dummiesknutmork
 
Exceptions and errors in Java
Exceptions and errors in JavaExceptions and errors in Java
Exceptions and errors in JavaManuela Grindei
 
Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.Nishan Barot
 
JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingPathomchon Sriwilairit
 
Java 8 lambda expressions
Java 8 lambda expressionsJava 8 lambda expressions
Java 8 lambda expressionsLogan Chien
 
Clone Refactoring with Lambda Expressions
Clone Refactoring with Lambda ExpressionsClone Refactoring with Lambda Expressions
Clone Refactoring with Lambda ExpressionsNikolaos Tsantalis
 
Writing beautiful code with Java 8
Writing beautiful code with Java 8Writing beautiful code with Java 8
Writing beautiful code with Java 8Sergiu Mircea Indrie
 

What's hot (20)

OCJP Samples Questions: Exceptions and assertions
OCJP Samples Questions: Exceptions and assertionsOCJP Samples Questions: Exceptions and assertions
OCJP Samples Questions: Exceptions and assertions
 
Java Generics
Java GenericsJava Generics
Java Generics
 
07. Arrays
07. Arrays07. Arrays
07. Arrays
 
Introduction to Java Programming Part 2
Introduction to Java Programming Part 2Introduction to Java Programming Part 2
Introduction to Java Programming Part 2
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean Code
 
Type Casting Operator
Type Casting OperatorType Casting Operator
Type Casting Operator
 
Variables and Data Types
Variables and Data TypesVariables and Data Types
Variables and Data Types
 
The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202
 
New Functional Features of Java 8
New Functional Features of Java 8New Functional Features of Java 8
New Functional Features of Java 8
 
Java Generics for Dummies
Java Generics for DummiesJava Generics for Dummies
Java Generics for Dummies
 
Exceptions and errors in Java
Exceptions and errors in JavaExceptions and errors in Java
Exceptions and errors in Java
 
Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.
 
JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java Programming
 
Java operators
Java operatorsJava operators
Java operators
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
 
Java 8 lambda expressions
Java 8 lambda expressionsJava 8 lambda expressions
Java 8 lambda expressions
 
Lazy java
Lazy javaLazy java
Lazy java
 
Clone Refactoring with Lambda Expressions
Clone Refactoring with Lambda ExpressionsClone Refactoring with Lambda Expressions
Clone Refactoring with Lambda Expressions
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Writing beautiful code with Java 8
Writing beautiful code with Java 8Writing beautiful code with Java 8
Writing beautiful code with Java 8
 

Viewers also liked

Jepsen V - Kyle Kingsbury - Key Note distributed matters Berlin 2015
Jepsen V - Kyle Kingsbury - Key Note distributed matters Berlin 2015Jepsen V - Kyle Kingsbury - Key Note distributed matters Berlin 2015
Jepsen V - Kyle Kingsbury - Key Note distributed matters Berlin 2015distributed matters
 
NoSQL in Financial Industry - Pierre Bittner
NoSQL in Financial Industry - Pierre BittnerNoSQL in Financial Industry - Pierre Bittner
NoSQL in Financial Industry - Pierre Bittnerdistributed matters
 
Conflict resolution with guns - Mark Nadal
Conflict resolution with guns - Mark NadalConflict resolution with guns - Mark Nadal
Conflict resolution with guns - Mark Nadaldistributed matters
 
NoSQL's biggest lie: SQL never went away - Martin Esmann
NoSQL's biggest lie: SQL never went away - Martin EsmannNoSQL's biggest lie: SQL never went away - Martin Esmann
NoSQL's biggest lie: SQL never went away - Martin Esmanndistributed matters
 
NoSQL meets Microservices - Michael Hackstein
NoSQL meets Microservices - Michael HacksteinNoSQL meets Microservices - Michael Hackstein
NoSQL meets Microservices - Michael Hacksteindistributed matters
 
Conflict Resolution
Conflict ResolutionConflict Resolution
Conflict ResolutionBill Taylor
 
Joins in a distributed world - Lucian Precup
Joins in a distributed world - Lucian Precup Joins in a distributed world - Lucian Precup
Joins in a distributed world - Lucian Precup distributed matters
 
NoSQL meets Microservices - Michael Hackstein
NoSQL meets Microservices -  Michael HacksteinNoSQL meets Microservices -  Michael Hackstein
NoSQL meets Microservices - Michael Hacksteindistributed matters
 
What and Why and How: Apache Drill ! - Tugdual Grall
What and Why and How: Apache Drill ! - Tugdual GrallWhat and Why and How: Apache Drill ! - Tugdual Grall
What and Why and How: Apache Drill ! - Tugdual Gralldistributed matters
 
Cloud Apps - Running Fully Distributed on Mobile Devices - Dominik Rüttimann
Cloud Apps - Running Fully Distributed on Mobile Devices - Dominik RüttimannCloud Apps - Running Fully Distributed on Mobile Devices - Dominik Rüttimann
Cloud Apps - Running Fully Distributed on Mobile Devices - Dominik Rüttimanndistributed matters
 
Replication and Synchronization Algorithms for Distributed Databases - Lena W...
Replication and Synchronization Algorithms for Distributed Databases - Lena W...Replication and Synchronization Algorithms for Distributed Databases - Lena W...
Replication and Synchronization Algorithms for Distributed Databases - Lena W...distributed matters
 
No Free Lunch, Indeed: Three Years of Microservices at SoundCloud - Phil Calcado
No Free Lunch, Indeed: Three Years of Microservices at SoundCloud - Phil CalcadoNo Free Lunch, Indeed: Three Years of Microservices at SoundCloud - Phil Calcado
No Free Lunch, Indeed: Three Years of Microservices at SoundCloud - Phil Calcadodistributed matters
 
A tale of queues — from ActiveMQ over Hazelcast to Disque - Philipp Krenn
A tale of queues — from ActiveMQ over Hazelcast to Disque - Philipp KrennA tale of queues — from ActiveMQ over Hazelcast to Disque - Philipp Krenn
A tale of queues — from ActiveMQ over Hazelcast to Disque - Philipp Krenndistributed matters
 

Viewers also liked (14)

Jepsen V - Kyle Kingsbury - Key Note distributed matters Berlin 2015
Jepsen V - Kyle Kingsbury - Key Note distributed matters Berlin 2015Jepsen V - Kyle Kingsbury - Key Note distributed matters Berlin 2015
Jepsen V - Kyle Kingsbury - Key Note distributed matters Berlin 2015
 
NoSQL in Financial Industry - Pierre Bittner
NoSQL in Financial Industry - Pierre BittnerNoSQL in Financial Industry - Pierre Bittner
NoSQL in Financial Industry - Pierre Bittner
 
Actors evolved- Rotem Hermon
Actors evolved- Rotem HermonActors evolved- Rotem Hermon
Actors evolved- Rotem Hermon
 
Conflict resolution with guns - Mark Nadal
Conflict resolution with guns - Mark NadalConflict resolution with guns - Mark Nadal
Conflict resolution with guns - Mark Nadal
 
NoSQL's biggest lie: SQL never went away - Martin Esmann
NoSQL's biggest lie: SQL never went away - Martin EsmannNoSQL's biggest lie: SQL never went away - Martin Esmann
NoSQL's biggest lie: SQL never went away - Martin Esmann
 
NoSQL meets Microservices - Michael Hackstein
NoSQL meets Microservices - Michael HacksteinNoSQL meets Microservices - Michael Hackstein
NoSQL meets Microservices - Michael Hackstein
 
Conflict Resolution
Conflict ResolutionConflict Resolution
Conflict Resolution
 
Joins in a distributed world - Lucian Precup
Joins in a distributed world - Lucian Precup Joins in a distributed world - Lucian Precup
Joins in a distributed world - Lucian Precup
 
NoSQL meets Microservices - Michael Hackstein
NoSQL meets Microservices -  Michael HacksteinNoSQL meets Microservices -  Michael Hackstein
NoSQL meets Microservices - Michael Hackstein
 
What and Why and How: Apache Drill ! - Tugdual Grall
What and Why and How: Apache Drill ! - Tugdual GrallWhat and Why and How: Apache Drill ! - Tugdual Grall
What and Why and How: Apache Drill ! - Tugdual Grall
 
Cloud Apps - Running Fully Distributed on Mobile Devices - Dominik Rüttimann
Cloud Apps - Running Fully Distributed on Mobile Devices - Dominik RüttimannCloud Apps - Running Fully Distributed on Mobile Devices - Dominik Rüttimann
Cloud Apps - Running Fully Distributed on Mobile Devices - Dominik Rüttimann
 
Replication and Synchronization Algorithms for Distributed Databases - Lena W...
Replication and Synchronization Algorithms for Distributed Databases - Lena W...Replication and Synchronization Algorithms for Distributed Databases - Lena W...
Replication and Synchronization Algorithms for Distributed Databases - Lena W...
 
No Free Lunch, Indeed: Three Years of Microservices at SoundCloud - Phil Calcado
No Free Lunch, Indeed: Three Years of Microservices at SoundCloud - Phil CalcadoNo Free Lunch, Indeed: Three Years of Microservices at SoundCloud - Phil Calcado
No Free Lunch, Indeed: Three Years of Microservices at SoundCloud - Phil Calcado
 
A tale of queues — from ActiveMQ over Hazelcast to Disque - Philipp Krenn
A tale of queues — from ActiveMQ over Hazelcast to Disque - Philipp KrennA tale of queues — from ActiveMQ over Hazelcast to Disque - Philipp Krenn
A tale of queues — from ActiveMQ over Hazelcast to Disque - Philipp Krenn
 

Similar to Functional Operations - Susan Potter

ch02-primitive-data-definite-loops.ppt
ch02-primitive-data-definite-loops.pptch02-primitive-data-definite-loops.ppt
ch02-primitive-data-definite-loops.pptMahyuddin8
 
ch02-primitive-data-definite-loops.ppt
ch02-primitive-data-definite-loops.pptch02-primitive-data-definite-loops.ppt
ch02-primitive-data-definite-loops.pptghoitsun
 
Ch02 primitive-data-definite-loops
Ch02 primitive-data-definite-loopsCh02 primitive-data-definite-loops
Ch02 primitive-data-definite-loopsJames Brotsos
 
Functional Programming in Swift
Functional Programming in SwiftFunctional Programming in Swift
Functional Programming in SwiftSaugat Gautam
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++NUST Stuff
 
Ifi7184 lesson3
Ifi7184 lesson3Ifi7184 lesson3
Ifi7184 lesson3Sónia
 
Lec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questionsLec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questionsYashJain47002
 
Pi j1.3 operators
Pi j1.3 operatorsPi j1.3 operators
Pi j1.3 operatorsmcollison
 
Intro to React
Intro to ReactIntro to React
Intro to ReactTroy Miles
 
Mat lab workshop
Mat lab workshopMat lab workshop
Mat lab workshopVinay Kumar
 
E2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docx
E2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docxE2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docx
E2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docxjacksnathalie
 

Similar to Functional Operations - Susan Potter (20)

Java Tutorial
Java Tutorial Java Tutorial
Java Tutorial
 
Java introduction
Java introductionJava introduction
Java introduction
 
ch02-primitive-data-definite-loops.ppt
ch02-primitive-data-definite-loops.pptch02-primitive-data-definite-loops.ppt
ch02-primitive-data-definite-loops.ppt
 
ch02-primitive-data-definite-loops.ppt
ch02-primitive-data-definite-loops.pptch02-primitive-data-definite-loops.ppt
ch02-primitive-data-definite-loops.ppt
 
Ch02 primitive-data-definite-loops
Ch02 primitive-data-definite-loopsCh02 primitive-data-definite-loops
Ch02 primitive-data-definite-loops
 
Functional Programming in Swift
Functional Programming in SwiftFunctional Programming in Swift
Functional Programming in Swift
 
Lec1
Lec1Lec1
Lec1
 
Test
TestTest
Test
 
Test
TestTest
Test
 
Test
TestTest
Test
 
Test
TestTest
Test
 
Test
TestTest
Test
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
 
Ifi7184 lesson3
Ifi7184 lesson3Ifi7184 lesson3
Ifi7184 lesson3
 
Lec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questionsLec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questions
 
Pi j1.3 operators
Pi j1.3 operatorsPi j1.3 operators
Pi j1.3 operators
 
Intro to React
Intro to ReactIntro to React
Intro to React
 
Mat lab workshop
Mat lab workshopMat lab workshop
Mat lab workshop
 
Mit6 094 iap10_lec03
Mit6 094 iap10_lec03Mit6 094 iap10_lec03
Mit6 094 iap10_lec03
 
E2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docx
E2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docxE2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docx
E2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docx
 

Recently uploaded

BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxolyaivanovalion
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxolyaivanovalion
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxolyaivanovalion
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysismanisha194592
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...amitlee9823
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% SecurePooja Nehwal
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Delhi Call girls
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxolyaivanovalion
 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxolyaivanovalion
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptxAnupama Kate
 
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort ServiceBDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort ServiceDelhi Call girls
 
Zuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptxZuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptxolyaivanovalion
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxolyaivanovalion
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxolyaivanovalion
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxolyaivanovalion
 
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptxBPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptxMohammedJunaid861692
 

Recently uploaded (20)

Sampling (random) method and Non random.ppt
Sampling (random) method and Non random.pptSampling (random) method and Non random.ppt
Sampling (random) method and Non random.ppt
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptx
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptx
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFx
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysis
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptx
 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptx
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx
 
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort ServiceBDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
 
Zuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptxZuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptx
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts ServiceCall Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptx
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptx
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFx
 
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptxBPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
 

Functional Operations - Susan Potter

  • 1. Functional Operations #dmconf1521 November 2015 Susan Potter Lookout twitter: @SusanPotter github: mbbx6spp
  • 2. % whoami Figure: From developer to (dev)ops engineer
  • 3. Agenda 1 Motivation for reasoning 2 Review functional programming 101 3 Illustrate forms of reasoning 4 Case study: Reimagine package management
  • 5. Reliability “Those who want really reliable software will discover that they must find means of avoiding the majority of bugs to start with, and as a result the programming process will become cheaper. If you want more effective programmers, you will discover that they should not waste their time debugging, they should not introduce the bugs to start with.”[?]
  • 6. Why care now? 1 Economic factors necessity of distributed systems
  • 7. Why care now? 1 Economic factors necessity of distributed systems 2 Human factors high churn/turnover, low quality of ops life
  • 8. Why care now? 1 Economic factors necessity of distributed systems 2 Human factors high churn/turnover, low quality of ops life 3 Technological factors programmable infrastructure & FP no longer just for academics
  • 10. Application Delivery 1 Provision infrastructure 2 Configure nodes 3 Orchestrate services
  • 11. Need to support more 1 Application services 2 Environments 3 Data services 4 Distributed services
  • 12. Optimize for 1 Scalability solved by on-demand ”cloud”
  • 13. Optimize for 1 Scalability solved by on-demand ”cloud” 2 Reliability solved by . . .
  • 14. So what yields reliability?
  • 15. Reason The required techniques of effective reasoning are pretty formal, but as long as programming is done by people that don’t master them, the software crisis will remain with us and will be considered an incurable disease. [?]
  • 17. Functions have inputs (Ruby) 1 # Two input arguments here 2 def add(x, y) 3 x + y 4 end 5 6 # One input argument here 7 def len(s) 8 s.size 9 end
  • 18. Functions have inputs (Scala) 1 object Functions { 2 // Two typed input arguments here 3 def add(x: Int , y: Int) = x + y 4 5 // One typed input argument here 6 def len(s: String) = s.size 7 }
  • 19. Functions return a result 1 scala > add(5, 6) 2 res0: Int = 11 3 4 scala > len("Hello ,␣Barcelona") 5 res1: Int = 16
  • 20. Only depend on inputs 1/2 1 scala > val defaultTimeout : Int = 30 2 scala > val timeout1: Option[Int] = Some (15) 3 scala > val timeout2: Option[Int] = None 4 scala > :paste 5 def defaulter[A](a: => A, ma: Option[A]) = 6 ma match { 7 case Some(x) => x 8 case None => a 9 }
  • 21. Only depend on inputs 2/2 1 scala > timeout1 2 timeout1: Option[Int] = Some (15) 3 4 scala > timeout2 5 timeout2: Option[Int] = None 6 7 scala > defaulter(defaultTimeout , timeout1) 8 res0: Int = 15 9 10 scala > defaulter(defaultTimeout , timeout2) 11 res1: Int = 30
  • 22. Return same result given same inputs 1 scala > len("Hello ,␣Barcelona") 2 res0: Int = 16 3 4 scala > len("Hello ,␣Barcelona") 5 res1: Int = 16 6 ... 7 scala > len("Hello ,␣Barcelona") 8 res3333333333: Int = 16 9 10 scala > // Always!
  • 23. The Big idea Referential Transparency Given same inputs, return same result. Always.
  • 24. Functions can use other values 1 // type aliasing a function 2 type Pred[A] = A => Boolean 3 4 // Passing a function as an input argument 5 def is[A](p: Pred[A])(a: A) = p(a) 6 7 // This uses already defined function +is+ 8 def not[A](p: Pred[A])(a: A) = !is(p)(a)
  • 25. Values can be functions 1 // Returning a function as a value 2 def lessThanN(n: Int): Pred[Int] = _ < n 3 4 // Using two in scope functions 5 def islessThanN(n: Int)(x: Int) = 6 is(ltN(n))(x) 7 8 // Those values can be functions :)
  • 26. Another important idea Higher Order Functions Build useful functions from simpler functions!
  • 29. UNIX Pipes 1/4 1 sh > echo -n "Hello ~~~" 2 | sed ’s/~//g’ 3 | tr ’[: lower :]’ ’[:upper :]’ 4 | wc -c 5 5
  • 30. UNIX Pipes 2/4 1 sh > alias sanitize=’sed "s/~//g"’ 2 sh > alias toUpper=’tr "[: lower :]" "[: upper :]"’ 3 sh > alias len=’wc -c’ 4 5 sh > alias myfun0=’sanitize | toUpper ’ 6 sh > alias myfun1=’myfun0 | wc -c’
  • 31. UNIX Pipes 3/4 1 sh > echo -n "Hello ~~~" 2 | sed ’s/~//g’ 3 | tr ’[: lower :]’ ’[:upper :]’ 4 | wc -c 5 5 6 7 sh > echo -n "Hello ~~~" | myfun1 8 5
  • 32. UNIX Pipes 4/4 • Character-based • File descriptors
  • 33. Function Composition 1/3 1 def toUpper = (s: String) => s.toUpperCase 2 def len = (s: String) => s.size 3 def sanitize = "~".r replaceAllIn (_, "")
  • 34. Function Composition 2/3 1 scala > def myfun0 = sanitize andThen toUpper 2 scala > def myfun1 = myfun0 andThen len 3 4 scala > myfun0 "Hello ~~~" 5 res0 = HELLO 6 7 scala > myfun1 "Hello ~~~" 8 res1: Int = 5
  • 35. Function Composition 3/3 • Value-based • Functions
  • 37. Functions as building blocks Figure: Build solid walls/foundations from simple, regular building blocks
  • 39. Equational Reasoning • Simpler testing; reduce test suite complexity • Substitute repeated expression with name • Refactoring is trivial; not error prone
  • 40. Testing non-RT code Figure: Side effecting, non-RT code requires complex testing setup.
  • 41. Equational Reasoning 1 scala > f(v1 , v2 , otherfun) == expected 2 res0: Boolean = true 3 • no setup/teardown complexity to test assertions • no mutation of SUT just for testing
  • 42. Equational Reasoning 1 scala > val x = "YOLO" 2 x: java.lang.String = YOLO 3 4 scala > val r1 = x.reverse 5 r1: String = OLOY 6 7 scala > "YOLO".reverse 8 res0: String = OLOY 9
  • 43. Equational Reasoning 1 scala > var age = 27 2 scala > def incr = { age += 1; age } 3 4 scala > incr 5 res0: Int = 28 6 7 scala > incr 8 res1: Int = 29 9
  • 44. Axiomatic Reasoning • Axioms are basic assumptions • Allows us to specify properties • Infer potential problems due to properties • What can we guarantee? What can we not?
  • 45. Axiomatic Reasoning 1 class Vpc < AwsResource 2 def initialize(conf) 3 @cidr = conf[’vpc.cidr ’] 4 end 5 end 6 # Is the Vpc instance created with an 7 # empty conf Hash guaranteeing the 8 # premise of the initialize contract? 9
  • 46. Axiomatic Reasoning 1 // validate inputs before instantiating 2 case class Vpc(cidr: Cidr) 3 4 def createVpc(ipStr: String , pfx: String) = 5 for { 6 ip <- IpAddress.toIpAddress(ipStr) 7 prefix <- Cidr.toPrefix(pfx) 8 } yield Vpc(Cidr(ip , prefix )) 9
  • 47. Axiomatic Reasoning 1 // if fun is 1-to -1 or bijective 2 // we know inverse must exist 3 def toLower(u: UpperCaseChar ): LowerCaseChar = ?? 4 def toUpper(l: LowerCaseChar ): UpperCaseChar = ?? 5 6 // idempotency typically important in ops 7 f(f(x)) == f(x) 8
  • 48. Axiomatic Reasoning 1 // commutativity important in dist sys 2 // f, binary operator over type A 3 // x and y are of type A then 4 // x @ y = y @ x 5 f(x, y) == f(y, x) 6 7 // associativity important in concurrency 8 // f, binary operator over type A 9 // x, y, and z are of type A then 10 // x @ (y @ z) = (x @ y) @ z 11 f(x, f(y, z)) == f(f(x, y), z) 12
  • 49. Axiomatic Reasoning - Distributed Systems • Simple causal consistency (partial ordering) • CRDTs (semilattice algebra) • Invariant confluence (coordination free execution)
  • 50. Axiomatic Reasoning - Concurrency • MVars, TVars, LVars have algebraic properties • Design for liveness via axiomatic reasoning
  • 51. Axiomatic Reasoning - More Exhaustive Testing • Property-based testing 1 // untested - uses scalacheck 2 def inverse[A]( implicit a: Action[A]) = 3 Prop.forAll { (x: A) => 4 x === a.inverse(a.inverse(x)) 5 } 6 7 implicit def actionArbitrary [A] 8 (implicit a: Arbitrary[A]) = Arbitrary { /* 9
  • 52. Generic Reasoning • Reasoning on generic type functions • Less specific the types the more we know about the function • Find design-time bugs
  • 53. Generic Reasoning 1 def f0[A](a: A): A = ??? 2
  • 54. Generic Reasoning 1 // Only definition 2 def f0[A](a: A): A = a 3
  • 55. Generic Reasoning 1 def f1[A](a: A)(ma: Option[A]): A = ??? 2
  • 56. Generic Reasoning 1 def f1[A](a: A)(ma: Option[A]): A = a 2 1 def f1[A](a: A)(ma: Option[A]): A = ma match { 2 case Some(x) => x 3 case None => a 4 } 5
  • 57. Generic Reasoning 1 def f2[A](as: List[A]): A = ??? 2
  • 58. Generic Reasoning 1 // what about an empty list? Yikes! 2 def f2[A](as: List[A]): A = ??? 3
  • 59. Generic Reasoning 1 // Assuming we want the head element! 2 // A more sensible type signature design 3 def f3[A](as: List[A]): Option[A] = as match { 4 case Nil => None 5 case a :: rest => a 6 } 7
  • 60. Generic Reasoning 1 def f4[A](ma: Option[A]): A = ??? 2
  • 62. Reasoning Process • Assume as little as possible • Define all inputs • Build only on top of RT • Derive properties from domain • Infer properties from generic types
  • 63. Methods / Techniques • Encode normal error cases in results • Strive for termination • Strive for RT where ever possible
  • 64. Iteration / Improvement • So much to learn • Start out informal; add formal reasoning as needed/learned • The more we do the easier it gets
  • 66. Mainstream Package Management Based on shared + mutable state (filesystem)
  • 68. Alternatives • shared + immutable • private + mutable • expensive coarse grained locks • hybrid without the expense
  • 69. Define all inputs • Force clean build env (chroot) • Requires explicit inputs • Full dependency definition
  • 70. Ensure RT • Use private mutable space • Different inputs, different result • Symlink unique results (atomic op)
  • 71. Different inputs give unique results • Shared + immutable provides RT • New inputs result in different path • Install many versions/configurations • Means we can do binary substitution
  • 72. RT foundation • Update existing node more reliably • Supports immutable infrastructure • Supports congruent configuration
  • 73. Recap 1 Referential Transparency allows us to reason 3 HOFs & Composition is the motar 4 Logical reasoning provides solid foundation
  • 74. Recap 1 Referential Transparency allows us to reason 2 Functions are simple, regular building blocks 3 HOFs & Composition is the motar 4 Logical reasoning provides solid foundation
  • 75. Don’t fear the maths! Figure: Domain specific language of the sciences and engineering
  • 76. Questions Figure: Heckle me @SusanPotter later too.