-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy path04_lambda_functions.py
More file actions
340 lines (262 loc) · 10.1 KB
/
04_lambda_functions.py
File metadata and controls
340 lines (262 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
"""
================================================================================
File: 04_lambda_functions.py
Topic: Lambda Functions in Python
================================================================================
This file demonstrates lambda functions (anonymous functions) in Python.
Lambda functions are small, one-line functions that can be defined inline
without using the 'def' keyword.
Key Concepts:
- Lambda syntax
- When to use lambda functions
- Lambda with built-in functions (map, filter, sorted)
- Lambda vs regular functions
- Common use cases and patterns
================================================================================
"""
# -----------------------------------------------------------------------------
# 1. Basic Lambda Syntax
# -----------------------------------------------------------------------------
# lambda arguments: expression
print("--- Basic Lambda Syntax ---")
# Regular function
def add_regular(a, b):
return a + b
# Equivalent lambda function
add_lambda = lambda a, b: a + b
print(f"Regular function: {add_regular(3, 5)}")
print(f"Lambda function: {add_lambda(3, 5)}")
# More examples
square = lambda x: x ** 2
is_even = lambda x: x % 2 == 0
greet = lambda name: f"Hello, {name}!"
print(f"\nsquare(4) = {square(4)}")
print(f"is_even(7) = {is_even(7)}")
print(f"greet('Alice') = {greet('Alice')}")
# Lambda with no arguments
get_pi = lambda: 3.14159
print(f"get_pi() = {get_pi()}")
# -----------------------------------------------------------------------------
# 2. Lambda with Multiple Arguments
# -----------------------------------------------------------------------------
print("\n--- Multiple Arguments ---")
# Two arguments
multiply = lambda x, y: x * y
print(f"multiply(4, 5) = {multiply(4, 5)}")
# Three arguments
volume = lambda l, w, h: l * w * h
print(f"volume(2, 3, 4) = {volume(2, 3, 4)}")
# With default arguments
power = lambda base, exp=2: base ** exp
print(f"power(3) = {power(3)}") # 3^2 = 9
print(f"power(2, 3) = {power(2, 3)}") # 2^3 = 8
# -----------------------------------------------------------------------------
# 3. Lambda with Conditional Expression
# -----------------------------------------------------------------------------
# Using ternary operator in lambda
print("\n--- Conditional Lambda ---")
# Simple conditional
get_sign = lambda x: "positive" if x > 0 else ("negative" if x < 0 else "zero")
print(f"get_sign(5) = {get_sign(5)}")
print(f"get_sign(-3) = {get_sign(-3)}")
print(f"get_sign(0) = {get_sign(0)}")
# Max of two numbers
max_of_two = lambda a, b: a if a > b else b
print(f"\nmax_of_two(10, 20) = {max_of_two(10, 20)}")
# Absolute value
absolute = lambda x: x if x >= 0 else -x
print(f"absolute(-7) = {absolute(-7)}")
# -----------------------------------------------------------------------------
# 4. Lambda with map()
# -----------------------------------------------------------------------------
# Apply function to each element of iterable
print("\n--- Lambda with map() ---")
numbers = [1, 2, 3, 4, 5]
# Square each number
squares = list(map(lambda x: x ** 2, numbers))
print(f"Original: {numbers}")
print(f"Squared: {squares}")
# Convert to strings
strings = list(map(lambda x: str(x), numbers))
print(f"As strings: {strings}")
# Multiple iterables with map
list1 = [1, 2, 3]
list2 = [10, 20, 30]
sums = list(map(lambda x, y: x + y, list1, list2))
print(f"\n{list1} + {list2} = {sums}")
# Processing strings
names = ["alice", "bob", "charlie"]
capitalized = list(map(lambda name: name.capitalize(), names))
print(f"Capitalized: {capitalized}")
# -----------------------------------------------------------------------------
# 5. Lambda with filter()
# -----------------------------------------------------------------------------
# Filter elements based on condition
print("\n--- Lambda with filter() ---")
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Filter even numbers
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(f"Original: {numbers}")
print(f"Even only: {evens}")
# Filter numbers greater than 5
greater_than_5 = list(filter(lambda x: x > 5, numbers))
print(f"Greater than 5: {greater_than_5}")
# Filter non-empty strings
strings = ["hello", "", "world", "", "python", ""]
non_empty = list(filter(lambda s: s, strings))
print(f"\nNon-empty strings: {non_empty}")
# Complex filtering
people = [
{"name": "Alice", "age": 25},
{"name": "Bob", "age": 17},
{"name": "Charlie", "age": 30},
{"name": "David", "age": 15}
]
adults = list(filter(lambda p: p["age"] >= 18, people))
print(f"Adults: {[p['name'] for p in adults]}")
# -----------------------------------------------------------------------------
# 6. Lambda with sorted()
# -----------------------------------------------------------------------------
# Custom sorting with key function
print("\n--- Lambda with sorted() ---")
# Sort by absolute value
numbers = [-5, 2, -1, 7, -3, 4]
by_absolute = sorted(numbers, key=lambda x: abs(x))
print(f"Original: {numbers}")
print(f"Sorted by absolute value: {by_absolute}")
# Sort strings by length
words = ["python", "is", "a", "programming", "language"]
by_length = sorted(words, key=lambda w: len(w))
print(f"\nSorted by length: {by_length}")
# Sort objects by attribute
students = [
{"name": "Alice", "grade": 85},
{"name": "Bob", "grade": 92},
{"name": "Charlie", "grade": 78}
]
by_grade = sorted(students, key=lambda s: s["grade"], reverse=True)
print(f"\nBy grade (highest first):")
for s in by_grade:
print(f" {s['name']}: {s['grade']}")
# Sort by multiple criteria
items = [("apple", 3), ("banana", 1), ("cherry", 2), ("apple", 1)]
# Sort by name, then by number
sorted_items = sorted(items, key=lambda x: (x[0], x[1]))
print(f"\nSorted by name, then number: {sorted_items}")
# -----------------------------------------------------------------------------
# 7. Lambda with reduce()
# -----------------------------------------------------------------------------
# Reduce iterable to single value
print("\n--- Lambda with reduce() ---")
from functools import reduce
numbers = [1, 2, 3, 4, 5]
# Sum all numbers
total = reduce(lambda acc, x: acc + x, numbers)
print(f"Sum of {numbers} = {total}")
# Product of all numbers
product = reduce(lambda acc, x: acc * x, numbers)
print(f"Product of {numbers} = {product}")
# Find maximum
maximum = reduce(lambda a, b: a if a > b else b, numbers)
print(f"Maximum of {numbers} = {maximum}")
# Concatenate strings
words = ["Hello", " ", "World", "!"]
sentence = reduce(lambda a, b: a + b, words)
print(f"Concatenated: '{sentence}'")
# With initial value
numbers = [1, 2, 3]
sum_with_initial = reduce(lambda acc, x: acc + x, numbers, 100)
print(f"\nSum with initial 100: {sum_with_initial}")
# -----------------------------------------------------------------------------
# 8. Lambda in Data Processing
# -----------------------------------------------------------------------------
print("\n--- Data Processing Example ---")
# Sample data
transactions = [
{"id": 1, "type": "credit", "amount": 100},
{"id": 2, "type": "debit", "amount": 50},
{"id": 3, "type": "credit", "amount": 200},
{"id": 4, "type": "debit", "amount": 75},
{"id": 5, "type": "credit", "amount": 150}
]
# Filter credits only
credits = list(filter(lambda t: t["type"] == "credit", transactions))
print(f"Credit transactions: {len(credits)}")
# Extract amounts from credits
credit_amounts = list(map(lambda t: t["amount"], credits))
print(f"Credit amounts: {credit_amounts}")
# Total credits
total_credits = reduce(lambda acc, t: acc + t["amount"], credits, 0)
print(f"Total credits: ${total_credits}")
# Combined: total debits in one expression
total_debits = reduce(
lambda acc, x: acc + x,
map(
lambda t: t["amount"],
filter(lambda t: t["type"] == "debit", transactions)
),
0
)
print(f"Total debits: ${total_debits}")
# -----------------------------------------------------------------------------
# 9. Lambda vs Regular Functions
# -----------------------------------------------------------------------------
print("\n--- Lambda vs Regular Functions ---")
# Lambda: one expression, implicit return
# Regular: multiple statements, explicit return
# When to use LAMBDA:
# - Simple, one-line operations
# - As arguments to higher-order functions
# - When function won't be reused
# When to use REGULAR FUNCTIONS:
# - Multiple expressions/statements needed
# - Need docstrings
# - Function will be reused or tested
# - Complex logic
# Example: Complex logic needs regular function
def process_value(x):
"""Process value with multiple steps."""
# Step 1: Validate
if x < 0:
return None
# Step 2: Transform
result = x ** 2
# Step 3: Apply ceiling
if result > 100:
result = 100
return result
# This CAN'T be easily done with lambda
# lambda x: (None if x < 0 else min(x ** 2, 100)) # Gets messy
print(f"process_value(-5) = {process_value(-5)}")
print(f"process_value(5) = {process_value(5)}")
print(f"process_value(15) = {process_value(15)}")
# -----------------------------------------------------------------------------
# 10. Common Lambda Patterns
# -----------------------------------------------------------------------------
print("\n--- Common Lambda Patterns ---")
# 1. Default value getter
get_value = lambda d, key, default=None: d.get(key, default)
data = {"name": "John", "age": 30}
print(f"get_value: {get_value(data, 'name')}, {get_value(data, 'email', 'N/A')}")
# 2. Compose functions
compose = lambda f, g: lambda x: f(g(x))
add_one = lambda x: x + 1
double = lambda x: x * 2
add_then_double = compose(double, add_one)
print(f"add_then_double(5) = {add_then_double(5)}") # (5+1)*2 = 12
# 3. Partial application simulation
multiply_by = lambda n: lambda x: x * n
times_three = multiply_by(3)
print(f"times_three(7) = {times_three(7)}")
# 4. Key extractors for sorting
by_key = lambda key: lambda x: x[key]
products = [
{"name": "Apple", "price": 1.50},
{"name": "Banana", "price": 0.75},
{"name": "Cherry", "price": 2.00}
]
sorted_by_price = sorted(products, key=by_key("price"))
print(f"\nSorted by price: {[p['name'] for p in sorted_by_price]}")
# 5. Immediate invocation (IIFE-style)
result = (lambda x, y: x ** y)(2, 10)
print(f"\nImmediately invoked: 2^10 = {result}")