From 1daa5e9e85711cc1ef96f161b74b96dc1f2d4034 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 2 Jan 2020 16:49:25 +0200 Subject: [PATCH] Add check prop --- snippets/check_prop.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 snippets/check_prop.md diff --git a/snippets/check_prop.md b/snippets/check_prop.md new file mode 100644 index 000000000..cb98054bf --- /dev/null +++ b/snippets/check_prop.md @@ -0,0 +1,23 @@ +--- +title: function_name +tags: utility,intermediate +--- + +Given a predicate function, `fn`, and a `prop` string, this curried function will then take an object to inspect by calling the property and passing it to the predicate. + +Return a `lambda` function that takes an object and applies the predicate function, `fn` to the specified property. + +```py +def check_prop(fn, prop): + return lambda obj: fn(obj[prop]) +``` + +```py +check_age = check_prop(lambda x: x >= 18, 'age') +user = { + 'name': 'Mark', + 'age': 18 +} + +check_age(user) # True +```