-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_playground.py
More file actions
39 lines (27 loc) · 926 Bytes
/
Copy pathlambda_playground.py
File metadata and controls
39 lines (27 loc) · 926 Bytes
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
"""Większy przykład: composable lambdy na danych liczbowych."""
def compose(f, g):
return lambda x: f(g(x))
def make_pipeline(*funcs):
"""Składa wiele funkcji w jedną pipeline'ową funkcję."""
if not funcs:
return lambda x: x
def pipeline(x):
for f in funcs:
x = f(x)
return x
return pipeline
if __name__ == "__main__":
# Pojedyncze lambdy
add1 = lambda x: x + 1
square = lambda x: x * x
half = lambda x: x / 2
# Kompozycja f(g(x))
h = compose(square, add1) # (x+1)^2
print("compose square(add1(x)) dla x=3 ->", h(3))
# Pipeline wielu kroków
p = make_pipeline(add1, square, half)
print("pipeline add1 -> square -> half dla x=3 ->", p(3))
dane = [1, 2, 3, 4, 5]
transform = make_pipeline(lambda x: x * 3, lambda x: x - 1)
wynik = [transform(x) for x in dane]
print("transformacja listy:", wynik)