param.reactive.reactive_ops.map#
- reactive_ops.map(func, /, *args, **kwargs) rx[source]#
Apply a function to each item in the reactive collection.
This method applies a given function to every item in the current reactive collection and returns a new reactive expression containing the results.
- Parameters:
func (callable) – The function to apply to each item in the collection.
*args (iterable, optional) – Positional arguments to pass to the function.
**kwargs (dict, optional) – Keyword arguments to pass to the function.
- Returns:
A new reactive expression containing the results of applying
functo each item.- Return type:
- Raises:
TypeError – If
funcis a generator or asynchronous generator function, which are not supported.
Examples
Apply a function to double each item in a reactive list:
>>> import param >>> reactive_list = param.rx([1, 2, 3]) >>> reactive_doubled = reactive_list.rx.map(lambda x: x * 2) >>> reactive_doubled.rx.value [2, 4, 6]
Use positional arguments in the function:
>>> reactive_offset = reactive_list.rx.map(lambda x, offset: x + offset, offset=10) >>> reactive_offset.rx.value [11, 12, 13]
Use keyword arguments in the function:
>>> reactive_power = reactive_list.rx.map(lambda x, power=1: x ** power, power=3) >>> reactive_power.rx.value [1, 8, 27]