Top Banner
Functional Programming [introduction]
54
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Intro to Functional Programming @ Scala Montreal

Functional Programming

[introduction]

Page 2: Intro to Functional Programming @ Scala Montreal

Le Plan- Motivations- Definitions and Examples

Page 3: Intro to Functional Programming @ Scala Montreal

Motivations

Page 4: Intro to Functional Programming @ Scala Montreal

What makes programming so hard?

Page 5: Intro to Functional Programming @ Scala Montreal

“Controlling complexity is the essence of computer programming.”- Brian Kernighan

Page 6: Intro to Functional Programming @ Scala Montreal

Complexity comes from input and state.

Possible Inputs

Possible States

Possible OutcomesX =

Page 7: Intro to Functional Programming @ Scala Montreal

We should aim at reducing the input and state space.

Possible Inputs

Possible States

Possible OutcomesX =

Page 8: Intro to Functional Programming @ Scala Montreal

In Object-Oriented programming, everything* is an object.

Page 9: Intro to Functional Programming @ Scala Montreal

Objects combine data and behavior.

Page 10: Intro to Functional Programming @ Scala Montreal

Objects are state machines.

A = xB = y

A = x’B = y’

A = x’B = y

A = xB = y’

Page 11: Intro to Functional Programming @ Scala Montreal

Most objects are badly designed state machines.

A = xB = y

A = x’B = y’

A = x’B = y

A = xB = y’

Page 12: Intro to Functional Programming @ Scala Montreal

Large state space are hard to reason about.

Page 13: Intro to Functional Programming @ Scala Montreal

Concurrency adds new visible states.

A = xB = y

A = x’B = y’

A = x’B = y

A = xB = y’

Page 14: Intro to Functional Programming @ Scala Montreal

How can we write correct code if we can’t reason about it?

Page 15: Intro to Functional Programming @ Scala Montreal

Let’s add more unit tests!

Page 16: Intro to Functional Programming @ Scala Montreal

Let’s add more unit tests!

Page 17: Intro to Functional Programming @ Scala Montreal

Definitionsand

Examples

Page 18: Intro to Functional Programming @ Scala Montreal

Functional Programming imposes constraints that eliminate states and ease reasoning.

Page 19: Intro to Functional Programming @ Scala Montreal

Functional Programming is about values, functions and types*.

Page 20: Intro to Functional Programming @ Scala Montreal

Values

Page 21: Intro to Functional Programming @ Scala Montreal

Values are immutable.

Page 22: Intro to Functional Programming @ Scala Montreal
Page 23: Intro to Functional Programming @ Scala Montreal

What about data structures?Can they be values?

Page 24: Intro to Functional Programming @ Scala Montreal

Persistent data structures create a new updated version. They are immutable.

v

v

Nil

Page 25: Intro to Functional Programming @ Scala Montreal
Page 26: Intro to Functional Programming @ Scala Montreal

Immutability eliminates state and means you can safely share the reference.

Page 27: Intro to Functional Programming @ Scala Montreal

Functions

Page 28: Intro to Functional Programming @ Scala Montreal

A function takes arguments and produces a result.

(Pure) Functionshave no side-effects.

Page 29: Intro to Functional Programming @ Scala Montreal

Side-effects examples:- write to disk- read from stdin- throw an exception- change a state

Page 30: Intro to Functional Programming @ Scala Montreal
Page 31: Intro to Functional Programming @ Scala Montreal

Having no side-effecthelps reasoning about code.

Page 32: Intro to Functional Programming @ Scala Montreal

Having no side-effect simplifies testing.

Page 33: Intro to Functional Programming @ Scala Montreal

Errors are handled by returning them as results.

Page 34: Intro to Functional Programming @ Scala Montreal
Page 35: Intro to Functional Programming @ Scala Montreal

Updating a state is done by creating a new instance with the updated state.

Page 36: Intro to Functional Programming @ Scala Montreal
Page 37: Intro to Functional Programming @ Scala Montreal

For other necessary side-effects, push them on the boundaries.

Page 38: Intro to Functional Programming @ Scala Montreal

Looping can be implemented with recursion*.

Page 39: Intro to Functional Programming @ Scala Montreal

A high-order functions takes functions as parameters and/or produces a function.

Page 40: Intro to Functional Programming @ Scala Montreal
Page 41: Intro to Functional Programming @ Scala Montreal
Page 42: Intro to Functional Programming @ Scala Montreal

Types

Page 43: Intro to Functional Programming @ Scala Montreal

Types define classifications of values.

Page 44: Intro to Functional Programming @ Scala Montreal

With types, the compilercan verify some aspects of correctness.

Page 45: Intro to Functional Programming @ Scala Montreal
Page 46: Intro to Functional Programming @ Scala Montreal

Meta

Page 47: Intro to Functional Programming @ Scala Montreal

Object-Oriented is about semantic.

Functional Programming is about shape.

Page 48: Intro to Functional Programming @ Scala Montreal
Page 49: Intro to Functional Programming @ Scala Montreal
Page 50: Intro to Functional Programming @ Scala Montreal
Page 51: Intro to Functional Programming @ Scala Montreal

Reuse by shape is more powerful than reuse by semantic.

Page 52: Intro to Functional Programming @ Scala Montreal

BUT understanding shape is harder than understanding semantic.

Page 53: Intro to Functional Programming @ Scala Montreal

Summary

Page 54: Intro to Functional Programming @ Scala Montreal

Summary

- Functional programing: immutable values, pure functions and precise types*.

- Benefits are:- Simple code- Concurrency and Parallelism for free- Easy to reason about- Reusable