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

Functional Programming Is Awesome

Functional Programming Is Awesome

An introduction/overview of the properties of functional programming, followed by an exploration of some benefits. Code examples are in either javascript or scala.

Kyle Willett

May 18, 2016
Tweet

Other Decks in Programming

Transcript

  1. fp

  2. First Class Functions There is no restriction on the use

    of functions. 1) Return results can be functions. (i.e. Functions can return functions.)
  3. First Class Functions There is no restriction on the use

    of functions. 1) Return results can be functions. (i.e. Functions can return functions.)
  4. First Class Functions There is no restriction on the use

    of functions. 1) Return results can be functions. (i.e. Functions can return functions.)
  5. First Class Functions There is no restriction on the use

    of functions. 1) Return results can be functions. (i.e. Functions can return functions.) 2) Arguments can be functions. (i.e. Functions can take functions.)
  6. First Class Functions There is no restriction on the use

    of functions. 1) Return results can be functions. (i.e. Functions can return functions.) 2) Arguments can be functions. (i.e. Functions can take functions.)
  7. First Class Functions There is no restriction on the use

    of functions. 1) Return results can be functions. (i.e. Functions can return functions.) 2) Arguments can be functions. (i.e. Functions can take functions.)
  8. First Class Functions There is no restriction on the use

    of functions. 1) Return results can be functions. (i.e. Functions can return functions.) 2) Arguments can be functions. (i.e. Functions can take functions.) makeAdd and addAndMultiply are higher-order functions.
  9. Referential Transparency The values of expressions do not change. •

    They are context independent. (doesn’t matter when or where)
  10. Referential Transparency The values of expressions do not change. •

    They are context independent. (doesn’t matter when or where) • The same input produces same output. (predictable, deterministic program)
  11. Referential Transparency The values of expressions do not change. •

    They are context independent. (doesn’t matter when or where) • The same input produces same output. (predictable, deterministic program)
  12. Referential Transparency The values of expressions do not change. •

    They are context independent. (doesn’t matter when or where) • The same input produces same output. (predictable, deterministic program)
  13. Referential Transparency The values of expressions do not change. •

    They are context independent. (doesn’t matter when or where) • The same input produces same output. (predictable, deterministic program)
  14. Referential Transparency The values of expressions do not change. •

    They are context independent. (doesn’t matter when or where) • The same input produces same output. (predictable, deterministic program)
  15. Referential Transparency The values of expressions do not change. •

    They are context independent. (doesn’t matter when or where) • The same input produces same output. (predictable, deterministic program)
  16. Referential Transparency The values of expressions do not change. •

    They are context independent. (doesn’t matter when or where) • The same input produces same output. (predictable, deterministic program)
  17. Referential Transparency The values of expressions do not change. •

    They are context independent. (doesn’t matter when or where) • The same input produces same output. (predictable, deterministic program) RT just doesn’t work for imperative programming.
  18. No Side Effects Side effect free functions: • Don’t modify

    state outside of their scope. • Don’t have any observable interaction with their surrounding context.
  19. No Side Effects Side effect free functions: • Don’t modify

    state outside of their scope. • Don’t have any observable interaction with their surrounding context. • Are idempotent.
  20. No Side Effects Side effect free functions: • Don’t modify

    state outside of their scope. • Don’t have any observable interaction with their surrounding context. • Are idempotent. • Are eligible for referential transparency.
  21. No Side Effects Side effect free functions: • Don’t modify

    state outside of their scope. • Don’t have any observable interaction with their surrounding context. • Are idempotent. • Are eligible for referential transparency.
  22. No Side Effects Side effect free functions: • Don’t modify

    state outside of their scope. • Don’t have any observable interaction with their surrounding context. • Are idempotent. • Are eligible for referential transparency.
  23. No Side Effects Side effect free functions: • Don’t modify

    state outside of their scope. • Don’t have any observable interaction with their surrounding context. • Are idempotent. • Are eligible for referential transparency.
  24. Expressive Declarative programming: • Expresses what to do, not how

    to do it. • Is not sequential instructions. (doesn’t describe control flow of execution)
  25. Expressive Declarative programming: • Expresses what to do, not how

    to do it. • Is not sequential instructions. (doesn’t describe control flow of execution) • Achieves separation of concerns between description and implementation.
  26. Expressive Declarative programming: • Expresses what to do, not how

    to do it. • Is not sequential instructions. (doesn’t describe control flow of execution) • Achieves separation of concerns between description and implementation.
  27. Expressive Declarative programming: • Expresses what to do, not how

    to do it. • Is not sequential instructions. (doesn’t describe control flow of execution) • Achieves separation of concerns between description and implementation.
  28. Expressive Declarative programming: • Expresses what to do, not how

    to do it. • Is not sequential instructions. (doesn’t describe control flow of execution) • Achieves separation of concerns between description and implementation.
  29. Expressive Declarative programming: • Expresses what to do, not how

    to do it. • Is not sequential instructions. (doesn’t describe control flow of execution) • Achieves separation of concerns between description and implementation.
  30. Expressive Declarative programming: • Expresses what to do, not how

    to do it. • Is not sequential instructions. (doesn’t describe control flow of execution) • Achieves separation of concerns between description and implementation. Abstractions over evaluation are used to model effects.
  31. Expressive Declarative programming: • Expresses what to do, not how

    to do it. • Is not sequential instructions. (doesn’t describe control flow of execution) • Achieves separation of concerns between description and implementation. Abstractions over evaluation are used to model effects.
  32. Expressive Declarative programming: • Expresses what to do, not how

    to do it. • Is not sequential instructions. (doesn’t describe control flow of execution) • Achieves separation of concerns between description and implementation. Abstractions over evaluation are used to model effects.
  33. Expressive Declarative programming: • Expresses what to do, not how

    to do it. • Is not sequential instructions. (doesn’t describe control flow of execution) • Achieves separation of concerns between description and implementation. Abstractions over evaluation are used to model effects.
  34. Expressive Declarative programming: • Expresses what to do, not how

    to do it. • Is not sequential instructions. (doesn’t describe control flow of execution) • Achieves separation of concerns between description and implementation. Abstractions over evaluation are used to model effects.
  35. Composable Build complexity and remain expressive: • Assemble building blocks

    of data types and functions. • Use a toolbox of core, reusable, and proven tools
  36. Composable Build complexity and remain expressive: • Assemble building blocks

    of data types and functions. • Use a toolbox of core, reusable, and proven tools • Create new computations quickly from existing ones.
  37. Composable Build complexity and remain expressive: • Assemble building blocks

    of data types and functions. • Use a toolbox of core, reusable, and proven tools • Create new computations quickly from existing ones. • Utilize simple components that do one thing well.
  38. Composable Build complexity and remain expressive: • Assemble building blocks

    of data types and functions. • Use a toolbox of core, reusable, and proven tools • Create new computations quickly from existing ones. • Utilize simple components that do one thing well.
  39. Composable Build complexity and remain expressive: • Assemble building blocks

    of data types and functions. • Use a toolbox of core, reusable, and proven tools • Create new computations quickly from existing ones. • Utilize simple components that do one thing well.
  40. Composable Build complexity and remain expressive: • Assemble building blocks

    of data types and functions. • Use a toolbox of core, reusable, and proven tools • Create new computations quickly from existing ones. • Utilize simple components that do one thing well.
  41. Composable Build complexity and remain expressive: • Assemble building blocks

    of data types and functions. • Use a toolbox of core, reusable, and proven tools • Create new computations quickly from existing ones. • Utilize simple components that do one thing well.
  42. Composable Build complexity and remain expressive: • Assemble building blocks

    of data types and functions. • Use a toolbox of core, reusable, and proven tools • Create new computations quickly from existing ones. • Utilize simple components that do one thing well.
  43. Composable Build complexity and remain expressive: • Assemble building blocks

    of data types and functions. • Use a toolbox of core, reusable, and proven tools • Create new computations quickly from existing ones. • Utilize simple components that do one thing well.
  44. Composable Build complexity and remain expressive: • Assemble building blocks

    of data types and functions. • Use a toolbox of core, reusable, and proven tools • Create new computations quickly from existing ones. • Utilize simple components that do one thing well.
  45. Composable Build complexity and remain expressive: • Assemble building blocks

    of data types and functions. • Use a toolbox of core, reusable, and proven tools • Create new computations quickly from existing ones. • Utilize simple components that do one thing well. The goal: not heaps of special cases and tangled conditions, but an expressive domain composed of simple parts.
  46. Modular High cohesion and loose coupling: • Immutable data simplifies

    concurrency. • Functional abstractions separate concerns elegantly.
  47. Modular High cohesion and loose coupling: • Immutable data simplifies

    concurrency. • Functional abstractions separate concerns elegantly. • Required reasoning about code is reduced and localized.
  48. Modular High cohesion and loose coupling: • Immutable data simplifies

    concurrency. • Functional abstractions separate concerns elegantly. • Required reasoning about code is reduced and localized. • Pure functions can be easily reused.
  49. Testable • Does an input equal an expected output? No

    mocking required. • Previously tested “tools” can be used with confidence.
  50. Testable • Does an input equal an expected output? No

    mocking required. • Previously tested “tools” can be used with confidence. • Properties can be verified with property-based testing.
  51. Testable • Does an input equal an expected output? No

    mocking required. • Previously tested “tools” can be used with confidence. • Properties can be verified with property-based testing. • Properties expressed by types are automatically “tested” by the compiler.
  52. Learn More Functional Programming in Scala by Paul Chiusano and

    Rúnar Bjarnason (on Safari) Functional and Reactive Domain Modeling by Debasish Ghosh (Early Access) 4 upcoming on-demand coursera courses from the Scala Center at EPFL