Pure Functions ka Funda

Understand how Pure Functions works with Fullstackgada

✨ Pure vs Impure Function Tester

Pure Function ke Niyam:
1️⃣ Same input = Same output (hamesha)
2️⃣ Koi side effects nahi (bahar ki duniya ko nahi chhuta)
Upar ke buttons click karo to alag-alag functions test karo!

External State Monitor:

Global Variables:
bonus = 100
counter = 0
lastUser = "none"

🔧 Interactive Function Builder

Apna Function Banaye: Pure aur Impure functions banao aur dekho kaise kaam karte hain!

📊 Function Execution ka History

0
Functions test karne ke liye ready...

💻 Pure vs Impure Code ke Examples

// ✅ Pure Function ke Examples
function add(a, b) {
return a + b; // Same input = Same output
}
function multiply(x, y) {
return x * y; // Koi side effects nahi
}
function getFullName(firstName, lastName) {
return firstName + " " + lastName;
}
// Testing
console.log(add(2, 3)); // Hamesha 5
console.log(add(2, 3)); // Hamesha 5
console.log(add(2, 3)); // Hamesha 5
// ✅ Pure Array Operations
function doubleArray(arr) {
return arr.map(x => x * 2); // Nayi array return karta hai
}
function filterAdults(people) {
return people.filter(person => person.age >= 18);
}
// Original arrays wahi rehte hain
let nums = [1, 2, 3];
let doubled = doubleArray(nums);
console.log(nums); // [1, 2, 3] (badla nahi)
console.log(doubled); // [2, 4, 6] (nayi array)
// ❌ Impure Function ke Examples
let bonus = 100; // Global variable
function calculateSalaryWithBonus(base) {
return base + bonus; // Bahar ke state par depend karta hai
}
// Same input, alag output
console.log(calculateSalaryWithBonus(1000)); // 1100
bonus = 200; // Bahar se badla
console.log(calculateSalaryWithBonus(1000)); // 1200 (Alag!)
// ❌ Side Effects wale Functions
let counter = 0;
function incrementAndAdd(a, b) {
counter++; // Side effect: global state badalta hai
console.log("Adding..."); // Side effect: logging
return a + b;
}
function saveToDatabase(data) {
database.save(data); // Side effect: bahar ke change
return "Saved successfully";
}
// ✅ Pure Function se Pure Function Call
function square(x) {
return x * x; // Pure
}
function add(a, b) {
return a + b; // Pure
}
function sumOfSquares(a, b) {
return add(square(a), square(b)); // Pure se Pure = Pure!
}
// Predictable results
console.log(sumOfSquares(3, 4)); // Hamesha 25
console.log(sumOfSquares(3, 4)); // Hamesha 25
// ❌ Pure Function se Impure Function Call
let randomSeed = 42;
function getRandomNumber() {
return Math.random(); // Impure: har baar alag output
}
function addRandomToNumber(num) {
return num + getRandomNumber(); // Ab yeh bhi Impure!
}
// Unpredictable results
console.log(addRandomToNumber(10)); // 10.234...
console.log(addRandomToNumber(10)); // 10.876... (Alag!)
// ⚠️ Common Galti #1: Chhupi Dependencies
const TAX_RATE = 0.18;
function calculateTax(amount) {
return amount * TAX_RATE; // Pure lagta hai par external constant par depend karta hai
}
// ✅ Better: Sab kuch parameters mein do
function calculateTaxPure(amount, taxRate) {
return amount * taxRate;
}
// ⚠️ Common Galti #2: Array Mutation
function addToArray(arr, item) {
arr.push(item); // Original array badalta hai!
return arr;
}
// ✅ Better: Nayi array return karo
function addToArrayPure(arr, item) {
return [...arr, item]; // Nayi array banata hai
}

🎯 Predictability Test: Same Input, Same Output?

✅ Pure Function Test

Pure function ka predictability test karne ke liye click karo...

❌ Impure Function Test

Impure function ka predictability test karne ke liye click karo...

🎯 Pure Functions: Fayde aur Testing

Aspect ✅ Pure Functions ❌ Impure Functions
Predictability Same input = Same output (hamesha) Same input ≠ Same output (kabhi kabhi)
Testing Aasaan testing - koi mocking ki zarurat nahi Mushkil testing - external dependencies ko mock karna padta hai
Debugging Aasaan debugging - isolated logic Mushkil debugging - side effects har jagah
Caching Memoization possible (result caching) Cache nahi kar sakte - results alag ho sakte hain
Parallelization Thread-safe, parallel execution safe Race conditions, synchronization ke issues
Code Reuse Zyada reusability - kahin bhi use karo Kam reusability - context par depend karta hai
Jethalal ki Misal Jaise calculator - 2+3 hamesha 5 Jaise weather - aaj dhoop, kal baarish