Skip to content

pfun.lens.lens(t=None)

Create a Lens object that supports transformations of instances of type T

Examples:

>>> from typing import List
>>> lens(List[int])[0](1)([0])
[1]

Parameters:

Name Type Description Default
t Optional[T]

Type that the Lens supports transformations of

None

Returns:

Type Description
RootLens[T]

Lens instance

pfun.lens.RootLens dataclass

Lens object that supports attribute access and indexing

__getattr__(self, name)

Create a new Lens that transform instances at path name

Examples:

>>> from pfun import Immutable
>>> class User(Immutable):
...     name: str
>>> user = User('Bob')
>>> lens(User).name('Alice')(user)
User(name='Alice')

Parameters:

Name Type Description Default
name str

name of attribute to transform in returned Lens

required

Returns:

Type Description
Lens[A, Any]

Lens that transform instances at path name

__getitem__(self, index)

Create a Lens object that supports transformations of instances at index `index

Examples:

>>> from typing import List
>>> lens(List[int])[0](1)([0])
[1]

Parameters:

Name Type Description Default
index Any

index that the Lens supports transformations of

required

Returns:

Type Description
Lens[A, Any]

Lens instance

pfun.lens.Lens dataclass

Lens object that supports attribute access, indexing and transform objects

__call__(self, value)

Apply lens to object with value.

Examples:

>>> lens()['foo']('bar')({})
{'foo': 'bar'}

Parameters:

Name Type Description Default
value B

Value to assign to this name/index in the path

required

Returns:

Type Description
Callable[[A], A]

Transformation function that can be applied to an object