Yejun Su
January 03, 2023
70

# Functional programming - part 1

Learning the skill "Distinguish Actions, Calculations and Data".

January 03, 2023

## Transcript

1. Functional programming
James Su - 2022/04/15
Sur
f
ing in the changing world

2. What is functional
programming?

3. Functional programming
in Wikipedia
• A programming paradigm characterized by mathematical functions and
the avoidance of side e
ff
ects.

• A programming style that uses only pure functions without side e
ff
ects.

4. Side effects
Anything a function does
other than returning a value

5. Pure functions
Don't have any side e
ff
ects.
Given the same arguments,
return the same result.

6. Bene
f
its of pure functions
• Don't depend on how many times and when they are run.

• Results are predictable, easier to understand and control.

• Compilers can optimize for predictable things.

7. But, we need side effects
in real world
• Send an email

f
ile

• Writing to database

• Making a web request

8. Functional programming
in real world
• A set of skills, concepts and principles:

• Distinguishing actions, calculations, and data

• Staying immutable in a mutable language

• ... and more

• They are valuable and regardless of what language you use.

9. Distinguishing

Actions, Calculations and Data

10. Actions
• Depend on how many times or when it is run.

• Also called: functions with side e
ff
ects.

• Examples:

• Send an email

11. Calculations
• Computations from input to output.

• Also called: pure functions, mathematical functions.

• Examples:

• Find the users to send email

• Calculate total price in shopping cart

12. Data

• Their meanings are di
ff
erent according to situations.

• Examples:

• A user structure

• A dinner receipt

13. Bene
f
its of distinguishing actions, calculations and data

• Clarify parts that need special attention (actions)

• What data we will need to capture (data)

• What decisions we will need to make (calculations)

• Coding a solution

• Write code with immutable in mind: data > calculations > actions.

• Refactor the code to better separate from actions, calculations and data.

14. Thinking a problem

15. Thinking a problem

16. Strati
f
ied design
Organizing code by

"rate of change"

17. Why functional programming
popular these days?

18. Functional languages
• Erlang

• WhatsApp - Why WhatsApp Only Needs 50 Engineers for Its 900M Users

• Elixir

• Discord - How Discord Scaled Elixir to 5,000,000 Concurrent Users

• Apple - Working on APIs towards carbon neutral by 2030

• Clojure

• Roam Research - State of Clojure 2021 Results

• Elm

• https://github.com/jah2488/elm-companies

• Blazing Fast HTML

19. We are in a world
programs run on multicores and distributed devices
• Functional programs run in parallel.

• Compilers optimize for functional programs.

20. Q & A