$30 off During Our Annual Pro Sale. View Details »

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
    James Su - 2022/04/15
    Sur
    f
    ing in the changing world

    View Slide

  2. What is functional
    programming?

    View Slide

  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.

    View Slide

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

    View Slide

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


    View Slide

  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.

    View Slide

  7. 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

    View Slide

  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.

    View Slide

  9. Distinguishing


    Actions, Calculations and Data

    View Slide

  10. 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

    View Slide

  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

    View Slide

  12. Data
    • Facts about events.


    • Their meanings are di
    ff
    erent according to situations.


    • Examples:


    • An email address


    • A user structure


    • A dinner receipt

    View Slide

  13. 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.

    View Slide

  14. Thinking a problem

    View Slide

  15. Thinking a problem

    View Slide

  16. Strati
    f
    ied design
    Organizing code by


    "rate of change"

    View Slide

  17. Why functional programming
    popular these days?

    View Slide

  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

    View Slide

  19. View Slide

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


    • Compilers optimize for functional programs.

    View Slide

  21. View Slide

  22. Q & A

    View Slide