Trait aoko::no_std::functions::ext::AnyExt1[][src]

pub trait AnyExt1<R>: Sized {
    fn let_ref<'a>(&'a self, f: impl FnOnce(&'a Self) -> R) -> R { ... }
fn let_mut<'a>(&'a mut self, f: impl FnOnce(&'a mut Self) -> R) -> R { ... }
fn let_owned(self, f: impl FnOnce(Self) -> R) -> R { ... }
fn also_ref(self, f: impl FnOnce(&Self) -> R) -> Self { ... }
fn also_mut(self, f: impl FnOnce(&mut Self) -> R) -> Self { ... }
fn y(self, f: impl Copy + Fn(&dyn Fn(Self) -> R, Self) -> R) -> R { ... }
fn then_if(
        self,
        f1: impl FnOnce(&Self) -> bool,
        f2: impl FnOnce(Self) -> R
    ) -> Option<R> { ... }
fn then_unless(
        self,
        f1: impl FnOnce(&Self) -> bool,
        f2: impl FnOnce(Self) -> R
    ) -> Option<R> { ... } }
Expand description

This trait is to implement some extension functions, which need a generic return type, for any sized type.

Provided methods

Performs operation f with &self, returns the closure result.

Moves environment variable(s) to closure by default.

Examples
use aoko::no_std::functions::ext::*;

let s = "Hello".let_ref(|s| format!("String is: {}", s));
assert_eq!(s, "String is: Hello");

Performs operation f with &mut self, returns the closure result.

Moves environment variable(s) to closure by default.

Examples
use aoko::no_std::functions::ext::*;

let v = vec!["a", "b", "c"]
    .let_mut(|v| { v.push("d"); format!("Vector is: {:?}", v) });
assert_eq!(v, "Vector is: [\"a\", \"b\", \"c\", \"d\"]");

Consumes self, performs operation f with it, returns the closure result.

Moves environment variable(s) to closure by default.

Examples
use aoko::no_std::functions::ext::*;

assert_eq!(1, 1.let_owned(|i| i));

Consumes self, performs operation f with it, returns the receiver.

Moves environment variable(s) to closure by default.

Examples
use aoko::no_std::functions::ext::*;

let s = "abc".also_ref(|s| s.chars().for_each(|c| print!("{}", c)));
assert_eq!(s, "abc");

Consumes self, performs operation f on it, returns the updated value.

Moves environment variable(s) to closure by default.

Examples
use aoko::no_std::functions::ext::*;

let v = vec!["Hello", "Kotlin"].also_mut(|v| v[1] = "Rust");
assert_eq!(v, vec!["Hello", "Rust"]);

The Y Combinator

Examples
use aoko::no_std::functions::ext::*;

fn fact(n: usize) -> usize {
    n.y(|f, n| match n {
        0 | 1 => 1,
        n => n * f(n - 1),
    })
}
assert_eq!(fact(5), 5 * 4 * 3 * 2 * 1);

fn fibonacci(n: usize) -> usize {
    n.y(|f, n| match n {
        0 => 0,
        1 => 1,
        n => f(n - 1) + f(n - 2),
    })
}
assert_eq!(fibonacci(10), 55);

Returns Some(f()) if it satisfies the given predicate function, or None if it doesn’t.

Examples
use aoko::no_std::functions::ext::*;

assert_eq!("Hello World".to_string().as_some(), "Hello".then_if(|s| s.starts_with("Hel"), |s| format!("{} World", s)));
assert_eq!(None, "Hello".then_if(|s| s.starts_with("Wor"), |_| ()));

Returns Some(f()) if it doesn’t satisfy the given predicate function, or None if it does.

Examples
use aoko::no_std::functions::ext::*;

assert_eq!(None, "Hello".then_unless(|s| s.starts_with("Hel"), |_| ()));
assert_eq!("Hello World".to_string().as_some(), "Hello".then_unless(|s| s.starts_with("Wor"), |s| format!("{} World", s)));

Implementors