Skip to content

pfun.trampoline.Trampoline dataclass

Base class for Trampolines. Useful for writing stack safe-safe recursive functions.

and_then(self, f)

Apply f to the value wrapped by this trampoline.

Parameters:

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

function to apply the value in this trampoline

required

Returns:

Type Description
Trampoline[B]

Result of applying f to the value wrapped by this trampoline

map(self, f)

Map f over the value wrapped by this trampoline.

Parameters:

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

function to wrap over this trampoline

required

Returns:

Type Description
Trampoline[B]

new trampoline wrapping the result of f

run(self)

Interpret a structure of trampolines to produce a result

Returns:

Type Description
~A

result of intepreting this structure of trampolines

pfun.trampoline.Done dataclass

Represents the result of a recursive computation.

pfun.trampoline.Call dataclass

Represents a recursive call.

pfun.trampoline.AndThen dataclass

Represents monadic bind for trampolines as a class to avoid deep recursive calls to Trampoline.run during interpretation.

and_then(self, f)

Apply f to the value wrapped by this trampoline.

Parameters:

Name Type Description Default
f Callable[[~A], pfun.trampoline.Trampoline[~B]]

function to apply the value in this trampoline

required

Returns:

Type Description
pfun.trampoline.Trampoline[~B]

Result of applying f to the value wrapped by this trampoline

pfun.trampoline.for_each(f, iterable)

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

Examples:

1
2
>>> map_m(Done, range(3)).run()
(0, 1, 2)

Parameters:

Name Type Description Default
f Callable[[~A], pfun.trampoline.Trampoline[~B]]

Function to map over iterable

required
iterable Iterable[~A]

Iterable to map f over

required

Returns:

Type Description
pfun.trampoline.Trampoline[Iterable[~B]]

f mapped over iterable and combined from left to right.

pfun.trampoline.sequence(iterable)

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

Examples:

1
2
>>> sequence([Done(v) for v in range(3)]).run()
(0, 1, 2)

Parameters:

Name Type Description Default
iterable Iterable[pfun.trampoline.Trampoline[~A]]

The iterable to collect results from

required

Returns:

Type Description
pfun.trampoline.Trampoline[Iterable[~A]]

Trampoline of collected results

pfun.trampoline.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:

1
2
>>> filter_m(lambda v: Done(v % 2 == 0), range(3)).run()
(0, 2)

Parameters:

Name Type Description Default
f Callable[[~A], pfun.trampoline.Trampoline[bool]]

Function to map iterable by

required
iterable Iterable[~A]

Iterable to map by f

required

Returns:

Type Description
pfun.trampoline.Trampoline[Iterable[~A]]

iterable mapped and filtered by f