pub struct TryThunk<'a, A, E>(/* private fields */);Expand description
A deferred computation that may fail with error type E.
Like Thunk, this is NOT memoized. Each TryThunk::evaluate re-executes.
Unlike Thunk, the result is Result<A, E>.
§Type Parameters
A: The type of the value produced by the computation on success.E: The type of the error produced by the computation on failure.
§Fields
0: The closure that performs the computation.
§Examples
use fp_library::types::*;
let computation: TryThunk<i32, &str> = TryThunk::new(|| {
Ok(42)
});
match computation.evaluate() {
Ok(val) => assert_eq!(val, 42),
Err(_) => panic!("Should not fail"),
}Implementations§
Source§impl<'a, A: 'a, E: 'a> TryThunk<'a, A, E>
impl<'a, A: 'a, E: 'a> TryThunk<'a, A, E>
Sourcepub fn new<F>(f: F) -> Self
pub fn new<F>(f: F) -> Self
Creates a new TryThunk from a thunk.
§Type Signature
forall self. (() -> Result A E) -> self
§Type Parameters
F: The type of the thunk.
§Parameters
f: The thunk to wrap.
§Returns
A new TryThunk instance.
§Examples
use fp_library::types::*;
let try_thunk: TryThunk<i32, ()> = TryThunk::new(|| Ok(42));
assert_eq!(try_thunk.evaluate(), Ok(42));Sourcepub fn pure(a: A) -> Selfwhere
A: 'a,
pub fn pure(a: A) -> Selfwhere
A: 'a,
Returns a pure value (already computed).
§Type Signature
forall self. A -> self
§Parameters
a: The value to wrap.
§Returns
A new TryThunk instance containing the value.
§Examples
use fp_library::types::*;
let try_thunk: TryThunk<i32, ()> = TryThunk::pure(42);
assert_eq!(try_thunk.evaluate(), Ok(42));Sourcepub fn defer<F>(f: F) -> Self
pub fn defer<F>(f: F) -> Self
Defers a computation that returns a TryThunk.
§Type Signature
forall self. (() -> TryThunk A E) -> self
§Type Parameters
F: The type of the thunk.
§Parameters
f: The thunk that returns aTryThunk.
§Returns
A new TryThunk instance.
§Examples
use fp_library::types::*;
let try_thunk: TryThunk<i32, ()> = TryThunk::defer(|| TryThunk::pure(42));
assert_eq!(try_thunk.evaluate(), Ok(42));Sourcepub fn ok(a: A) -> Selfwhere
A: 'a,
pub fn ok(a: A) -> Selfwhere
A: 'a,
Alias for pure.
Creates a successful computation.
§Type Signature
forall self. A -> self
§Parameters
a: The value to wrap.
§Returns
A new TryThunk instance containing the value.
§Examples
use fp_library::types::*;
let try_thunk: TryThunk<i32, ()> = TryThunk::ok(42);
assert_eq!(try_thunk.evaluate(), Ok(42));Sourcepub fn bind<B: 'a, F>(self, f: F) -> TryThunk<'a, B, E>
pub fn bind<B: 'a, F>(self, f: F) -> TryThunk<'a, B, E>
Monadic bind: chains computations.
§Type Signature
forall b. (A -> TryThunk b E) -> TryThunk b E
§Type Parameters
B: The type of the result of the new computation.F: The type of the function to apply.
§Parameters
f: The function to apply to the result of the computation.
§Returns
A new TryThunk instance representing the chained computation.
§Examples
use fp_library::types::*;
let try_thunk: TryThunk<i32, ()> = TryThunk::pure(21).bind(|x| TryThunk::pure(x * 2));
assert_eq!(try_thunk.evaluate(), Ok(42));Sourcepub fn map<B: 'a, Func>(self, func: Func) -> TryThunk<'a, B, E>where
Func: FnOnce(A) -> B + 'a,
pub fn map<B: 'a, Func>(self, func: Func) -> TryThunk<'a, B, E>where
Func: FnOnce(A) -> B + 'a,
Functor map: transforms the result.
§Type Signature
forall b. (A -> b) -> TryThunk b E
§Type Parameters
B: The type of the result of the transformation.F: The type of the transformation function.
§Parameters
func: The function to apply to the result of the computation.
§Returns
A new TryThunk instance with the transformed result.
§Examples
use fp_library::types::*;
let try_thunk: TryThunk<i32, ()> = TryThunk::pure(21).map(|x| x * 2);
assert_eq!(try_thunk.evaluate(), Ok(42));Sourcepub fn map_err<E2: 'a, F>(self, f: F) -> TryThunk<'a, A, E2>where
F: FnOnce(E) -> E2 + 'a,
pub fn map_err<E2: 'a, F>(self, f: F) -> TryThunk<'a, A, E2>where
F: FnOnce(E) -> E2 + 'a,
Map error: transforms the error.
§Type Signature
forall e2. (E -> e2) -> TryThunk A e2
§Type Parameters
E2: The type of the new error.F: The type of the transformation function.
§Parameters
f: The function to apply to the error.
§Returns
A new TryThunk instance with the transformed error.
§Examples
use fp_library::types::*;
let try_thunk: TryThunk<i32, i32> = TryThunk::err(21).map_err(|x| x * 2);
assert_eq!(try_thunk.evaluate(), Err(42));Sourcepub fn catch<F>(self, f: F) -> Self
pub fn catch<F>(self, f: F) -> Self
Recovers from an error.
§Type Signature
forall self. (E -> TryThunk 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 TryThunk that attempts to recover from failure.
§Examples
use fp_library::types::*;
let try_thunk: TryThunk<i32, &str> = TryThunk::err("error")
.catch(|_| TryThunk::pure(42));
assert_eq!(try_thunk.evaluate(), Ok(42));Trait Implementations§
Source§impl<'a, A, E> Deferrable<'a> for TryThunk<'a, A, E>where
A: 'a,
E: 'a,
impl<'a, A, E> Deferrable<'a> for TryThunk<'a, A, E>where
A: 'a,
E: 'a,
Source§fn defer<F>(f: F) -> Self
fn defer<F>(f: F) -> Self
Creates a TryThunk from a computation that produces it.
§Type Signature
forall self. Deferrable self => (() -> self) -> self
§Type Parameters
F: The type of the thunk.
§Parameters
f: A thunk that produces the try thunk.
§Returns
The deferred try thunk.
§Examples
use fp_library::{brands::*, functions::*, types::*, classes::Deferrable};
let task: TryThunk<i32, ()> = Deferrable::defer(|| TryThunk::pure(42));
assert_eq!(task.evaluate(), Ok(42));Source§impl<'a, A, E, Config> From<Lazy<'a, A, Config>> for TryThunk<'a, A, E>where
A: Clone + 'a,
E: 'a,
Config: LazyConfig,
impl<'a, A, E, Config> From<Lazy<'a, A, Config>> for TryThunk<'a, A, E>where
A: Clone + 'a,
E: 'a,
Config: LazyConfig,
Source§impl<'a, A: Semigroup + 'a, E: 'a> Semigroup for TryThunk<'a, A, E>
impl<'a, A: Semigroup + 'a, E: 'a> Semigroup for TryThunk<'a, A, E>
Source§fn append(a: Self, b: Self) -> Self
fn append(a: Self, b: Self) -> Self
Combines two TryThunks by combining their results.
§Type Signature
forall self. Semigroup self => (self, self) -> self
§Parameters
a: The firstTryThunk.b: The secondTryThunk.
§Returns
A new TryThunk containing the combined result.
§Examples
use fp_library::{brands::*, classes::*, functions::*, types::*};
let t1: TryThunk<String, ()> = pure::<TryThunkWithErrBrand<()>, _>("Hello".to_string());
let t2: TryThunk<String, ()> = pure::<TryThunkWithErrBrand<()>, _>(" World".to_string());
let t3 = append::<_>(t1, t2);
assert_eq!(t3.evaluate(), Ok("Hello World".to_string()));Auto Trait Implementations§
impl<'a, A, E> Freeze for TryThunk<'a, A, E>
impl<'a, A, E> !RefUnwindSafe for TryThunk<'a, A, E>
impl<'a, A, E> !Send for TryThunk<'a, A, E>
impl<'a, A, E> !Sync for TryThunk<'a, A, E>
impl<'a, A, E> Unpin for TryThunk<'a, A, E>
impl<'a, A, E> !UnwindSafe for TryThunk<'a, 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