570 B
570 B
title, type, language, tags, cover, dateModified
| title | type | language | tags | cover | dateModified | |
|---|---|---|---|---|---|---|
| Apply function when true | snippet | python |
|
interior-10 | 2020-11-09T23:56:11+02:00 |
Tests a value, x, against a testing function, conditionally applying a function.
- Check if the value of
predicate()isTrueforxand if so callwhen_true(), otherwise returnx.
def when(predicate, when_true):
return lambda x: when_true(x) if predicate(x) else x
double_even_numbers = when(lambda x: x % 2 == 0, lambda x : x * 2)
double_even_numbers(2) # 4
double_even_numbers(1) # 1