Learning the skill "Distinguish Actions, Calculations and Data".
Functional programmingJames Su - 2022/04/15Surfing in the changing world
View Slide
What is functionalprogramming?
Functional programmingin Wikipedia• A programming paradigm characterized by mathematical functions andthe avoidance of side effects.• A programming style that uses only pure functions without side effects.
Side effectsAnything a function doesother than returning a value
Pure functionsDon't have any side effects.Given the same arguments,return the same result.
Benefits 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.
But, we need side effectsin real world• Send an email• Reading afile• Writing to database• Making a web request• Blinking a light
Functional programmingin 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.
DistinguishingActions, Calculations and Data
Actions• Depend on how many times or when it is run.• Also called: functions with side effects.• Examples:• Send an email• Read from a database
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
Data• Facts about events.• Their meanings are different according to situations.• Examples:• An email address• A user structure• A dinner receipt
Benefits 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.
Thinking a problem
Stratified designOrganizing code by"rate of change"
Why functional programmingpopular these days?
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
We are in a worldprograms run on multicores and distributed devices• Functional programs run in parallel.• Compilers optimize for functional programs.
Q & A