Skip to main content

TryTrampoline

Struct TryTrampoline 

Source
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 internal Trampoline wrapping a Result.

§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>

Source

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));
Source

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()));
Source

pub fn new<F>(f: F) -> Self
where F: FnOnce() -> Result<A, E> + 'static,

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));
Source

pub fn defer<F>(f: F) -> Self
where 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));
Source

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));
Source

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()));
Source

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));
Source

pub fn catch<F>(self, f: F) -> Self
where 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));
Source

pub fn evaluate(self) -> Result<A, E>

Runs the computation, returning the result.

§Type Signature

Result A E

§Returns

The result of the computation.

§Examples
use fp_library::types::*;

let task: TryTrampoline<i32, String> = TryTrampoline::ok(42);
assert_eq!(task.evaluate(), Ok(42));

Trait Implementations§

Source§

impl<A, E> Deferrable<'static> for TryTrampoline<A, E>
where A: 'static + Send, E: 'static + Send,

Source§

fn defer<F>(f: F) -> Self
where F: FnOnce() -> Self + 'static, Self: Sized,

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>
where A: Clone + Send + 'static, E: Send + 'static, Config: LazyConfig,

Source§

fn from(memo: Lazy<'static, A, Config>) -> Self

Converts to this type from the input type.
Source§

impl<A, E> From<Trampoline<A>> for TryTrampoline<A, E>
where A: Send + 'static, E: Send + 'static,

Source§

fn from(task: Trampoline<A>) -> Self

Converts to this type from the input type.
Source§

impl<A, E, Config> From<TryLazy<'static, A, E, Config>> for TryTrampoline<A, E>
where A: Clone + Send + 'static, E: Clone + Send + 'static, Config: LazyConfig,

Source§

fn from(memo: TryLazy<'static, A, E, Config>) -> Self

Converts to this type from the input type.
Source§

impl<'a, A, E> From<TryTrampoline<A, E>> for TryLazy<'a, A, E, RcLazyConfig>
where A: Send, E: Send,

Source§

fn from(task: TryTrampoline<A, E>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<A, E> Freeze for TryTrampoline<A, E>
where A: Freeze, E: Freeze,

§

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>
where A: Unpin, E: Unpin,

§

impl<A, E> !UnwindSafe for TryTrampoline<A, E>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.