pub struct FBox<FIn, FOut> { /* private fields */ }
Expand description
FBox
is a generic wrapper of a unary function. FBox
lets you compose unary functions via a user friendly syntax and in a type-safe manner.
FBox
works with functions that take ownership over their argument. This way you can exploit the ownership rules of Rust.
FBox
is lazy. It only calls its function when you explicitly tell it to do so. You can call apply
or apply_drop
with a single argument, the type of which corresponds to the FIn
type parameter of the FBox
. The return type will correspond to the FOut
type parameter.
Implementations
sourceimpl<FIn: 'static, FOut: 'static> FBox<FIn, FOut>
impl<FIn: 'static, FOut: 'static> FBox<FIn, FOut>
sourcepub fn new(f: impl Fn(FIn) -> FOut + 'static) -> FBox<FIn, FOut>
pub fn new(f: impl Fn(FIn) -> FOut + 'static) -> FBox<FIn, FOut>
Creates a new FBox
from a unary function.
Examples
fn inc(n: i32) -> i32 {
n + 1
}
let fbox1 = FBox::new(|x| x + 1);
let fbox2 = FBox::new(inc);
assert_eq!(
fbox1.apply(3),
fbox2.apply(3)
);
sourcepub fn compose<GIn: 'static>(
self,
g: impl Fn(GIn) -> FIn + 'static
) -> FBox<GIn, FOut>
pub fn compose<GIn: 'static>(
self,
g: impl Fn(GIn) -> FIn + 'static
) -> FBox<GIn, FOut>
You can compose a function f(x)
inside an FBox
with another function g
. The result is a new function f(g(x))
wrapped in a new FBox
. The output type of g
must match the input type of f
.
Examples
assert_eq!(
10,
FBox::new(|x| x + 1).compose(|x| x * x).apply(3),
);
sourcepub fn and_then<GOut: 'static>(
self,
g: impl Fn(FOut) -> GOut + 'static
) -> FBox<FIn, GOut>
pub fn and_then<GOut: 'static>(
self,
g: impl Fn(FOut) -> GOut + 'static
) -> FBox<FIn, GOut>
Similar to compose
, except the result is g(f(x))
in a new FBox
. The output type of f
must match the input type of g
.
Examples
assert_eq!(
16,
FBox::new(|x| x + 1).and_then(|x| x * x).apply(3),
);
sourcepub fn compose_b<GIn: 'static>(self, other: FBox<GIn, FIn>) -> FBox<GIn, FOut>
pub fn compose_b<GIn: 'static>(self, other: FBox<GIn, FIn>) -> FBox<GIn, FOut>
Similar to compose
, except its argument is another FBox
with a function g
.
Examples
assert_eq!(
10,
FBox::new(|x| x + 1).compose_b(FBox::new(|x| x * x)).apply(3)
);
sourcepub fn and_then_b<GOut: 'static>(
self,
other: FBox<FOut, GOut>
) -> FBox<FIn, GOut>
pub fn and_then_b<GOut: 'static>(
self,
other: FBox<FOut, GOut>
) -> FBox<FIn, GOut>
Similar to and_then
, except its argument is another FBox
with a function g
.
Examples
assert_eq!(
16,
FBox::new(|x| x + 1).and_then_b(FBox::new(|x| x * x)).apply(3)
);