Files
30-seconds-of-code/snippets/when.md
Isabelle Viktoria Maciohsek 19f636408c Make expertise a field
2022-03-01 20:27:27 +02:00

578 B

title, tags, expertise, firstSeen, lastUpdated
title tags expertise firstSeen lastUpdated
Apply function when true function intermediate 2020-01-02T20:25:45+02:00 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() is True for x and if so call when_true(), 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