0% found this document useful (0 votes)
8 views11 pages

15 Functional Programming

Functional programming is a paradigm emphasizing pure functions, immutability, and function composition while avoiding shared state and side effects. Key concepts include pure functions, immutability, declarative code, and loose coupling, which promote code reusability and independence. The document also poses questions related to practical applications of these concepts in programming.

Uploaded by

ummeaeman.com
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views11 pages

15 Functional Programming

Functional programming is a paradigm emphasizing pure functions, immutability, and function composition while avoiding shared state and side effects. Key concepts include pure functions, immutability, declarative code, and loose coupling, which promote code reusability and independence. The document also poses questions related to practical applications of these concepts in programming.

Uploaded by

ummeaeman.com
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Functional programming

Functional Programming is a programming paradigm where functions are first-class citizens, and
the focus is on pure functions, immutability, and function composition rather than shared state and
side-effects.

1. Pure Functions 6. Reuse or Compose Logic

2. Immutability 7. Don’t Iterate

3. Declarative 8. Loose coupling

4. Avoid Shared state 9. First-Class & Higher-Order Functions

5. Avoid Side Effects


1. Pure Functions
A function is pure if:
❑ It returns the same output for the same input.
❑ It doesn’t cause side effects (like modifying external variables or DOM).
2. Immutability
Do not modify existing data. Instead, return new copies.
3. Declarative Code
Describe what should be done, not how.
4. Avoid Shared State
Shared mutable state can lead to bugs, especially in async or parallel systems.
5. Avoid Side Effect
Side effects are anything a function does outside its scope
(API call, DOM update, modifying global vars).
6. Reuse Or Compose Logic
Build small reusable functions and compose them together.
7. Don’t Iterate (Imperatively)
Avoid for, while, etc. Use map, filter, reduce.
8. Loose Coupling
Coupling refers to how dependent one piece of code is on another.
Loose coupling means less dependent, Keep functions and modules independent.
9. First-Class & Higher-Order Functions
❑ First-Class: Functions can be stored in variables, passed, and returned.
❑ Higher-Order: Functions that take other functions as arguments or return them.

Note:

❑ All callbacks are first-class functions, but not all first-


class functions are callbacks.

❑ All higher order functions are first class functions but


not all first-class functions are higher order functions
1. What is a pure function, and why is it useful in UI rendering?

2. How would you use .map() to transform a list of products into a list of HTML elements?

3. How do you use .reduce() to calculate the total price in a shopping cart?

4. Explain immutability and how you would update an object in an array without mutating the original.

5. How would you compose multiple functions to transform data step-by-step (e.g., sanitize → trim →
capitalize)? Scenario: You’re preparing user input before storing it. (Expected knowledge: Function
composition, chaining, pipe or compose logic.)

6. What is the difference between forEach and map, and when is it wrong to use map?

7. How do you implement your own version of .map() function on arrays?

You might also like