Menu

gitpiper

when python Code Snippet in 2024

functionintermediate

Last Updated: 20 April 2024

Tests a value, x, against a testing function, conditionally applying a function.

  • Check if the value of predicate(x) is True and if so return when_true(x), otherwise return x.
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

python snippet similar to when For You in April 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️