際際滷

際際滷Share a Scribd company logo
Pure Functions
Leon Maia
https://leonmaia.com
Agenda
Whats a function?
Whats a pure function?
Whats a side effect?
Referential transparency.
Keep it simple.
Examples.
The trouble with shared state.
2
Whats a function?
3
A function is something that takes an input, and
produces some output.
function sum(a, b) {
return a+b;
}
For example, this is a function that takes two numbers
and returns the sum.
Whats a pure function?
4
A pure function is a function which:
 Given the same input, will always return the same
output.
 Produces no side effects.
 Relies on no external state.
Whats a side effect?
5
Side effects may include, but are not limited to:
 Changing the file system.
 Inserting a record into a database.
 Making an http call.
 Mutations.
 Printing to the screen / logging.
 Obtaining user input.
 Accessing system state.
Referential transparency.
6
A function in algebra: f(x) = 2x
> const double = x => x * 2;
Since `double` is a pure function we can replace
`double(5)` with 10 without changing the meaning.
So, `console.log(double(5));` is the same as
`console.log(10);`
Keep it simple.
7
Pure functions take some input and return some output
based on that input. They are the simplest reusable
building blocks of code in a program:
 KISS (Keep It Simple, Stupid)
Pure functions are stupid simple in the best possible
way.
Examples:
8
Examples:
9
In summary.
 Extremely independent.
 Easy to move around.
 Refactor.
 Easy to reorganize in your code.
 Gives more flexibility.
 Adaptable to future changes.
 Easy to test.
10
The trouble with shared state
11
The trouble with shared state
 Reliant on sequences which vary depending on indeterministic factors.
 The output is impossible to predict.
 Impossible to properly test.
 Impossible to fully understand.
12

| Confidential
There is no non-determinism without mutable states. There
is no race condition or deadlock without mutable states.
Mutable states are the root of the problem.
13
 Martin Odersky
| Confidential
non-determinism = parallel processing + mutable state
14
Recipe for bugs.
 A single threaded JS engine does not imply that there is no
concurrency.
 API I/O, event listeners, web workers, iframes, and timeouts.
Combine that with shared state, and youve got a recipe for bugs.
15

| Confidential
Shared mutable state and parallel processing doesn't go
together at all. If you do not use proper synchronization you
will create extremely difficult to find bugs and your
programs are basically broken even if they appear to work
just fine in most cases.
16
| Confidential
Thanks!
17

More Related Content

Pure functions using Javascript.

  • 2. Agenda Whats a function? Whats a pure function? Whats a side effect? Referential transparency. Keep it simple. Examples. The trouble with shared state. 2
  • 3. Whats a function? 3 A function is something that takes an input, and produces some output. function sum(a, b) { return a+b; } For example, this is a function that takes two numbers and returns the sum.
  • 4. Whats a pure function? 4 A pure function is a function which: Given the same input, will always return the same output. Produces no side effects. Relies on no external state.
  • 5. Whats a side effect? 5 Side effects may include, but are not limited to: Changing the file system. Inserting a record into a database. Making an http call. Mutations. Printing to the screen / logging. Obtaining user input. Accessing system state.
  • 6. Referential transparency. 6 A function in algebra: f(x) = 2x > const double = x => x * 2; Since `double` is a pure function we can replace `double(5)` with 10 without changing the meaning. So, `console.log(double(5));` is the same as `console.log(10);`
  • 7. Keep it simple. 7 Pure functions take some input and return some output based on that input. They are the simplest reusable building blocks of code in a program: KISS (Keep It Simple, Stupid) Pure functions are stupid simple in the best possible way.
  • 10. In summary. Extremely independent. Easy to move around. Refactor. Easy to reorganize in your code. Gives more flexibility. Adaptable to future changes. Easy to test. 10
  • 11. The trouble with shared state 11
  • 12. The trouble with shared state Reliant on sequences which vary depending on indeterministic factors. The output is impossible to predict. Impossible to properly test. Impossible to fully understand. 12
  • 13. | Confidential There is no non-determinism without mutable states. There is no race condition or deadlock without mutable states. Mutable states are the root of the problem. 13 Martin Odersky
  • 14. | Confidential non-determinism = parallel processing + mutable state 14
  • 15. Recipe for bugs. A single threaded JS engine does not imply that there is no concurrency. API I/O, event listeners, web workers, iframes, and timeouts. Combine that with shared state, and youve got a recipe for bugs. 15
  • 16. | Confidential Shared mutable state and parallel processing doesn't go together at all. If you do not use proper synchronization you will create extremely difficult to find bugs and your programs are basically broken even if they appear to work just fine in most cases. 16