Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Functional programming - part 1

Yejun Su
January 03, 2023

Functional programming - part 1

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

Yejun Su

January 03, 2023
Tweet

More Decks by Yejun Su

Other Decks in Programming

Transcript

  1. 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.
  2. Pure functions Don't have any side e ff ects. Given

    the same arguments, return the same result.
  3. 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.
  4. 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
  5. 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.
  6. 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
  7. 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
  8. Data • Facts about events. • Their meanings are di

    ff erent according to situations. • Examples: • An email address • A user structure • A dinner receipt
  9. 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.
  10. 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
  11. We are in a world programs run on multicores and

    distributed devices • Functional programs run in parallel. • Compilers optimize for functional programs.