Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

What is functional programming?

Slide 3

Slide 3 text

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.

Slide 4

Slide 4 text

Side effects Anything a function does other than returning a value

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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.

Slide 7

Slide 7 text

But, we need side effects in real world • Send an email • Reading a f ile • Writing to database • Making a web request • Blinking a light

Slide 8

Slide 8 text

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.

Slide 9

Slide 9 text

Distinguishing Actions, Calculations and Data

Slide 10

Slide 10 text

Actions • Depend on how many times or when it is run. • Also called: functions with side e ff ects. • Examples: • Send an email • Read from a database

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Data • Facts about events. • Their meanings are di ff erent according to situations. • Examples: • An email address • A user structure • A dinner receipt

Slide 13

Slide 13 text

Bene f its of distinguishing actions, calculations and data • Thinking about a problem • 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. • Reading code • Refactor the code to better separate from actions, calculations and data.

Slide 14

Slide 14 text

Thinking a problem

Slide 15

Slide 15 text

Thinking a problem

Slide 16

Slide 16 text

Strati f ied design Organizing code by "rate of change"

Slide 17

Slide 17 text

Why functional programming popular these days?

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

We are in a world programs run on multicores and distributed devices • Functional programs run in parallel. • Compilers optimize for functional programs.

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

Q & A