pub trait Apply<F, AB>where
Self: Functor<F, AB>,
F: HKT1<T<AB> = Self>,{
// Required method
fn apply_with<A, B, Cloner>(self, a: F::T<A>, cloner: Cloner) -> F::T<B>
where AB: F1<A, Ret = B>,
Cloner: for<'a> F1<&'a A, Ret = A>;
// Provided method
fn apply<A, B>(self, a: F::T<A>) -> F::T<B>
where Self: Sized,
AB: F1<A, Ret = B>,
A: Clone { ... }
}
Expand description
Apply
generalizes Functor
to any arity.
In other words, you can lift a function
A -> B -> C -> D
to
F<A> -> F<B> -> F<C> -> F<D>
.
Example
use naan::prelude::*;
fn maybe_n() -> Option<usize> {
Some(1)
}
fn maybe_string() -> Option<String> {
None
}
fn maybe_bytes() -> Option<Vec<u8>> {
None
}
fn combine_many_things(n: usize, string: String, bytes: Vec<u8>) {
// ...
}
Some(combine_many_things.curry()).apply(maybe_n())
.apply(maybe_string())
.apply(maybe_bytes());
// with `if let`:
if let Some(n) = maybe_n() {
if let Some(s) = maybe_string() {
if let Some(bs) = maybe_bytes() {
combine_many_things(n, s, bs);
}
}
}
// with `let else`:
let Some(n) = maybe_n() else {return};
let Some(s) = maybe_string() else {return};
let Some(bs) = maybe_bytes() else {return};
combine_many_things(n, s, bs);
// with `match`:
match (maybe_n(), maybe_string(), maybe_bytes()) {
| (Some(n), Some(s), Some(bs)) => combine_many_things(n, s, bs),
| _ => (),
}