pub struct TryTrampoline<A: 'static, E: 'static>(/* private fields */);Expand description
A lazy, stack-safe computation that may fail with an error.
This is Trampoline<Result<A, E>> with ergonomic combinators.
§Type Parameters
A: The type of the success value.E: The type of the error value.
§Fields
0: The internalTrampolinewrapping aResult.
§Examples
use fp_library::types::*;
let task: TryTrampoline<i32, String> = TryTrampoline::ok(10);
assert_eq!(task.evaluate(), Ok(10));Implementations§
Source§impl<A: 'static + Send, E: 'static + Send> TryTrampoline<A, E>
impl<A: 'static + Send, E: 'static + Send> TryTrampoline<A, E>
Sourcepub fn ok(a: A) -> Self
pub fn ok(a: A) -> Self
Creates a successful TryTrampoline.
§Type Signature
forall self. A -> self
§Type Parameters
A: The type of the success value.E: The type of the error value.
§Parameters
a: The success value.
§Returns
A TryTrampoline representing success.
§Examples
use fp_library::types::*;
let task: TryTrampoline<i32, String> = TryTrampoline::ok(42);
assert_eq!(task.evaluate(), Ok(42));Sourcepub fn err(e: E) -> Self
pub fn err(e: E) -> Self
Creates a failed TryTrampoline.
§Type Signature
forall self. E -> self
§Type Parameters
A: The type of the success value.E: The type of the error value.
§Parameters
e: The error value.
§Returns
A TryTrampoline representing failure.
§Examples
use fp_library::types::*;
let task: TryTrampoline<i32, String> = TryTrampoline::err("error".to_string());
assert_eq!(task.evaluate(), Err("error".to_string()));Sourcepub fn new<F>(f: F) -> Self
pub fn new<F>(f: F) -> Self
Creates a lazy TryTrampoline that may fail.
§Type Signature
forall self. (() -> Result A E) -> self
§Type Parameters
F: The type of the closure.
§Parameters
f: The closure to execute.
§Returns
A TryTrampoline that executes f when run.
§Examples
use fp_library::types::*;
let task: TryTrampoline<i32, String> = TryTrampoline::new(|| Ok(42));
assert_eq!(task.evaluate(), Ok(42));Sourcepub fn defer<F>(f: F) -> Selfwhere
F: FnOnce() -> TryTrampoline<A, E> + 'static,
pub fn defer<F>(f: F) -> Selfwhere
F: FnOnce() -> TryTrampoline<A, E> + 'static,
Defers the construction of a TryTrampoline.
Use this for stack-safe recursion.
§Type Signature
forall self. (() -> TryTrampoline A E) -> self
§Type Parameters
F: The type of the thunk.
§Parameters
f: A thunk that returns the next step.
§Returns
A TryTrampoline that executes f to get the next step.
§Examples
use fp_library::types::*;
let task: TryTrampoline<i32, String> = TryTrampoline::defer(|| TryTrampoline::ok(42));
assert_eq!(task.evaluate(), Ok(42));Stack-safe recursion:
use fp_library::types::*;
fn factorial(n: i32, acc: i32) -> TryTrampoline<i32, String> {
if n < 0 {
TryTrampoline::err("Negative input".to_string())
} else if n == 0 {
TryTrampoline::ok(acc)
} else {
TryTrampoline::defer(move || factorial(n - 1, n * acc))
}
}
let task = factorial(5, 1);
assert_eq!(task.evaluate(), Ok(120));Sourcepub fn map<B: 'static + Send, Func>(self, func: Func) -> TryTrampoline<B, E>where
Func: FnOnce(A) -> B + 'static,
pub fn map<B: 'static + Send, Func>(self, func: Func) -> TryTrampoline<B, E>where
Func: FnOnce(A) -> B + 'static,
Maps over the success value.
§Type Signature
forall b. (A -> b) -> TryTrampoline b E
§Type Parameters
B: The type of the new success value.F: The type of the mapping function.
§Parameters
func: The function to apply to the success value.
§Returns
A new TryTrampoline with the transformed success value.
§Examples
use fp_library::types::*;
let task: TryTrampoline<i32, String> = TryTrampoline::ok(10).map(|x| x * 2);
assert_eq!(task.evaluate(), Ok(20));Sourcepub fn map_err<E2: 'static + Send, Func>(
self,
func: Func,
) -> TryTrampoline<A, E2>where
Func: FnOnce(E) -> E2 + 'static,
pub fn map_err<E2: 'static + Send, Func>(
self,
func: Func,
) -> TryTrampoline<A, E2>where
Func: FnOnce(E) -> E2 + 'static,
Maps over the error value.
§Type Signature
forall e2. (E -> e2) -> TryTrampoline A e2
§Type Parameters
E2: The type of the new error value.F: The type of the mapping function.
§Parameters
func: The function to apply to the error value.
§Returns
A new TryTrampoline with the transformed error value.
§Examples
use fp_library::types::*;
let task: TryTrampoline<i32, String> = TryTrampoline::err("error".to_string())
.map_err(|e| e.to_uppercase());
assert_eq!(task.evaluate(), Err("ERROR".to_string()));Sourcepub fn bind<B: 'static + Send, F>(self, f: F) -> TryTrampoline<B, E>where
F: FnOnce(A) -> TryTrampoline<B, E> + 'static,
pub fn bind<B: 'static + Send, F>(self, f: F) -> TryTrampoline<B, E>where
F: FnOnce(A) -> TryTrampoline<B, E> + 'static,
Chains fallible computations.
§Type Signature
forall b. (A -> TryTrampoline b E) -> TryTrampoline b E
§Type Parameters
B: The type of the new success value.F: The type of the binding function.
§Parameters
f: The function to apply to the success value.
§Returns
A new TryTrampoline that chains f after this task.
§Examples
use fp_library::types::*;
let task: TryTrampoline<i32, String> = TryTrampoline::ok(10).bind(|x| TryTrampoline::ok(x * 2));
assert_eq!(task.evaluate(), Ok(20));Sourcepub fn catch<F>(self, f: F) -> Selfwhere
F: FnOnce(E) -> TryTrampoline<A, E> + 'static,
pub fn catch<F>(self, f: F) -> Selfwhere
F: FnOnce(E) -> TryTrampoline<A, E> + 'static,
Recovers from an error.
§Type Signature
forall self. (E -> TryTrampoline A E) -> self
§Type Parameters
F: The type of the recovery function.
§Parameters
f: The function to apply to the error value.
§Returns
A new TryTrampoline that attempts to recover from failure.
§Examples
use fp_library::types::*;
let task: TryTrampoline<i32, String> = TryTrampoline::err("error".to_string())
.catch(|_| TryTrampoline::ok(42));
assert_eq!(task.evaluate(), Ok(42));Trait Implementations§
Source§impl<A, E> Deferrable<'static> for TryTrampoline<A, E>
impl<A, E> Deferrable<'static> for TryTrampoline<A, E>
Source§fn defer<F>(f: F) -> Self
fn defer<F>(f: F) -> Self
Creates a value from a computation that produces the value.
§Type Signature
forall self. Deferrable self => (() -> self) -> self
§Type Parameters
F: The type of the thunk.
§Parameters
f: A thunk that produces the value.
§Returns
The deferred value.
§Examples
use fp_library::{brands::*, functions::*, types::*, classes::Deferrable};
let task: TryTrampoline<i32, String> = Deferrable::defer(|| TryTrampoline::ok(42));
assert_eq!(task.evaluate(), Ok(42));Source§impl<A, E, Config> From<Lazy<'static, A, Config>> for TryTrampoline<A, E>
impl<A, E, Config> From<Lazy<'static, A, Config>> for TryTrampoline<A, E>
Source§impl<A, E> From<Trampoline<A>> for TryTrampoline<A, E>
impl<A, E> From<Trampoline<A>> for TryTrampoline<A, E>
Source§fn from(task: Trampoline<A>) -> Self
fn from(task: Trampoline<A>) -> Self
Source§impl<A, E, Config> From<TryLazy<'static, A, E, Config>> for TryTrampoline<A, E>
impl<A, E, Config> From<TryLazy<'static, A, E, Config>> for TryTrampoline<A, E>
Source§impl<'a, A, E> From<TryTrampoline<A, E>> for TryLazy<'a, A, E, RcLazyConfig>
impl<'a, A, E> From<TryTrampoline<A, E>> for TryLazy<'a, A, E, RcLazyConfig>
Source§fn from(task: TryTrampoline<A, E>) -> Self
fn from(task: TryTrampoline<A, E>) -> Self
Auto Trait Implementations§
impl<A, E> Freeze for TryTrampoline<A, E>
impl<A, E> !RefUnwindSafe for TryTrampoline<A, E>
impl<A, E> !Send for TryTrampoline<A, E>
impl<A, E> !Sync for TryTrampoline<A, E>
impl<A, E> Unpin for TryTrampoline<A, E>
impl<A, E> !UnwindSafe for TryTrampoline<A, E>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more