Skip to content

pfun.list.List

__add__(self, other)

Concatenate with other Iterable or List

Examples:

>>> List(range(2)) + range(2, 4)
List((0, 1, 2, 3))

Parameters:

Name Type Description Default
other Iterable[~A]

Iterable to concatenate with

required

Returns:

Type Description
List[A]

new List concatenated with other

__radd__(self, other)

Concatenate with other Iterable or List

Examples:

>>> range(2) + List(range(2, 4))
List((0, 1, 2, 3))

Parameters:

Name Type Description Default
other Iterable[~A]

Iterable to concatenate with

required

Returns:

Type Description
List[A]

new List concatenated with other

__repr__(self)

Return repr(self).

and_then(self, f)

Chain together functions that produce more than one result

Examples:

>>> List(range(4)).and_then(lambda v: List(range(v)))
[0, 0, 1, 0, 1, 2]

Parameters:

Name Type Description Default
f Callable[[A], List[B]]

Function to apply to elements of this List

required

Returns:

Type Description
List[B]

Concatenated results from applying f to all elements

append(self, a)

Add element to end of list

Examples:

>>> List(range(3)).append(3)
[1, 2, 3]

Parameters:

Name Type Description Default
a ~A

Element to append

required

Returns:

Type Description
List[A]

New List with a appended

extend(self, iterable)

Add all elements from iterable to end of list

Examples:

>>> List(range(3)).extend(range(3))
[0, 1, 2, 0, 1, 2]

Parameters:

Name Type Description Default
iterable Iterable[~A]

Iterable to extend by

required

Returns:

Type Description
List[A]

New List with extended by iterable

filter(self, f)

Filter elements by the predicate f

Examples:

>>> List(range(4)).filter(lambda e: e % 2 == 0)
[0, 2]

Parameters:

Name Type Description Default
f Callable[[~A], bool]

Function to filter by

required

Returns:

Type Description
List[A]

new List filtered by f

map(self, f)

Apply f to each element in the list

Examples:

>>> List(range(2)).map(str)
['0', '1']

Parameters:

Name Type Description Default
f Callable[[~A], ~B]

Function to apply

required

Returns:

Type Description
List[B]

new List mapped by f

reduce(self, f, initializer=None)

Aggregate elements by f

Examples:

>>> List(range(3)).reduce(lambda a, b: a + b)
3

Parameters:

Name Type Description Default
f Callable[[~B, ~A], ~B]

Function to perform aggregation

required
initializer Optional[~B]

Starting value for aggregation

None

Returns:

Type Description
~B

Aggregated result

zip(self, other)

Zip together with another iterable

Examples:

>>> List(List(range(2)).zip(range(2)))
[(0, 0), (1, 1)]

Parameters:

Name Type Description Default
other Iterable[~B]

Iterable to zip with

required

Returns:

Type Description
Iterable[Tuple[~A, ~B]]

Zip with other

pfun.list.for_each(f, iterable)

Map each in element in iterable to an List by applying f, combine the elements by and_then from left to right and collect the results

Examples:

>>> for_each(lambda v: List([v]), range(3))
List(((0, 1, 2),))

Parameters:

Name Type Description Default
f Callable[[~A], pfun.list.List[~B]]

Function to map over iterable

required
iterable Iterable[~A]

Iterable to map f over

required

Returns:

Type Description
pfun.list.List[Iterable[~B]]

f mapped over iterable and combined from left to right.

pfun.list.gather(iterable)

Evaluate each List in iterable from left to right and collect the results

Examples:

>>> gather([List([v]) for v in range(3)])
List(((0, 1, 2),))

Parameters:

Name Type Description Default
iterable Iterable[pfun.list.List[~A]]

The iterable to collect results from

required

Returns:

Type Description
pfun.list.List[Iterable[~A]]

List of collected results

pfun.list.filter_(f, iterable)

Map each element in iterable by applying f, filter the results by the value returned by f and combine from left to right.

Examples:

>>> filter(lambda v: List([v % 2 == 0]), range(3))
List(((0, 2),))

Parameters:

Name Type Description Default
f Callable[[~A], pfun.list.List[bool]]

Function to map iterable by

required
iterable Iterable[~A]

Iterable to map by f

required

Returns:

Type Description
pfun.list.List[Iterable[~A]]

iterable mapped and filtered by f