#[fp_macros::document_module]
mod inner {
use {
crate::{
Apply,
brands::OptionBrand,
classes::{
Alt,
Applicative,
ApplyFirst,
ApplySecond,
CloneableFn,
Compactable,
Filterable,
Foldable,
Functor,
Lift,
MonadRec,
Monoid,
Plus,
Pointed,
Semiapplicative,
Semimonad,
Traversable,
Witherable,
foldable_with_index::FoldableWithIndex,
functor_with_index::FunctorWithIndex,
traversable_with_index::TraversableWithIndex,
with_index::WithIndex,
},
impl_kind,
kinds::*,
},
core::ops::ControlFlow,
fp_macros::*,
};
impl_kind! {
for OptionBrand {
type Of<'a, A: 'a>: 'a = Option<A>;
}
}
impl Functor for OptionBrand {
#[document_signature]
#[document_type_parameters(
"The lifetime of the value.",
"The type of the value inside the option.",
"The type of the result of applying the function."
)]
#[document_parameters("The function to apply to the value.", "The option to map over.")]
#[document_returns(
"A new option containing the result of applying the function, or `None`."
)]
#[document_examples]
fn map<'a, A: 'a, B: 'a>(
func: impl Fn(A) -> B + 'a,
fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
fa.map(func)
}
}
impl Lift for OptionBrand {
#[document_signature]
#[document_type_parameters(
"The lifetime of the values.",
"The type of the first option's value.",
"The type of the second option's value.",
"The return type of the function."
)]
#[document_parameters(
"The binary function to apply.",
"The first option.",
"The second option."
)]
#[document_returns("`Some(f(a, b))` if both options are `Some`, otherwise `None`.")]
#[document_examples]
fn lift2<'a, A, B, C>(
func: impl Fn(A, B) -> C + 'a,
fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>)
where
A: 'a,
B: 'a,
C: 'a, {
fa.zip(fb).map(|(a, b)| func(a, b))
}
}
impl Pointed for OptionBrand {
#[document_signature]
#[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
#[document_parameters("The value to wrap.")]
#[document_returns("`Some(a)`.")]
#[document_examples]
fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
Some(a)
}
}
impl ApplyFirst for OptionBrand {}
impl ApplySecond for OptionBrand {}
impl Semiapplicative for OptionBrand {
#[document_signature]
#[document_type_parameters(
"The lifetime of the values.",
"The brand of the cloneable function wrapper.",
"The type of the input value.",
"The type of the output value."
)]
#[document_parameters(
"The option containing the function.",
"The option containing the value."
)]
#[document_returns("`Some(f(a))` if both are `Some`, otherwise `None`.")]
#[document_examples]
fn apply<'a, FnBrand: 'a + CloneableFn, A: 'a + Clone, B: 'a>(
ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneableFn>::Of<'a, A, B>>),
fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
match (ff, fa) {
(Some(f), Some(a)) => Some(f(a)),
_ => None,
}
}
}
impl Semimonad for OptionBrand {
#[document_signature]
#[document_type_parameters(
"The lifetime of the values.",
"The type of the result of the first computation.",
"The type of the result of the second computation."
)]
#[document_parameters(
"The first option.",
"The function to apply to the value inside the option."
)]
#[document_returns(
"The result of applying `f` to the value if `ma` is `Some`, otherwise `None`."
)]
#[document_examples]
fn bind<'a, A: 'a, B: 'a>(
ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
func: impl Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
ma.and_then(func)
}
}
impl Alt for OptionBrand {
#[document_signature]
#[document_type_parameters("The lifetime of the values.", "The type of the value.")]
#[document_parameters("The first option.", "The second option.")]
#[document_returns("The first `Some` value, or `None`.")]
#[document_examples]
fn alt<'a, A: 'a>(
fa1: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
fa2: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
fa1.or(fa2)
}
}
impl Plus for OptionBrand {
#[document_signature]
#[document_type_parameters("The lifetime of the value.", "The type of the value.")]
#[document_returns("`None`.")]
#[document_examples]
fn empty<'a, A: 'a>() -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
None
}
}
impl Foldable for OptionBrand {
#[document_signature]
#[document_type_parameters(
"The lifetime of the values.",
"The brand of the cloneable function to use.",
"The type of the elements in the structure.",
"The type of the accumulator."
)]
#[document_parameters("The folding function.", "The initial value.", "The option to fold.")]
#[document_returns("`func(a, initial)` if `fa` is `Some(a)`, otherwise `initial`.")]
#[document_examples]
fn fold_right<'a, FnBrand, A: 'a + Clone, B: 'a>(
func: impl Fn(A, B) -> B + 'a,
initial: B,
fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> B
where
FnBrand: CloneableFn + 'a, {
match fa {
Some(a) => func(a, initial),
None => initial,
}
}
#[document_signature]
#[document_type_parameters(
"The lifetime of the values.",
"The brand of the cloneable function to use.",
"The type of the elements in the structure.",
"The type of the accumulator."
)]
#[document_parameters(
"The function to apply to the accumulator and each element.",
"The initial value of the accumulator.",
"The option to fold."
)]
#[document_returns("`f(initial, a)` if `fa` is `Some(a)`, otherwise `initial`.")]
#[document_examples]
fn fold_left<'a, FnBrand, A: 'a + Clone, B: 'a>(
func: impl Fn(B, A) -> B + 'a,
initial: B,
fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> B
where
FnBrand: CloneableFn + 'a, {
match fa {
Some(a) => func(initial, a),
None => initial,
}
}
#[document_signature]
#[document_type_parameters(
"The lifetime of the values.",
"The brand of the cloneable function to use.",
"The type of the elements in the structure.",
"The type of the monoid."
)]
#[document_parameters("The mapping function.", "The option to fold.")]
#[document_returns("`func(a)` if `fa` is `Some(a)`, otherwise `M::empty()`.")]
#[document_examples]
fn fold_map<'a, FnBrand, A: 'a + Clone, M>(
func: impl Fn(A) -> M + 'a,
fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> M
where
M: Monoid + 'a,
FnBrand: CloneableFn + 'a, {
match fa {
Some(a) => func(a),
None => M::empty(),
}
}
}
impl Traversable for OptionBrand {
#[document_signature]
#[document_type_parameters(
"The lifetime of the values.",
"The type of the elements in the traversable structure.",
"The type of the elements in the resulting traversable structure.",
"The applicative context."
)]
#[document_parameters(
"The function to apply to each element, returning a value in an applicative context.",
"The option to traverse."
)]
#[document_returns("The option wrapped in the applicative context.")]
#[document_examples]
fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
func: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)>)
where
Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
match ta {
Some(a) => F::map(|b| Some(b), func(a)),
None => F::pure(None),
}
}
#[document_signature]
#[document_type_parameters(
"The lifetime of the values.",
"The type of the elements in the traversable structure.",
"The applicative context."
)]
#[document_parameters("The option containing the applicative value.")]
#[document_returns("The result of the traversal.")]
#[document_examples]
fn sequence<'a, A: 'a + Clone, F: Applicative>(
ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)>)
) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)>)
where
Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone, {
match ta {
Some(fa) => F::map(|a| Some(a), fa),
None => F::pure(None),
}
}
}
impl WithIndex for OptionBrand {
type Index = ();
}
impl FunctorWithIndex for OptionBrand {
#[document_signature]
#[document_type_parameters(
"The lifetime of the value.",
"The type of the value inside the option.",
"The type of the result of applying the function."
)]
#[document_parameters(
"The function to apply to the value and its index.",
"The option to map over."
)]
#[document_returns(
"A new option containing the result of applying the function, or `None`."
)]
#[document_examples]
fn map_with_index<'a, A: 'a, B: 'a>(
f: impl Fn((), A) -> B + 'a,
fa: Option<A>,
) -> Option<B> {
fa.map(|a| f((), a))
}
}
impl FoldableWithIndex for OptionBrand {
#[document_signature]
#[document_type_parameters(
"The lifetime of the value.",
"The type of the value inside the option.",
"The monoid type."
)]
#[document_parameters(
"The function to apply to the value and its index.",
"The option to fold."
)]
#[document_returns("The monoid value.")]
#[document_examples]
fn fold_map_with_index<'a, A: 'a + Clone, R: Monoid>(
f: impl Fn((), A) -> R + 'a,
fa: Option<A>,
) -> R {
match fa {
Some(a) => f((), a),
None => R::empty(),
}
}
}
impl TraversableWithIndex for OptionBrand {
#[document_signature]
#[document_type_parameters(
"The lifetime of the value.",
"The type of the value inside the option.",
"The type of the result.",
"The applicative context."
)]
#[document_parameters(
"The function to apply to the value and its index, returning a value in an applicative context.",
"The option to traverse."
)]
#[document_returns("The option wrapped in the applicative context.")]
#[document_examples]
fn traverse_with_index<'a, A: 'a, B: 'a + Clone, M: Applicative>(
f: impl Fn((), A) -> M::Of<'a, B> + 'a,
ta: Option<A>,
) -> M::Of<'a, Option<B>> {
match ta {
Some(a) => M::map(|b| Some(b), f((), a)),
None => M::pure(None),
}
}
}
impl Compactable for OptionBrand {
#[document_signature]
#[document_type_parameters("The lifetime of the values.", "The type of the elements.")]
#[document_parameters("The nested option.")]
#[document_returns("The flattened option.")]
#[document_examples]
fn compact<'a, A: 'a>(
fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<
'a,
Apply!(<OptionBrand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
>)
) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
fa.flatten()
}
#[document_signature]
#[document_type_parameters(
"The lifetime of the values.",
"The type of the error value.",
"The type of the success value."
)]
#[document_parameters("The option of result.")]
#[document_returns("A pair of options.")]
#[document_examples]
fn separate<'a, E: 'a, O: 'a>(
fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Result<O, E>>)
) -> (
Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>),
Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, O>),
) {
match fa {
Some(Ok(o)) => (None, Some(o)),
Some(Err(e)) => (Some(e), None),
None => (None, None),
}
}
}
impl Filterable for OptionBrand {
#[document_signature]
#[document_type_parameters(
"The lifetime of the values.",
"The type of the input value.",
"The type of the error value.",
"The type of the success value."
)]
#[document_parameters("The function to apply.", "The option to partition.")]
#[document_returns("A pair of options.")]
#[document_examples]
fn partition_map<'a, A: 'a, E: 'a, O: 'a>(
func: impl Fn(A) -> Result<O, E> + 'a,
fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> (
Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>),
Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, O>),
) {
match fa {
Some(a) => match func(a) {
Ok(o) => (None, Some(o)),
Err(e) => (Some(e), None),
},
None => (None, None),
}
}
#[document_signature]
#[document_type_parameters("The lifetime of the values.", "The type of the elements.")]
#[document_parameters("The predicate.", "The option to partition.")]
#[document_returns("A pair of options.")]
#[document_examples]
fn partition<'a, A: 'a + Clone>(
func: impl Fn(A) -> bool + 'a,
fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> (
Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) {
match fa {
Some(a) =>
if func(a.clone()) {
(None, Some(a))
} else {
(Some(a), None)
},
None => (None, None),
}
}
#[document_signature]
#[document_type_parameters(
"The lifetime of the values.",
"The type of the input value.",
"The type of the result of applying the function."
)]
#[document_parameters("The function to apply.", "The option to filter and map.")]
#[document_returns("The filtered and mapped option.")]
#[document_examples]
fn filter_map<'a, A: 'a, B: 'a>(
func: impl Fn(A) -> Option<B> + 'a,
fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
fa.and_then(func)
}
#[document_signature]
#[document_type_parameters("The lifetime of the values.", "The type of the elements.")]
#[document_parameters("The predicate.", "The option to filter.")]
#[document_returns("The filtered option.")]
#[document_examples]
fn filter<'a, A: 'a + Clone>(
func: impl Fn(A) -> bool + 'a,
fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
fa.filter(|a| func(a.clone()))
}
}
impl Witherable for OptionBrand {
#[document_signature]
#[document_type_parameters(
"The lifetime of the values.",
"The applicative context.",
"The type of the elements in the input structure.",
"The type of the error values.",
"The type of the success values."
)]
#[document_parameters(
"The function to apply to each element, returning a `Result` in an applicative context.",
"The option to partition."
)]
#[document_returns("The partitioned option wrapped in the applicative context.")]
#[document_examples]
fn wilt<'a, M: Applicative, A: 'a + Clone, E: 'a + Clone, O: 'a + Clone>(
func: impl Fn(A) -> Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Result<O, E>>)
+ 'a,
ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<
'a,
(
Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>),
Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, O>),
),
>)
where
Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Result<O, E>>): Clone,
Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Result<O, E>>): Clone, {
match ta {
Some(a) => M::map(
|res| match res {
Ok(o) => (None, Some(o)),
Err(e) => (Some(e), None),
},
func(a),
),
None => M::pure((None, None)),
}
}
#[document_signature]
#[document_type_parameters(
"The lifetime of the values.",
"The applicative context.",
"The type of the elements in the input structure.",
"The type of the result of applying the function."
)]
#[document_parameters(
"The function to apply to each element, returning an `Option` in an applicative context.",
"The option to filter and map."
)]
#[document_returns("The filtered and mapped option wrapped in the applicative context.")]
#[document_examples]
fn wither<'a, M: Applicative, A: 'a + Clone, B: 'a + Clone>(
func: impl Fn(A) -> Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Option<B>>) + 'a,
ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<
'a,
Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
>)
where
Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Option<B>>): Clone,
Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Option<B>>): Clone, {
match ta {
Some(a) => func(a),
None => M::pure(None),
}
}
}
impl MonadRec for OptionBrand {
#[document_signature]
#[document_type_parameters(
"The lifetime of the computation.",
"The type of the initial value and loop state.",
"The type of the result."
)]
#[document_parameters("The step function.", "The initial value.")]
#[document_returns(
"The result of the computation, or `None` if the step function returned `None`."
)]
#[document_examples]
fn tail_rec_m<'a, A: 'a, B: 'a>(
func: impl Fn(
A,
)
-> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, ControlFlow<B, A>>)
+ 'a,
initial: A,
) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
let mut current = initial;
loop {
match func(current) {
None => return None,
Some(ControlFlow::Continue(next)) => current = next,
Some(ControlFlow::Break(b)) => return Some(b),
}
}
}
}
}
#[cfg(test)]
mod tests {
use {
crate::{
brands::*,
classes::CloneableFn,
functions::*,
},
quickcheck_macros::quickcheck,
};
#[quickcheck]
fn functor_identity(x: Option<i32>) -> bool {
map::<OptionBrand, _, _>(identity, x) == x
}
#[quickcheck]
fn functor_composition(x: Option<i32>) -> bool {
let f = |x: i32| x.wrapping_add(1);
let g = |x: i32| x.wrapping_mul(2);
map::<OptionBrand, _, _>(compose(f, g), x)
== map::<OptionBrand, _, _>(f, map::<OptionBrand, _, _>(g, x))
}
#[quickcheck]
fn applicative_identity(v: Option<i32>) -> bool {
apply::<RcFnBrand, OptionBrand, _, _>(
pure::<OptionBrand, _>(<RcFnBrand as CloneableFn>::new(identity)),
v,
) == v
}
#[quickcheck]
fn applicative_homomorphism(x: i32) -> bool {
let f = |x: i32| x.wrapping_mul(2);
apply::<RcFnBrand, OptionBrand, _, _>(
pure::<OptionBrand, _>(<RcFnBrand as CloneableFn>::new(f)),
pure::<OptionBrand, _>(x),
) == pure::<OptionBrand, _>(f(x))
}
#[quickcheck]
fn applicative_composition(
w: Option<i32>,
u_is_some: bool,
v_is_some: bool,
) -> bool {
let v_fn = |x: i32| x.wrapping_mul(2);
let u_fn = |x: i32| x.wrapping_add(1);
let v = if v_is_some {
pure::<OptionBrand, _>(<RcFnBrand as CloneableFn>::new(v_fn))
} else {
None
};
let u = if u_is_some {
pure::<OptionBrand, _>(<RcFnBrand as CloneableFn>::new(u_fn))
} else {
None
};
let vw = apply::<RcFnBrand, OptionBrand, _, _>(v.clone(), w);
let rhs = apply::<RcFnBrand, OptionBrand, _, _>(u.clone(), vw);
let uv = match (u, v) {
(Some(uf), Some(vf)) => {
let composed = move |x| uf(vf(x));
Some(<RcFnBrand as CloneableFn>::new(composed))
}
_ => None,
};
let lhs = apply::<RcFnBrand, OptionBrand, _, _>(uv, w);
lhs == rhs
}
#[quickcheck]
fn applicative_interchange(y: i32) -> bool {
let f = |x: i32| x.wrapping_mul(2);
let u = pure::<OptionBrand, _>(<RcFnBrand as CloneableFn>::new(f));
let lhs = apply::<RcFnBrand, OptionBrand, _, _>(u.clone(), pure::<OptionBrand, _>(y));
let rhs_fn =
<RcFnBrand as CloneableFn>::new(move |f: std::rc::Rc<dyn Fn(i32) -> i32>| f(y));
let rhs = apply::<RcFnBrand, OptionBrand, _, _>(pure::<OptionBrand, _>(rhs_fn), u);
lhs == rhs
}
#[quickcheck]
fn monad_left_identity(a: i32) -> bool {
let f = |x: i32| Some(x.wrapping_mul(2));
bind::<OptionBrand, _, _>(pure::<OptionBrand, _>(a), f) == f(a)
}
#[quickcheck]
fn monad_right_identity(m: Option<i32>) -> bool {
bind::<OptionBrand, _, _>(m, pure::<OptionBrand, _>) == m
}
#[quickcheck]
fn monad_associativity(m: Option<i32>) -> bool {
let f = |x: i32| Some(x.wrapping_mul(2));
let g = |x: i32| Some(x.wrapping_add(1));
bind::<OptionBrand, _, _>(bind::<OptionBrand, _, _>(m, f), g)
== bind::<OptionBrand, _, _>(m, |x| bind::<OptionBrand, _, _>(f(x), g))
}
#[test]
fn map_none() {
assert_eq!(map::<OptionBrand, _, _>(|x: i32| x + 1, None), None);
}
#[test]
fn bind_none() {
assert_eq!(bind::<OptionBrand, _, _>(None, |x: i32| Some(x + 1)), None);
}
#[test]
fn bind_returning_none() {
assert_eq!(bind::<OptionBrand, _, _>(Some(5), |_| None::<i32>), None);
}
#[test]
fn fold_right_none() {
assert_eq!(
crate::classes::foldable::fold_right::<RcFnBrand, OptionBrand, _, _>(
|x: i32, acc| x + acc,
0,
None
),
0
);
}
#[test]
fn fold_left_none() {
assert_eq!(
crate::classes::foldable::fold_left::<RcFnBrand, OptionBrand, _, _>(
|acc, x: i32| acc + x,
0,
None
),
0
);
}
#[test]
fn traverse_none() {
assert_eq!(
crate::classes::traversable::traverse::<OptionBrand, _, _, OptionBrand>(
|x: i32| Some(x + 1),
None
),
Some(None)
);
}
#[test]
fn traverse_returning_none() {
assert_eq!(
crate::classes::traversable::traverse::<OptionBrand, _, _, OptionBrand>(
|_: i32| None::<i32>,
Some(5)
),
None
);
}
#[quickcheck]
fn monad_rec_identity(x: i32) -> bool {
use {
crate::classes::monad_rec::tail_rec_m,
core::ops::ControlFlow,
};
tail_rec_m::<OptionBrand, _, _>(|a| Some(ControlFlow::Break(a)), x) == Some(x)
}
#[test]
fn monad_rec_sum_range() {
use {
crate::classes::monad_rec::tail_rec_m,
core::ops::ControlFlow,
};
let result = tail_rec_m::<OptionBrand, _, _>(
|(n, acc)| {
if n == 0 {
Some(ControlFlow::Break(acc))
} else {
Some(ControlFlow::Continue((n - 1, acc + n)))
}
},
(100i64, 0i64),
);
assert_eq!(result, Some(5050));
}
#[test]
fn monad_rec_short_circuit() {
use {
crate::classes::monad_rec::tail_rec_m,
core::ops::ControlFlow,
};
let result: Option<i32> = tail_rec_m::<OptionBrand, _, _>(
|n| {
if n == 5 { None } else { Some(ControlFlow::Continue(n + 1)) }
},
0,
);
assert_eq!(result, None);
}
#[test]
fn monad_rec_stack_safety() {
use {
crate::classes::monad_rec::tail_rec_m,
core::ops::ControlFlow,
};
let iterations: i64 = 200_000;
let result = tail_rec_m::<OptionBrand, _, _>(
|acc| {
if acc < iterations {
Some(ControlFlow::Continue(acc + 1))
} else {
Some(ControlFlow::Break(acc))
}
},
0i64,
);
assert_eq!(result, Some(iterations));
}
}