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 |
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 |
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 |
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 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
Callable[[~A], pfun.trampoline.Trampoline[~B]] |
Function to map over |
required |
iterable |
Iterable[~A] |
Iterable to map |
required |
Returns:
Type | Description |
---|---|
pfun.trampoline.Trampoline[Iterable[~B]] |
|
pfun.trampoline.sequence(iterable)
Evaluate each Trampoline
in iterable
from left to right
and collect the results
Examples:
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]] |
|
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 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
Callable[[~A], pfun.trampoline.Trampoline[bool]] |
Function to map |
required |
iterable |
Iterable[~A] |
Iterable to map by |
required |
Returns:
Type | Description |
---|---|
pfun.trampoline.Trampoline[Iterable[~A]] |
|