Why Functional Programming Matters: Concepts and History

Why Functional Programming Matters: Concepts and History
Slide Note
Embed
Share

Functional programming is a paradigm that offers unique ways to modularize applications, challenging the traditional object-oriented thinking. Explore the basic and optional functional concepts, the importance of programming languages in translating ideas into machine instructions, and the significance of the abstraction principle in programming.

  • Functional Programming
  • Concepts
  • Abstraction Principle
  • Programming Languages
  • History

Uploaded on Mar 17, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Why Functional Programming Matters

  2. Concepts What is a programming language? a formal language designed to translate ideas into machine instructions

  3. Concepts What do programming languages do for us? they provide "first-class concepts" they provide a means for thinking about the problem they provide an abstraction principle

  4. Concepts What is the abstraction principle? Pierce (2002), from "Types and Programming Languages": "Each significant piece of functionality in a program should be implemented in just one place in the source code. Where similar functions are carried out by distinct pieces of code, it is generally beneficial to combine them into one by abstracting out the varying parts." Cole/Morrison (1982), from "An introduction to programming with S-algol":

  5. Concepts In short: like most languages, programming languages give us a framework--a set of abstractions--by which we can think about (and solve) problems if the abstraction does not exist within the language, it is difficult (if not impossible) to embrace or use

  6. History In the beginning, God created objects....

  7. History Functional programming is a different way of thinking about modularizing applications And, admittedly at times it is a different way of thinking that runs entirely contradictory to the way that object-oriented programmers think

  8. What's it mean, exactly? FUNCTIONAL PROGRAMMING

  9. Concepts Basic functional concepts immutable values over mutable variables functions as first-class values currying, partial-application of functions expressions-not-statements laziness/deferred execution

  10. Concepts Optional functional concepts strongly-typed, type-inferenced recursion tuples, lists pattern-matching

  11. Concepts Immutable values once bound, a binding remains constant throughout its lifetime thus offers no opportunity for confusion/reassignment/etc Values over variables "values" are not "variables" name/value binding is fixed once bound

  12. Concepts A functional pseudocode example let x = 0 let y = 1 let z = x + y

  13. Concepts Functions as first-class values think about common operations-if we could vary the actual operation itself as a parameter, how much work could be saved? example: you need to iterate through a collection to... ... and each time you write it as a "for" loop, you're violating DRY this enables the use of functions as "higher- order" functions "take this function, and execute inside your

  14. Concepts Higher-order functions let numbers = [1, 2, 3, 4, 5] let squares = numbers.map((num) -> num * num); // squares = [1, 4, 9, 16, 25]

  15. Concepts Partial application providing some of the parameters (but not all) to a function and capturing that as a function to be called Currying it turns out (thank you Alonzo Church!) that all functions can be reduced to functions taking one parameter and either yielding a result or another function this permits easy "pipelining" and composition

  16. Concepts Partial application let add x y = x + y let five = add 2 3 // = 5 let addBy2 = add 2 // = (anonymous fn) = 2 + y let six = addBy2 addBy2 2 // = 6

  17. Concepts Function composition In functional languages, then, we achieve reuse through the composition/combination of functional parts into larger functions By doing so, we "build up" larger more complex functions When combining several in a row using currying, this is also called "pipelining"

  18. Concepts Strongly-typed the dynamic language community will have you believe that it's better to write unit tests by hand than to have a system that can do common-sense checking for you Type-inferenced why do I have to be explicit to the language/compiler, when it can figure out what I'm trying to do and when?

  19. Concepts Recursion immutable values doesn't mean no state changes instead, hold state on the stack, not in mutable memory

  20. Concepts Expressions-not-statements this is an outgrowth of the functions-as-first- class-citizens idea: if functions yield values, what is the practical difference between a keyword and a function? even C++ tried to make user-defined elements look and feel like built-in constructs and vice versa if we're really good about this, developers can create new "language" primitives and nobody will know the difference

  21. Concepts Tuples, lists "bundles" of data in different directions tuples give developers a "lightweight" object that needn't be named or otherwise formalized

  22. Concepts Pattern-matching switch/case is to pattern-matching as my kid's soccer team is to Arsenal or Manchester United pattern-matching also encourages "destructuring" of data when necessary/desired pattern-matching can operate off of collections of values

  23. Concepts Laziness object-oriented laziness has nothing on functional laziness don't compute anything until absolutely necessary (but make sure to maintain the dependency graph so everything is there when needed) laziness is highly encouraged/permissible in pure FP just to be fair, laziness is highly desirable

  24. Concepts Sequences lots of things can be seen as sequences characters in a string fields in a record records in a database files in a directory algorithmic calculations (factorial, fibonacci, ...) lines in a file user interactions sequences and collections have a deep relationship

  25. Seeing it in action EXAMPLES

  26. Wrapping up SUMMARY

  27. Summary Abstractions Parsing, for example, is made easier because the functional approach better matches what parsers do Transaction Scripts (Fowler, Patterns of Enterprise Application Architecture) are ideally highly functional in nature How many tortured object designs must we build before we acknowledge that objects don't fit everything we build?

  28. Summary Continuations instead of wiring steps together explicitly, do it implicitly by passing in the next "thing" to do as a function Concurrency instead of locking explicitly, allow the underlying language library or runtime to manage the physical details of the parallelization, or (better yet) avoid the need entirely

  29. Summary Functional programming is not going to replace object-orientation objects didn't replace procedural programming, but built on top of it and incorporated it most new FP languages are functional/object hybrids, not pure FP languages The best functional usage, then, will supplement your objects, and vice versa

  30. Summary Functional programming represents a new tool in your toolbox, not wholesale rejection of prior art

More Related Content