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

Functional Programming

Functional Programming

A quick intro to functional programming.

Avatar for Luan Fagundes

Luan Fagundes

July 18, 2020

More Decks by Luan Fagundes

Other Decks in Programming

Transcript

  1. 4. CONCLUSION Trends & Drawbacks What is & Design Principles

    PRESENTATION 2. Fundamental Concepts & Techniques Conclusion & References 3. ANALYSIS INTRODUCTION 1.
  2. Thought Programming is about trying to make the future less

    painful. It’s about making things easier for our teammates. It’s about getting things wrong and being able to recover. It’s about forming good habits. It’s about understanding your toolset. Pragmatic Programmer
  3. Why I am talking about this? Functional programming adds an

    important new tool to your toolbox that will make you a better developer in any language. #diversity
  4. WHAT IS? It’s a paradigm that expresses a set of

    operations without revealing how they’re implemented or how data flows through them. Object-oriented programming Procedural Reactive programming Pascal, C++, Java, JavaScript, Swift, C#, Scala, Jynx, Closure, Visual Basic, Common Lisp, Scheme, Perl, PHP, Python, Ruby, Kotlin… Haskell only supports functional programming. Smalltalk only supports object-oriented programming.
  5. WHAT IS NOT? reddit So I am working on a

    large JavaScript project where the now lead developer has started heavily using functional programming throughout the code base, but to a level that makes it hard to work in. I'll give you an example - essentially he will try to never write an if statement (e.g. will create a function like "throwIf"), never write a loop, never declare variables, chain everything into massive pipe and compose functions.
  6. DESIGN PRINCIPLES Extensibility Do I constantly refactor my code to

    support additional functionality? Reusability Is there a lot of duplication? Easy to reason about Is my code unstructured and hard to follow? Easy to modularize If I change one file, is another file affected? Testability Do I struggle to unit test my functions?
  7. FUNDAMENTAL CONCEPTS FP is closely related to the singularity principle,

    which states that functions should have a single purpose. Singularity principle
  8. Pure functions FUNDAMENTAL CONCEPTS Given the same input, will always

    return the same output. Produces no side effects. In the case of a mutable state, two calls of the same function can potentially produce two different results.
  9. Referential transparency FUNDAMENTAL CONCEPTS It is used to replace expression

    in a program, without actually changing the final result of the program. Any piece of code that mutates variables, objects, or data structures outside of a function are not referentially transparent. The benefit is that it eliminates side-effects from your code.
  10. Immutability FUNDAMENTAL CONCEPTS An immutable object is an object whose

    state cannot be modified after it is created. Redux is a great example of use of functional concepts like immutable state.
  11. Recursion TECHNIQUES A recursive function is a function that calls

    itself until a base condition is true, and execution stops.
  12. FUN FACT TECHNIQUES The most-common cause of stack overflow is

    excessively deep or infinite recursion, in which a function calls itself so many times that the space needed to store the variables and information associated with each call is more than can fit on the stack.
  13. Map, Filter, Reduce TECHNIQUES These are useful functions for iterating

    over a list and doing sort of transformation or computation.
  14. Closures TECHNIQUES Nested functions are closures that have a name

    and can capture values from their enclosing function.
  15. Function composition TECHNIQUES Function composition is the ability to combine

    small functions together to make bigger functions.
  16. Higher-order functions TECHNIQUES Higher order functions are simply functions that

    can either accept functions or closures as arguments, or return a function/closure. Map, Filter, Reduce
  17. Currying TECHNIQUES It is a process of converting a function

    that accepts n arguments into n functions that accept only one argument.
  18. Lazy Evaluation TECHNIQUES It is an evaluation strategy which delays

    the evaluation of an expression until its value is needed.
  19. TRENDS CONCURRENCY Unfortunately most OOP languages cannot take advantage of

    multiple processors at once because of the interdependencies between data. In pure functional programming languages the computer can run two (or many more) functions at once because those functions are not altering outside state information.
  20. DRAWBACKS Hard to read and debug if you are coming

    from imperative programming background. Recursion is very expensive to use, It has a high cost in terms of memory. Terminology: curry, pure functions, referential transparency. Pure functions: combining them into a complete application is very hard. 01 02 03 04
  21. CONCLUSION It's not necessary you move to functional programming tomorrow

    or embrace all his concepts. You can just combine with the program paradigm that you are using today and get interesting results.
  22. REFERENCES BOOKS • Functional Programming in JavaScript: How to Improve

    Your JavaScript Programs • Functional Swift: Updated for Swift 4 REDDIT • r/programming • r/javascript • r/swift WEB • medium • wikipedia • Hacker news • Stack overflow • Objc.io • Hackingwithswift • dev.to
  23. —Martin Fowler “Any fool can write code that a computer

    can understand. Good programmers write code that humans can understand.” ”
  24. CREDITS: This presentation template was created by Slidesgo, including icons

    by Flaticon, and infographics & images by Freepik. THANKS! Do you have any questions? [email protected] pebmed.com.br Please keep this slide for attribution.