larray.Session.apply

Session.apply(func, *args, kind=<class 'larray.core.array.Array'>, **kwargs) Session[source]

Apply function func on elements of the session and return a new session.

Parameters
funcfunction

Function to apply to each element of the session. It should take a single element argument and return a single value.

*argsany

Any extra arguments are passed to the function

kindtype or tuple of types, optional

Type(s) of elements func will be applied to. Other elements will be left intact. Use kind=object to apply to all kinds of objects. Defaults to Array.

**kwargsany

Any extra keyword arguments are passed to the function

Returns
Session

A new session containing all processed elements

Examples

>>> arr1 = ndtest(2)
>>> arr1
a  a0  a1
    0   1
>>> arr2 = ndtest(3)
>>> arr2
a  a0  a1  a2
    0   1   2
>>> sess1 = Session({'arr1': arr1, 'arr2': arr2})
>>> sess1
Session(arr1, arr2)
>>> def increment(array):
...     return array + 1
>>> sess2 = sess1.apply(increment)
>>> sess2.arr1
a  a0  a1
    1   2
>>> sess2.arr2
a  a0  a1  a2
    1   2   3

You may also pass extra arguments or keyword arguments to the function

>>> def change(array, increment=1, multiplier=1):
...     return (array + increment) * multiplier
>>> sess2 = sess1.apply(change, 2, 2)
>>> sess2 = sess1.apply(change, 2, multiplier=2)
>>> sess2.arr1
a  a0  a1
    4   6
>>> sess2.arr2
a  a0  a1  a2
    4   6   8