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

dotnetMalaga_2022_Functional_Programming.pdf

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →
Avatar for Diego Parra Diego Parra
September 16, 2022
17

 dotnetMalaga_2022_Functional_Programming.pdf

Avatar for Diego Parra

Diego Parra

September 16, 2022

Transcript

  1. CONFIDENTIAL. Copyright © 2 Who am I? • 21 years

    of experience • Played different roles in Software Building (developer, team lead, architect, engineering director) • Currently Chief Software Engineer at • Also, Key developer at
  2. CONFIDENTIAL. Copyright © 4 CONFIDENTIAL. Copyright © 4 • Deterministic

    • Immutable(Does not change state or parameters) • Can live without context • Functions as first class values
  3. CONFIDENTIAL. Copyright © 5 CONFIDENTIAL. Copyright © 5 BENEFITS Easier

    to test Better readability More compact code Thread-safe by default Paralelizable out of the box
  4. CONFIDENTIAL. Copyright © 13 CONFIDENTIAL. Copyright © 13 What is

    a “pure” function? Output depends on inputs Cause no side effects Can live in the “void”
  5. CONFIDENTIAL. Copyright © 14 CONFIDENTIAL. Copyright © 14 What is

    a “side effect”? Mutates global state Mutates input args Modify I/O Invoke external services Throw exceptions
  6. CONFIDENTIAL. Copyright © 15 CONFIDENTIAL. Copyright © 15 So, are

    “impure” functions bad? In Mutates global state Mutates input args Modify I/O Invoke external services Throw exceptions AVOID! ISOLATE
  7. CONFIDENTIAL. Copyright © 17 CONFIDENTIAL. Copyright © 17 “Honest” functions,

    you say? It returns a value of the declared type It doesn’t throw exceptions It never returns null
  8. CONFIDENTIAL. Copyright © 20 Why null is dangerous Pollutes mathematical

    models, example: - Age has 120 possible values - If we add null as possible value, we will have 121 - If we combine with Gender (2 values + null) - We could have 243 possible values! We need to deal with all these cases Ambiguous function signatures Reference types are nullable and struct types are not Defensive null-checking - In case an unexpected null is found, what should we do?
  9. CONFIDENTIAL. Copyright © 21 CONFIDENTIAL. Copyright © 21 Can we

    get rid of the infamous “Object reference not set to an instance of an object”? Note: I’m going to use LaYumba library for the following examples https://github.com/la-yumba/functional-csharp-code
  10. CONFIDENTIAL. Copyright © 30 FUNCTOR is … MONAD is …

    a M<T> that has a Map type function a M<T> that has both, a Bind and a Return type functions … Therefore, IEnumerable<T> is both a Functor and a Monad!!! BONUS: Task<T> is too
  11. CONFIDENTIAL. Copyright © 32 Normally no if, for, try or

    catch statements A bunch of one- liners More condensed code Pipe.lining()