Some basic FP concepts

41
Some basic FP concepts @friemens

description

Introduction to important basic functional programming concepts.

Transcript of Some basic FP concepts

Page 1: Some basic FP concepts

Some basic FP concepts

@friemens

Page 2: Some basic FP concepts

Why is mutable state problematic?

Programming without assignment?

Functions as Values

Leave data alone!

Handle mutable state safely

Page 3: Some basic FP concepts

1930s Lambda Calculus

1960s Lisp

1990s Haskell

Nothing new here!

Page 4: Some basic FP concepts

„Familiarity and Simplicity are orthogonal concepts.“

Rich Hickey

Page 5: Some basic FP concepts

Why is mutable state problematic?

Page 6: Some basic FP concepts

A system without side-effects is useless,

but...

Page 7: Some basic FP concepts

Mutation hinders reasoning.

Page 8: Some basic FP concepts

Mutable state increases test effort.

Page 9: Some basic FP concepts

Mutation introduces order.

1. 2.

Page 10: Some basic FP concepts

Mutable state makes concurrency hard.

Page 11: Some basic FP concepts

Side-effects restrict how the machine can help us.

FP programming

OOP programming

Page 12: Some basic FP concepts
Page 13: Some basic FP concepts

Pure Functions

Side-effects

Context dependance

The future-proof structure of any software system.

Page 14: Some basic FP concepts

Functions as Values

Page 15: Some basic FP concepts

Pure Function=

No side-effects+

Result depends only on param values

Page 16: Some basic FP concepts

Lambda Expression=

An anonymous function.

Page 17: Some basic FP concepts

Closure=

A function + some captured environment.

Page 18: Some basic FP concepts

Higher-Order Function

Fn: [Any... -> Any]F: [Any -> Fn]G: [Fn -> Any]H: [Fn -> Fn]

A function that - does something with another function- or returns a function, - or both.

Page 19: Some basic FP concepts

Function Application

Fn: [Any... -> Any]apply: [Fn Any* -> Any]

Take collection of param values and invoke function.

Page 20: Some basic FP concepts

Partial Application

Fn: [Any1...Anym...Anyn -> Any]partial: [Fn Any1...Anym -> [Anym+1...Anyn -> Any]]

Create a new function with some arguments fixed.

(„Currying“ is automatic partial application.)

Page 21: Some basic FP concepts

Function Composition

F: [X -> Y]G: [Y -> Z]compose: [G F -> [X -> Z]]

Concatenate computations.

Page 22: Some basic FP concepts

Programming without assignment?

Page 23: Some basic FP concepts

Good bye =, for, while and cousins, ...

… say hello to let, map, filter and friends!

Page 24: Some basic FP concepts

Thinking in collection transformations.

xs

ys

z

filter

map

concat reduce

Page 25: Some basic FP concepts

Don't be so eager!

map filter mapcat into

Page 26: Some basic FP concepts

Cheap parallelization.

reduce

combine

Page 27: Some basic FP concepts

Handle mutable state safely

Page 28: Some basic FP concepts

Separate state from identity.

State x State x'

Identity

swap!

f

Page 29: Some basic FP concepts

Give identities well-defined concurrency semantics.

Page 30: Some basic FP concepts

Share immutable state liberally.

Be restrictive with access to identities.

Page 31: Some basic FP concepts

Leave data alone!

Page 32: Some basic FP concepts

Objects claim feature completeness.

BigHero

-a string

-a map

+method1+method2+method3

Page 33: Some basic FP concepts

You can't foresee the future.

Page 34: Some basic FP concepts

You will violate the Open-Closed-Principle.

Page 35: Some basic FP concepts

„Inventing a class with its own interface to hold a piece of information is like inventing a new language to write every short story. “

Rich Hickey

Page 36: Some basic FP concepts

Data is simple.

Page 37: Some basic FP concepts

Data Model

API

DSL

Page 38: Some basic FP concepts

Wrap up

Page 39: Some basic FP concepts

OO makes code understandable by encapsulating moving parts.

FP makes code understandable by minimizing moving parts.

Michael Feathers

Page 40: Some basic FP concepts

The future is functional.

You need to practice FP before you „get“ it.

It's more fun!

Page 41: Some basic FP concepts

Questions?