-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lambdas_Open_Closed.py
32 lines (23 loc) · 1014 Bytes
/
Lambdas_Open_Closed.py
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
'''
Open/Closed Principle
The open/closed principle states that code should be closed for modification,
yet open for extension.
That means you should be able to add new functionality
to an object or method without altering it.
One way to achieve this is by using a lambda,
which by nature is lazily bound to the lexical context.
Until you call a lambda, it is just a piece of data you can pass around.
Task at hand
Implement 3 lambdas that alter a message based on emotoion:
spoken = lambda greeting: ??? # "Hello."
shouted = lambda greeting: ??? # "HELLO!"
whispered = lambda greeting: ??? # "hello."
Then create a fourth lambda, this one will take one of the above
lambdas and a message, and the last lambda will delegate
the emotion and the message up the chain.
greet = lambda style, msg: ???
'''
spoken = lambda greeting: greeting.capitalize()+'.'
shouted = lambda greeting: greeting.upper() + '!'
whispered = lambda greeting: greeting.lower() + '.'
greet = lambda style, msg: style(msg)