From e245e833a6512d4d53b2999e96462c72f3c152c0 Mon Sep 17 00:00:00 2001 From: Phillip Cloud <417981+cpcloud@users.noreply.github.com> Date: Sat, 9 Sep 2023 09:41:07 -0400 Subject: [PATCH] docs: add `functools.partial` and lambda closures to `ArrayValue.map` and `ArrayValue.filter` --- ibis/expr/types/arrays.py | 60 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/ibis/expr/types/arrays.py b/ibis/expr/types/arrays.py index ba02a91634ef..65c99c5ca8d8 100644 --- a/ibis/expr/types/arrays.py +++ b/ibis/expr/types/arrays.py @@ -393,6 +393,36 @@ def map(self, func: Callable[[ir.Value], ir.Value]) -> ir.ArrayValue: │ [104.0] │ │ [] │ └───────────────────────┘ + + `.map()` also supports more complex callables like `functools.partial` + and lambdas with closures + + >>> from functools import partial + >>> def add(x, y): + ... return x + y + ... + >>> add2 = partial(add, y=2) + >>> t.a.map(add2) + ┏━━━━━━━━━━━━━━━━━━━━━━┓ + ┃ ArrayMap(a) ┃ + ┡━━━━━━━━━━━━━━━━━━━━━━┩ + │ array │ + ├──────────────────────┤ + │ [3, None, ... +1] │ + │ [6] │ + │ [] │ + └──────────────────────┘ + >>> y = 2 + >>> t.a.map(lambda x: x + y) + ┏━━━━━━━━━━━━━━━━━━━━━━┓ + ┃ ArrayMap(a) ┃ + ┡━━━━━━━━━━━━━━━━━━━━━━┩ + │ array │ + ├──────────────────────┤ + │ [3, None, ... +1] │ + │ [6] │ + │ [] │ + └──────────────────────┘ """ @functools.wraps(func) @@ -441,6 +471,36 @@ def filter( │ [4] │ │ [] │ └──────────────────────┘ + + `.filter()` also supports more complex callables like `functools.partial` + and lambdas with closures + + >>> from functools import partial + >>> def gt(x, y): + ... return x > y + ... + >>> gt1 = partial(gt, y=1) + >>> t.a.filter(gt1) + ┏━━━━━━━━━━━━━━━━━━━━━━┓ + ┃ ArrayFilter(a) ┃ + ┡━━━━━━━━━━━━━━━━━━━━━━┩ + │ array │ + ├──────────────────────┤ + │ [2] │ + │ [4] │ + │ [] │ + └──────────────────────┘ + >>> y = 1 + >>> t.a.filter(lambda x: x > y) + ┏━━━━━━━━━━━━━━━━━━━━━━┓ + ┃ ArrayFilter(a) ┃ + ┡━━━━━━━━━━━━━━━━━━━━━━┩ + │ array │ + ├──────────────────────┤ + │ [2] │ + │ [4] │ + │ [] │ + └──────────────────────┘ """ @functools.wraps(predicate)