Functional Module¶
The rite.functional module provides functional programming utilities including composition, currying, and decorators.
Overview¶
functional ¶
Functional Programming Module¶
Comprehensive functional programming utilities.
This module provides utilities for decorators, function composition, currying, partial application, memoization, and predicates.
Submodules¶
- decorators: Debounce, throttle, once, deprecated decorators
- composition: Function composition, piping, chaining
- currying: Curry and uncurry functions
- partial: Partial application from left and right
- memoization: Result caching with various strategies
- predicates: Identity, constant, negate predicates
Examples¶
Decorators: >>> from rite.functional import decorators_throttle >>> @decorators_throttle(1.0) ... def api_call(): ... return "response"
Composition
from rite.functional import composition_pipe f = composition_pipe(lambda x: x + 1, lambda x: x * 2) f(3) 8
Memoization
from rite.functional import memoization_memoize @memoization_memoize() ... def fibonacci(n): ... if n <= 1: ... return n ... return fibonacci(n-1) + fibonacci(n-2)
Modules¶
composition ¶
Composition Module¶
Function composition and piping utilities.
This submodule provides utilities for composing functions, piping values through functions, and chainable operations.
Examples¶
from rite.functional.composition import ( ... composition_compose, ... composition_pipe, ... composition_chain ... ) f = composition_pipe(lambda x: x + 1, lambda x: x * 2) f(3) 8
Modules¶
composition_chain ¶
Function Chain¶
Chain method calls fluently.
Examples¶
from rite.functional.composition import composition_chain result = composition_chain(3).pipe( ... lambda x: x + 1, ... lambda x: x * 2 ... ).value() result 8
Fluent chainable function application.
Attributes:
| Name | Type | Description |
|---|---|---|
_value |
Current value in chain. |
Examples:
>>> chain = composition_chain(5)
>>> result = chain.pipe(lambda x: x * 2).pipe(
... lambda x: x + 1
... ).value()
>>> result
11
Initialize chain with value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Initial value. |
required |
Apply functions to current value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*functions
|
Callable[[Any], Any]
|
Functions to apply. |
()
|
Returns:
| Type | Description |
|---|---|
composition_chain
|
Self for chaining. |
Examples:
composition_compose ¶
Function Composition¶
Compose multiple functions into one.
Examples¶
from rite.functional.composition import composition_compose add_one = lambda x: x + 1 double = lambda x: x * 2 f = composition_compose(double, add_one) f(3) 8
Compose functions right to left.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*functions
|
Callable[[Any], Any]
|
Functions to compose. |
()
|
Returns:
| Type | Description |
|---|---|
Callable[[Any], Any]
|
Composed function. |
Examples:
>>> inc = lambda x: x + 1
>>> double = lambda x: x * 2
>>> f = composition_compose(inc, double)
>>> f(3) # inc(double(3)) = inc(6) = 7
7
Notes
Functions are applied right to left: f(g(x)). For left to right, use composition_pipe.
composition_pipe ¶
Function Pipe¶
Pipe value through functions left to right.
Examples¶
from rite.functional.composition import composition_pipe add_one = lambda x: x + 1 double = lambda x: x * 2 f = composition_pipe(add_one, double) f(3) 8
Pipe value through functions left to right.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*functions
|
Callable[[Any], Any]
|
Functions to pipe through. |
()
|
Returns:
| Type | Description |
|---|---|
Callable[[Any], Any]
|
Piped function. |
Examples:
>>> inc = lambda x: x + 1
>>> double = lambda x: x * 2
>>> f = composition_pipe(inc, double)
>>> f(3) # double(inc(3)) = double(4) = 8
8
Notes
Functions are applied left to right: h = g ∘ f. For right to left, use composition_compose.
composition_chain ¶
Function Chain¶
Chain method calls fluently.
Examples¶
from rite.functional.composition import composition_chain result = composition_chain(3).pipe( ... lambda x: x + 1, ... lambda x: x * 2 ... ).value() result 8
Classes¶
composition_chain ¶
Fluent chainable function application.
Attributes:
| Name | Type | Description |
|---|---|---|
_value |
Current value in chain. |
Examples:
>>> chain = composition_chain(5)
>>> result = chain.pipe(lambda x: x * 2).pipe(
... lambda x: x + 1
... ).value()
>>> result
11
Initialize chain with value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Initial value. |
required |
Apply functions to current value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*functions
|
Callable[[Any], Any]
|
Functions to apply. |
()
|
Returns:
| Type | Description |
|---|---|
composition_chain
|
Self for chaining. |
Examples:
composition_compose ¶
Function Composition¶
Compose multiple functions into one.
Examples¶
from rite.functional.composition import composition_compose add_one = lambda x: x + 1 double = lambda x: x * 2 f = composition_compose(double, add_one) f(3) 8
Functions¶
composition_compose ¶
Compose functions right to left.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*functions
|
Callable[[Any], Any]
|
Functions to compose. |
()
|
Returns:
| Type | Description |
|---|---|
Callable[[Any], Any]
|
Composed function. |
Examples:
>>> inc = lambda x: x + 1
>>> double = lambda x: x * 2
>>> f = composition_compose(inc, double)
>>> f(3) # inc(double(3)) = inc(6) = 7
7
Notes
Functions are applied right to left: f(g(x)). For left to right, use composition_pipe.
composition_pipe ¶
Function Pipe¶
Pipe value through functions left to right.
Examples¶
from rite.functional.composition import composition_pipe add_one = lambda x: x + 1 double = lambda x: x * 2 f = composition_pipe(add_one, double) f(3) 8
Functions¶
composition_pipe ¶
Pipe value through functions left to right.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*functions
|
Callable[[Any], Any]
|
Functions to pipe through. |
()
|
Returns:
| Type | Description |
|---|---|
Callable[[Any], Any]
|
Piped function. |
Examples:
>>> inc = lambda x: x + 1
>>> double = lambda x: x * 2
>>> f = composition_pipe(inc, double)
>>> f(3) # double(inc(3)) = double(4) = 8
8
Notes
Functions are applied left to right: h = g ∘ f. For right to left, use composition_compose.
currying ¶
Currying Module¶
Function currying and uncurrying utilities.
This submodule provides utilities for transforming functions between curried and uncurried forms.
Examples¶
from rite.functional.currying import ( ... currying_curry, ... currying_uncurry ... ) def add(a, b, c): ... return a + b + c curried = currying_curry(add) curried(1)(2)(3) 6
Modules¶
currying_curry ¶
Function Currying¶
Transform multi-argument function into chain of single-argument functions.
Examples¶
from rite.functional.currying import currying_curry def add(a, b, c): ... return a + b + c curried = currying_curry(add) curried(1)(2)(3) 6
Curry a function.
Transforms f(a, b, c) into f(a)(b)©.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., T]
|
Function to curry. |
required |
Returns:
| Type | Description |
|---|---|
Callable[..., T | Callable[..., Any]]
|
Curried function. |
Examples:
>>> def multiply(x, y, z):
... return x * y * z
>>> curried = currying_curry(multiply)
>>> curried(2)(3)(4)
24
>>> partial = curried(2)(3)
>>> partial(4)
24
Notes
Automatically detects function arity from signature.
currying_uncurry ¶
Function Uncurrying¶
Transform curried function back to multi-argument form.
Examples¶
from rite.functional.currying import currying_uncurry curried = lambda a: lambda b: lambda c: a + b + c uncurried = currying_uncurry(curried, 3) uncurried(1, 2, 3) 6
Uncurry a curried function.
Transforms f(a)(b)© back to f(a, b, c).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Any]
|
Curried function. |
required |
arity
|
int
|
Number of arguments. |
required |
Returns:
| Type | Description |
|---|---|
Callable[..., Any]
|
Uncurried function. |
Examples:
>>> curried = lambda x: lambda y: x + y
>>> uncurried = currying_uncurry(curried, 2)
>>> uncurried(5, 3)
8
Notes
Arity must be specified as it cannot be inferred.
currying_curry ¶
Function Currying¶
Transform multi-argument function into chain of single-argument functions.
Examples¶
from rite.functional.currying import currying_curry def add(a, b, c): ... return a + b + c curried = currying_curry(add) curried(1)(2)(3) 6
Functions¶
currying_curry ¶
Curry a function.
Transforms f(a, b, c) into f(a)(b)©.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., T]
|
Function to curry. |
required |
Returns:
| Type | Description |
|---|---|
Callable[..., T | Callable[..., Any]]
|
Curried function. |
Examples:
>>> def multiply(x, y, z):
... return x * y * z
>>> curried = currying_curry(multiply)
>>> curried(2)(3)(4)
24
>>> partial = curried(2)(3)
>>> partial(4)
24
Notes
Automatically detects function arity from signature.
currying_uncurry ¶
Function Uncurrying¶
Transform curried function back to multi-argument form.
Examples¶
from rite.functional.currying import currying_uncurry curried = lambda a: lambda b: lambda c: a + b + c uncurried = currying_uncurry(curried, 3) uncurried(1, 2, 3) 6
Functions¶
currying_uncurry ¶
Uncurry a curried function.
Transforms f(a)(b)© back to f(a, b, c).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Any]
|
Curried function. |
required |
arity
|
int
|
Number of arguments. |
required |
Returns:
| Type | Description |
|---|---|
Callable[..., Any]
|
Uncurried function. |
Examples:
>>> curried = lambda x: lambda y: x + y
>>> uncurried = currying_uncurry(curried, 2)
>>> uncurried(5, 3)
8
Notes
Arity must be specified as it cannot be inferred.
decorators ¶
Decorators Module¶
Function decorators for control flow and behavior.
This submodule provides decorators for debouncing, throttling, one-time execution, and deprecation warnings.
Examples¶
from rite.functional.decorators import ( ... decorators_debounce, ... decorators_throttle, ... decorators_once ... ) @decorators_once() ... def initialize(): ... return "ready"
Modules¶
decorators_debounce ¶
Debounce Decorator¶
Delays function execution by specified wait time.
Examples¶
from rite.functional.decorators import decorators_debounce @decorators_debounce(0.5) ... def process(): ... return "done"
Decorator to debounce function call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wait_time
|
float
|
Time to wait in seconds before execution. |
required |
Returns:
| Type | Description |
|---|---|
Callable[[Callable[..., T]], Callable[..., T]]
|
Decorated function that delays execution. |
Examples:
Notes
This implementation waits before each execution. For event-driven debouncing, use throttle pattern.
decorators_deprecated ¶
Deprecated Decorator¶
Mark functions as deprecated with warnings.
Examples¶
from rite.functional.decorators import decorators_deprecated @decorators_deprecated("Use new_function instead") ... def old_function(): ... pass
decorators_deprecated(message: str = 'This function is deprecated') -> Callable[[Callable[..., T]], Callable[..., T]]
Decorator to mark function as deprecated.
Issues DeprecationWarning when function is called.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Deprecation message to show. |
'This function is deprecated'
|
Returns:
| Type | Description |
|---|---|
Callable[[Callable[..., T]], Callable[..., T]]
|
Decorated function that shows warning. |
Examples:
>>> @decorators_deprecated("Use process_v2() instead")
... def process_v1():
... return "old"
>>> process_v1()
'old'
Notes
Warnings can be controlled with warnings module.
decorators_once ¶
Once Decorator¶
Ensures function is called only once.
Examples¶
from rite.functional.decorators import decorators_once @decorators_once() ... def initialize(): ... return "initialized"
Decorator to execute function only once.
Subsequent calls return cached result.
Returns:
| Type | Description |
|---|---|
Callable[[Callable[..., T]], Callable[..., T]]
|
Decorated function that runs once. |
Examples:
>>> @decorators_once()
... def setup():
... return "configured"
>>> setup()
'configured'
>>> setup() # Returns cached result
'configured'
Notes
Result is cached regardless of arguments. For argument-sensitive caching, use memoization.
decorators_throttle ¶
Throttle Decorator¶
Limits function execution frequency.
Examples¶
from rite.functional.decorators import decorators_throttle @decorators_throttle(1.0) ... def api_call(): ... return "response"
Decorator to throttle function calls.
Ensures function is not called more frequently than interval.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interval
|
float
|
Minimum time between calls in seconds. |
required |
Returns:
| Type | Description |
|---|---|
Callable[[Callable[..., T]], Callable[..., T | None]]
|
Decorated function with throttling. |
Examples:
>>> @decorators_throttle(1.0)
... def process():
... return "done"
>>> process() # First call executes immediately
'done'
>>> process() # Subsequent calls within 1s are skipped
Notes
Returns None if called within throttle interval.
decorators_debounce ¶
Debounce Decorator¶
Delays function execution by specified wait time.
Examples¶
from rite.functional.decorators import decorators_debounce @decorators_debounce(0.5) ... def process(): ... return "done"
Functions¶
decorators_debounce ¶
Decorator to debounce function call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wait_time
|
float
|
Time to wait in seconds before execution. |
required |
Returns:
| Type | Description |
|---|---|
Callable[[Callable[..., T]], Callable[..., T]]
|
Decorated function that delays execution. |
Examples:
Notes
This implementation waits before each execution. For event-driven debouncing, use throttle pattern.
decorators_deprecated ¶
Deprecated Decorator¶
Mark functions as deprecated with warnings.
Examples¶
from rite.functional.decorators import decorators_deprecated @decorators_deprecated("Use new_function instead") ... def old_function(): ... pass
Functions¶
decorators_deprecated ¶
decorators_deprecated(message: str = 'This function is deprecated') -> Callable[[Callable[..., T]], Callable[..., T]]
Decorator to mark function as deprecated.
Issues DeprecationWarning when function is called.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Deprecation message to show. |
'This function is deprecated'
|
Returns:
| Type | Description |
|---|---|
Callable[[Callable[..., T]], Callable[..., T]]
|
Decorated function that shows warning. |
Examples:
>>> @decorators_deprecated("Use process_v2() instead")
... def process_v1():
... return "old"
>>> process_v1()
'old'
Notes
Warnings can be controlled with warnings module.
decorators_once ¶
Once Decorator¶
Ensures function is called only once.
Examples¶
from rite.functional.decorators import decorators_once @decorators_once() ... def initialize(): ... return "initialized"
Functions¶
decorators_once ¶
Decorator to execute function only once.
Subsequent calls return cached result.
Returns:
| Type | Description |
|---|---|
Callable[[Callable[..., T]], Callable[..., T]]
|
Decorated function that runs once. |
Examples:
>>> @decorators_once()
... def setup():
... return "configured"
>>> setup()
'configured'
>>> setup() # Returns cached result
'configured'
Notes
Result is cached regardless of arguments. For argument-sensitive caching, use memoization.
decorators_throttle ¶
Throttle Decorator¶
Limits function execution frequency.
Examples¶
from rite.functional.decorators import decorators_throttle @decorators_throttle(1.0) ... def api_call(): ... return "response"
Functions¶
decorators_throttle ¶
Decorator to throttle function calls.
Ensures function is not called more frequently than interval.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interval
|
float
|
Minimum time between calls in seconds. |
required |
Returns:
| Type | Description |
|---|---|
Callable[[Callable[..., T]], Callable[..., T | None]]
|
Decorated function with throttling. |
Examples:
>>> @decorators_throttle(1.0)
... def process():
... return "done"
>>> process() # First call executes immediately
'done'
>>> process() # Subsequent calls within 1s are skipped
Notes
Returns None if called within throttle interval.
memoization ¶
Memoization Module¶
Function result caching utilities.
This submodule provides decorators for memoizing function results with various caching strategies.
Examples¶
from rite.functional.memoization import ( ... memoization_memoize, ... memoization_lru_cache ... ) @memoization_memoize() ... def fibonacci(n): ... if n <= 1: ... return n ... return fibonacci(n-1) + fibonacci(n-2)
Modules¶
memoization_lru_cache ¶
LRU Cache¶
Least Recently Used cache with size limit.
Examples¶
from rite.functional.memoization import memoization_lru_cache @memoization_lru_cache(maxsize=128) ... def compute(n): ... return n * n
LRU cache decorator with size limit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
maxsize
|
int | None
|
Maximum cache size (None for unlimited). |
128
|
Returns:
| Type | Description |
|---|---|
Callable[[Callable[..., T]], Callable[..., T]]
|
Decorated function with LRU caching. |
Examples:
>>> @memoization_lru_cache(maxsize=100)
... def factorial(n):
... if n <= 1:
... return 1
... return n * factorial(n - 1)
>>> factorial(5)
120
Notes
Uses functools.lru_cache internally. Set maxsize=None for unlimited cache.
memoization_memoize ¶
Simple Memoization¶
Cache function results based on arguments.
Examples¶
from rite.functional.memoization import memoization_memoize @memoization_memoize() ... def fibonacci(n): ... if n <= 1: ... return n ... return fibonacci(n-1) + fibonacci(n-2)
Decorator to memoize function results.
Caches results based on arguments.
Returns:
| Type | Description |
|---|---|
Callable[[Callable[..., T]], Callable[..., T]]
|
Decorated function with caching. |
Examples:
>>> @memoization_memoize()
... def expensive_computation(n):
... return sum(range(n))
>>> expensive_computation(100)
4950
>>> expensive_computation(100) # Returns cached result
4950
Notes
Only works with hashable arguments. For unhashable args, use custom cache key.
memoization_lru_cache ¶
LRU Cache¶
Least Recently Used cache with size limit.
Examples¶
from rite.functional.memoization import memoization_lru_cache @memoization_lru_cache(maxsize=128) ... def compute(n): ... return n * n
Functions¶
memoization_lru_cache ¶
LRU cache decorator with size limit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
maxsize
|
int | None
|
Maximum cache size (None for unlimited). |
128
|
Returns:
| Type | Description |
|---|---|
Callable[[Callable[..., T]], Callable[..., T]]
|
Decorated function with LRU caching. |
Examples:
>>> @memoization_lru_cache(maxsize=100)
... def factorial(n):
... if n <= 1:
... return 1
... return n * factorial(n - 1)
>>> factorial(5)
120
Notes
Uses functools.lru_cache internally. Set maxsize=None for unlimited cache.
memoization_memoize ¶
Simple Memoization¶
Cache function results based on arguments.
Examples¶
from rite.functional.memoization import memoization_memoize @memoization_memoize() ... def fibonacci(n): ... if n <= 1: ... return n ... return fibonacci(n-1) + fibonacci(n-2)
Functions¶
memoization_memoize ¶
Decorator to memoize function results.
Caches results based on arguments.
Returns:
| Type | Description |
|---|---|
Callable[[Callable[..., T]], Callable[..., T]]
|
Decorated function with caching. |
Examples:
>>> @memoization_memoize()
... def expensive_computation(n):
... return sum(range(n))
>>> expensive_computation(100)
4950
>>> expensive_computation(100) # Returns cached result
4950
Notes
Only works with hashable arguments. For unhashable args, use custom cache key.
partial ¶
Partial Module¶
Partial function application utilities.
This submodule provides utilities for partial application of function arguments from left or right.
Examples¶
from rite.functional.partial import ( ... partial_apply, ... partial_right ... ) def power(base, exp): ... return base ** exp square = partial_apply(power, exponent=2) square(5) 25
Modules¶
partial_apply ¶
Partial Application¶
Partially apply function arguments.
Examples¶
from rite.functional.partial import partial_apply def greet(greeting, name): ... return f"{greeting}, {name}!" say_hello = partial_apply(greet, "Hello") say_hello("Alice") 'Hello, Alice!'
Partially apply function arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., T]
|
Function to partially apply. |
required |
*args
|
Any
|
Positional arguments to fix. |
()
|
**kwargs
|
Any
|
Keyword arguments to fix. |
{}
|
Returns:
| Type | Description |
|---|---|
Callable[..., T]
|
Partially applied function. |
Examples:
>>> def multiply(x, y, z):
... return x * y * z
>>> double = partial_apply(multiply, 2)
>>> double(3, 4)
24
>>> def power(base, exponent):
... return base ** exponent
>>> square = partial_apply(power, exponent=2)
>>> square(5)
25
Notes
Uses functools.partial internally.
partial_right ¶
Partial Right¶
Partially apply arguments from the right.
Examples¶
from rite.functional.partial import partial_right def divide(x, y): ... return x / y halve = partial_right(divide, 2) halve(10) 5.0
Partially apply arguments from the right.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., T]
|
Function to partially apply. |
required |
*fixed_args
|
Any
|
Arguments to fix from right. |
()
|
Returns:
| Type | Description |
|---|---|
Callable[..., T]
|
Partially applied function. |
Examples:
>>> def subtract(x, y, z):
... return x - y - z
>>> f = partial_right(subtract, 5, 2)
>>> f(10) # 10 - 5 - 2
3
Notes
Fixed arguments are appended to the right.
partial_apply ¶
Partial Application¶
Partially apply function arguments.
Examples¶
from rite.functional.partial import partial_apply def greet(greeting, name): ... return f"{greeting}, {name}!" say_hello = partial_apply(greet, "Hello") say_hello("Alice") 'Hello, Alice!'
Functions¶
partial_apply ¶
Partially apply function arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., T]
|
Function to partially apply. |
required |
*args
|
Any
|
Positional arguments to fix. |
()
|
**kwargs
|
Any
|
Keyword arguments to fix. |
{}
|
Returns:
| Type | Description |
|---|---|
Callable[..., T]
|
Partially applied function. |
Examples:
>>> def multiply(x, y, z):
... return x * y * z
>>> double = partial_apply(multiply, 2)
>>> double(3, 4)
24
>>> def power(base, exponent):
... return base ** exponent
>>> square = partial_apply(power, exponent=2)
>>> square(5)
25
Notes
Uses functools.partial internally.
partial_right ¶
Partial Right¶
Partially apply arguments from the right.
Examples¶
from rite.functional.partial import partial_right def divide(x, y): ... return x / y halve = partial_right(divide, 2) halve(10) 5.0
Functions¶
partial_right ¶
Partially apply arguments from the right.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., T]
|
Function to partially apply. |
required |
*fixed_args
|
Any
|
Arguments to fix from right. |
()
|
Returns:
| Type | Description |
|---|---|
Callable[..., T]
|
Partially applied function. |
Examples:
>>> def subtract(x, y, z):
... return x - y - z
>>> f = partial_right(subtract, 5, 2)
>>> f(10) # 10 - 5 - 2
3
Notes
Fixed arguments are appended to the right.
predicates ¶
Predicates Module¶
Predicate and utility functions.
This submodule provides basic predicates and utility functions for functional programming patterns.
Examples¶
from rite.functional.predicates import ( ... predicates_identity, ... predicates_constant, ... predicates_negate ... ) predicates_identity(42) 42
Modules¶
predicates_constant ¶
Constant Function¶
Returns constant value regardless of input.
Examples¶
from rite.functional.predicates import predicates_constant always_42 = predicates_constant(42) always_42("anything") 42
predicates_identity ¶
predicates_negate ¶
Negate Predicate¶
Negate boolean result of predicate function.
Examples¶
from rite.functional.predicates import predicates_negate is_even = lambda x: x % 2 == 0 is_odd = predicates_negate(is_even) is_odd(3) True
Negate predicate function result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[..., bool]
|
Function returning boolean. |
required |
Returns:
| Type | Description |
|---|---|
Callable[..., bool]
|
Negated predicate function. |
Examples:
>>> is_positive = lambda x: x > 0
>>> is_not_positive = predicates_negate(is_positive)
>>> is_not_positive(-5)
True
>>> is_not_positive(5)
False
Notes
Equivalent to: lambda *args: not predicate(*args).
predicates_constant ¶
Constant Function¶
Returns constant value regardless of input.
Examples¶
from rite.functional.predicates import predicates_constant always_42 = predicates_constant(42) always_42("anything") 42
predicates_identity ¶
predicates_negate ¶
Negate Predicate¶
Negate boolean result of predicate function.
Examples¶
from rite.functional.predicates import predicates_negate is_even = lambda x: x % 2 == 0 is_odd = predicates_negate(is_even) is_odd(3) True
Functions¶
predicates_negate ¶
Negate predicate function result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[..., bool]
|
Function returning boolean. |
required |
Returns:
| Type | Description |
|---|---|
Callable[..., bool]
|
Negated predicate function. |
Examples:
>>> is_positive = lambda x: x > 0
>>> is_not_positive = predicates_negate(is_positive)
>>> is_not_positive(-5)
True
>>> is_not_positive(5)
False
Notes
Equivalent to: lambda *args: not predicate(*args).
Submodules¶
Composition¶
Function composition utilities.
Composition Module¶
Function composition and piping utilities.
This submodule provides utilities for composing functions, piping values through functions, and chainable operations.
Examples¶
from rite.functional.composition import ( ... composition_compose, ... composition_pipe, ... composition_chain ... ) f = composition_pipe(lambda x: x + 1, lambda x: x * 2) f(3) 8
Modules¶
composition_compose ¶
Function Composition¶
Compose multiple functions into one.
Examples¶
from rite.functional.composition import composition_compose add_one = lambda x: x + 1 double = lambda x: x * 2 f = composition_compose(double, add_one) f(3) 8
Functions¶
composition_compose ¶
Compose functions right to left.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*functions
|
Callable[[Any], Any]
|
Functions to compose. |
()
|
Returns:
| Type | Description |
|---|---|
Callable[[Any], Any]
|
Composed function. |
Examples:
>>> inc = lambda x: x + 1
>>> double = lambda x: x * 2
>>> f = composition_compose(inc, double)
>>> f(3) # inc(double(3)) = inc(6) = 7
7
Notes
Functions are applied right to left: f(g(x)). For left to right, use composition_pipe.
composition_pipe ¶
Function Pipe¶
Pipe value through functions left to right.
Examples¶
from rite.functional.composition import composition_pipe add_one = lambda x: x + 1 double = lambda x: x * 2 f = composition_pipe(add_one, double) f(3) 8
Functions¶
composition_pipe ¶
Pipe value through functions left to right.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*functions
|
Callable[[Any], Any]
|
Functions to pipe through. |
()
|
Returns:
| Type | Description |
|---|---|
Callable[[Any], Any]
|
Piped function. |
Examples:
>>> inc = lambda x: x + 1
>>> double = lambda x: x * 2
>>> f = composition_pipe(inc, double)
>>> f(3) # double(inc(3)) = double(4) = 8
8
Notes
Functions are applied left to right: h = g ∘ f. For right to left, use composition_compose.
composition_chain ¶
Function Chain¶
Chain method calls fluently.
Examples¶
from rite.functional.composition import composition_chain result = composition_chain(3).pipe( ... lambda x: x + 1, ... lambda x: x * 2 ... ).value() result 8
Classes¶
composition_chain ¶
Fluent chainable function application.
Attributes:
| Name | Type | Description |
|---|---|---|
_value |
Current value in chain. |
Examples:
>>> chain = composition_chain(5)
>>> result = chain.pipe(lambda x: x * 2).pipe(
... lambda x: x + 1
... ).value()
>>> result
11
Initialize chain with value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Initial value. |
required |
Apply functions to current value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*functions
|
Callable[[Any], Any]
|
Functions to apply. |
()
|
Returns:
| Type | Description |
|---|---|
composition_chain
|
Self for chaining. |
Examples:
Currying¶
Function currying and partial application.
Currying Module¶
Function currying and uncurrying utilities.
This submodule provides utilities for transforming functions between curried and uncurried forms.
Examples¶
from rite.functional.currying import ( ... currying_curry, ... currying_uncurry ... ) def add(a, b, c): ... return a + b + c curried = currying_curry(add) curried(1)(2)(3) 6
Modules¶
currying_curry ¶
Function Currying¶
Transform multi-argument function into chain of single-argument functions.
Examples¶
from rite.functional.currying import currying_curry def add(a, b, c): ... return a + b + c curried = currying_curry(add) curried(1)(2)(3) 6
Functions¶
currying_curry ¶
Curry a function.
Transforms f(a, b, c) into f(a)(b)©.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., T]
|
Function to curry. |
required |
Returns:
| Type | Description |
|---|---|
Callable[..., T | Callable[..., Any]]
|
Curried function. |
Examples:
>>> def multiply(x, y, z):
... return x * y * z
>>> curried = currying_curry(multiply)
>>> curried(2)(3)(4)
24
>>> partial = curried(2)(3)
>>> partial(4)
24
Notes
Automatically detects function arity from signature.
currying_uncurry ¶
Function Uncurrying¶
Transform curried function back to multi-argument form.
Examples¶
from rite.functional.currying import currying_uncurry curried = lambda a: lambda b: lambda c: a + b + c uncurried = currying_uncurry(curried, 3) uncurried(1, 2, 3) 6
Functions¶
currying_uncurry ¶
Uncurry a curried function.
Transforms f(a)(b)© back to f(a, b, c).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Any]
|
Curried function. |
required |
arity
|
int
|
Number of arguments. |
required |
Returns:
| Type | Description |
|---|---|
Callable[..., Any]
|
Uncurried function. |
Examples:
>>> curried = lambda x: lambda y: x + y
>>> uncurried = currying_uncurry(curried, 2)
>>> uncurried(5, 3)
8
Notes
Arity must be specified as it cannot be inferred.
Decorators¶
Useful decorators.
Decorators Module¶
Function decorators for control flow and behavior.
This submodule provides decorators for debouncing, throttling, one-time execution, and deprecation warnings.
Examples¶
from rite.functional.decorators import ( ... decorators_debounce, ... decorators_throttle, ... decorators_once ... ) @decorators_once() ... def initialize(): ... return "ready"
Modules¶
decorators_debounce ¶
Debounce Decorator¶
Delays function execution by specified wait time.
Examples¶
from rite.functional.decorators import decorators_debounce @decorators_debounce(0.5) ... def process(): ... return "done"
Functions¶
decorators_debounce ¶
Decorator to debounce function call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wait_time
|
float
|
Time to wait in seconds before execution. |
required |
Returns:
| Type | Description |
|---|---|
Callable[[Callable[..., T]], Callable[..., T]]
|
Decorated function that delays execution. |
Examples:
Notes
This implementation waits before each execution. For event-driven debouncing, use throttle pattern.
decorators_throttle ¶
Throttle Decorator¶
Limits function execution frequency.
Examples¶
from rite.functional.decorators import decorators_throttle @decorators_throttle(1.0) ... def api_call(): ... return "response"
Functions¶
decorators_throttle ¶
Decorator to throttle function calls.
Ensures function is not called more frequently than interval.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interval
|
float
|
Minimum time between calls in seconds. |
required |
Returns:
| Type | Description |
|---|---|
Callable[[Callable[..., T]], Callable[..., T | None]]
|
Decorated function with throttling. |
Examples:
>>> @decorators_throttle(1.0)
... def process():
... return "done"
>>> process() # First call executes immediately
'done'
>>> process() # Subsequent calls within 1s are skipped
Notes
Returns None if called within throttle interval.
decorators_once ¶
Once Decorator¶
Ensures function is called only once.
Examples¶
from rite.functional.decorators import decorators_once @decorators_once() ... def initialize(): ... return "initialized"
Functions¶
decorators_once ¶
Decorator to execute function only once.
Subsequent calls return cached result.
Returns:
| Type | Description |
|---|---|
Callable[[Callable[..., T]], Callable[..., T]]
|
Decorated function that runs once. |
Examples:
>>> @decorators_once()
... def setup():
... return "configured"
>>> setup()
'configured'
>>> setup() # Returns cached result
'configured'
Notes
Result is cached regardless of arguments. For argument-sensitive caching, use memoization.
decorators_deprecated ¶
Deprecated Decorator¶
Mark functions as deprecated with warnings.
Examples¶
from rite.functional.decorators import decorators_deprecated @decorators_deprecated("Use new_function instead") ... def old_function(): ... pass
Functions¶
decorators_deprecated ¶
decorators_deprecated(message: str = 'This function is deprecated') -> Callable[[Callable[..., T]], Callable[..., T]]
Decorator to mark function as deprecated.
Issues DeprecationWarning when function is called.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Deprecation message to show. |
'This function is deprecated'
|
Returns:
| Type | Description |
|---|---|
Callable[[Callable[..., T]], Callable[..., T]]
|
Decorated function that shows warning. |
Examples:
>>> @decorators_deprecated("Use process_v2() instead")
... def process_v1():
... return "old"
>>> process_v1()
'old'
Notes
Warnings can be controlled with warnings module.
Memoization¶
Function result caching.
Memoization Module¶
Function result caching utilities.
This submodule provides decorators for memoizing function results with various caching strategies.
Examples¶
from rite.functional.memoization import ( ... memoization_memoize, ... memoization_lru_cache ... ) @memoization_memoize() ... def fibonacci(n): ... if n <= 1: ... return n ... return fibonacci(n-1) + fibonacci(n-2)
Modules¶
memoization_memoize ¶
Simple Memoization¶
Cache function results based on arguments.
Examples¶
from rite.functional.memoization import memoization_memoize @memoization_memoize() ... def fibonacci(n): ... if n <= 1: ... return n ... return fibonacci(n-1) + fibonacci(n-2)
Functions¶
memoization_memoize ¶
Decorator to memoize function results.
Caches results based on arguments.
Returns:
| Type | Description |
|---|---|
Callable[[Callable[..., T]], Callable[..., T]]
|
Decorated function with caching. |
Examples:
>>> @memoization_memoize()
... def expensive_computation(n):
... return sum(range(n))
>>> expensive_computation(100)
4950
>>> expensive_computation(100) # Returns cached result
4950
Notes
Only works with hashable arguments. For unhashable args, use custom cache key.
memoization_lru_cache ¶
LRU Cache¶
Least Recently Used cache with size limit.
Examples¶
from rite.functional.memoization import memoization_lru_cache @memoization_lru_cache(maxsize=128) ... def compute(n): ... return n * n
Functions¶
memoization_lru_cache ¶
LRU cache decorator with size limit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
maxsize
|
int | None
|
Maximum cache size (None for unlimited). |
128
|
Returns:
| Type | Description |
|---|---|
Callable[[Callable[..., T]], Callable[..., T]]
|
Decorated function with LRU caching. |
Examples:
>>> @memoization_lru_cache(maxsize=100)
... def factorial(n):
... if n <= 1:
... return 1
... return n * factorial(n - 1)
>>> factorial(5)
120
Notes
Uses functools.lru_cache internally. Set maxsize=None for unlimited cache.
Examples¶
from rite.functional import (
composition_compose,
decorators_debounce,
memoization_memoize
)
# Compose functions
add_one = lambda x: x + 1
double = lambda x: x * 2
composed = composition_compose(add_one, double)
result = composed(5) # (5 * 2) + 1 = 11
# Debounce function
@decorators_debounce(seconds=1.0)
def on_input_change(value):
print(f"Processing: {value}")
# Memoize expensive function
@memoization_memoize
def fibonacci(n):
if n < 2:
return n
return fibonacci(n-1) + fibonacci(n-2)