1) Simple Closure
Write a function outer() that defines a local variable x = 5 and returns an inner function that prints x.
Call the closure.
2) Closure With Parameter
Create outer(a) that returns a function which prints a.
Test with different values.
3) Add Using Closure
Write make_adder(x) that returns a function adding x to its argument.
Test with add5 = make_adder(5) → add5(10).
4) Multiply Using Closure
Write make_multiplier(n) that returns a function to multiply input by n.
Test with different multipliers.
5) Personalized Greeting Closure
Write a function that returns a greeting function based on a given name.
6) Counter Closure
Create make_counter() that returns a function which increments and returns a count starting at 0 each time it’s called.
7) Closure With Default
Use a default inner function to add a fixed tax percentage to an amount.
8) Store Value in Closure
Use a closure to store a secret value and retrieve it via the returned function.
9) Change Outer Variable
Write a closure that changes an outer variable using nonlocal.
10) Closure That Formats Text
Write make_formatter(prefix) that returns a function to print text with that prefix.
11) Closure Returning Boolean
Write a closure that checks if a number is greater than a fixed threshold.
12) Closure to Filter List
Create make_filter(n) that returns a function filtering numbers greater than n from a list.
13) Closure to Accumulate Sum
Write make_accumulator() that returns a function accumulating sum across calls.
14) Closure With Multiple Parameters
Write outer(a,b) that returns an inner function computing a * b plus input.
15) Replace Built-in Behavior
Use closure to modify the behavior of a function — for example, always convert text to uppercase before printing.
16) Delay Execution With Closure
Write a function that returns a closure which prints a message only when called later.
17) Generate Sequence Using Closures
Write create_seq(start) that returns a function generating next number on each call.
18) Powers Using Closure
Write power_of(n) returning a function that raises input to power n.
19) Conditional Rate Calculator
Write a closure that applies different rates (e.g., tax/discount) based on flags.
20) Decorator-like Closure
Create a simple closure that wraps another function to print “Start” and “End” around its execution.
🧠 Key Concepts Practiced
Inner fn defined inside outer fn
Capturing outer variables
Closure remembers outer state
Using closures for custom behavior
adders, multipliers, filters
Adder closure
Counter closure
canvil:
333f6c7d-6876-4f7e-b6ad-1bdb2233f5c1