Add a function `replace()`

This commit is contained in:
Emi Simpson 2023-02-10 08:32:25 -05:00
parent b13531aecf
commit 6577717cbf
Signed by: Emi
GPG Key ID: A12F2C2FFDC3D847
1 changed files with 13 additions and 0 deletions

View File

@ -48,6 +48,19 @@ def ident(x: A) -> A:
"The identity function. Output is identical to input."
return x
def replace(replace_with: A) -> Callable[..., A]:
"""
Get a function which always returns a constant value, regardless of input
The argument `replace_with` is the value the the returned function should always
return. The returned function can be used as if having any arity, and will always
return the same value originally passed to `replace`.
"""
def constant(*args: Any, **kwargs: Any) -> A:
"Always return a constant value, typically the one passed to `replace`"
return replace_with
return constant
# Partial Appliaction shorthand
p = partial
"An alias for partial application"