remove AIterative
, extract iterate_mut
This commit is contained in:
parent
b6980cc050
commit
feb5e5daec
51
src/func.rs
51
src/func.rs
@ -23,8 +23,8 @@ pub use std::ops::ControlFlow;
|
|||||||
pub use radn_derive::{CovariantFunctor, SharedFunctor};
|
pub use radn_derive::{CovariantFunctor, SharedFunctor};
|
||||||
|
|
||||||
pub use self::applicative_select::{ApplicativeSelect, Selected};
|
pub use self::applicative_select::{ApplicativeSelect, Selected};
|
||||||
pub use self::controlflow::{AIterative, AIterativeWrapped, Iterative, IterativeWrapped};
|
use self::controlflow::{BindableMut, ControlFlowClass};
|
||||||
use self::controlflow::{ArgumentedIterative, BindableMut, ControlFlowClass};
|
pub use self::controlflow::{Iterative, IterativeWrapped};
|
||||||
|
|
||||||
/// Part of Haskell's `Functor f` responsible for having `f a`.
|
/// Part of Haskell's `Functor f` responsible for having `f a`.
|
||||||
///
|
///
|
||||||
@ -180,39 +180,10 @@ pub trait Monad: Applicative {
|
|||||||
where
|
where
|
||||||
Self: 'a;
|
Self: 'a;
|
||||||
|
|
||||||
/// Simplified, [`FnMut`] version of [`Monad::iterate_argument`].
|
|
||||||
/// Reasoning for this method existing at all is that
|
|
||||||
/// most usecases are better modelled with [`FnMut`]
|
|
||||||
/// rather than some dedicated state type.
|
|
||||||
///
|
|
||||||
/// Note: hypothetically, you can implement [`Monad::iterate_argument`] from [`Monad::iterate_mut`],
|
|
||||||
/// but it's highly discouraged.
|
|
||||||
fn iterate_mut<'a, A: 'a, B: 'a>(
|
|
||||||
a: A,
|
|
||||||
f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
|
||||||
) -> Self::F<'a, B>
|
|
||||||
where
|
|
||||||
Self: 'a,
|
|
||||||
{
|
|
||||||
Self::iterate_argument(a, BindableMut::new(f))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Kleisli category composition special case used to represent loops.
|
|
||||||
/// Included for optimisation and clarity.
|
/// Included for optimisation and clarity.
|
||||||
/// Generally, [`Monad::bind`] should be enough implement it.
|
/// Generally, [`Monad::bind`] should be enough implement it.
|
||||||
/// See [`classes::stackless::StacklessClass::iterate`] for a generic, though less-than ideal, blanket implementation.
|
/// See [`classes::stackless::StacklessClass::iterate`] for a generic, though less-than ideal, blanket implementation.
|
||||||
/// On practice, you generally shouldn't be using [`Monad::bind`]/[`Pure::pure`]/[`Functor::fmap`] here.
|
/// On practice, you generally shouldn't be using [`Monad::bind`]/[`Pure::pure`]/[`Functor::fmap`] here.
|
||||||
fn iterate_argument<'a, A: 'a, B: 'a>(
|
|
||||||
a: A,
|
|
||||||
f: impl AIterative<'a, T = Self, A = A, B = B>,
|
|
||||||
) -> Self::F<'a, B>
|
|
||||||
where
|
|
||||||
Self: 'a,
|
|
||||||
{
|
|
||||||
Self::iterate(ArgumentedIterative(a, f))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Iteration without composition semantics of [`Monad::iterate_argument`].
|
|
||||||
fn iterate<'a, B: 'a>(f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
|
fn iterate<'a, B: 'a>(f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
|
||||||
where
|
where
|
||||||
Self: 'a;
|
Self: 'a;
|
||||||
@ -227,6 +198,24 @@ pub trait Monad: Applicative {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait MonadExt: Monad {
|
||||||
|
/// [`FnMut`] version of [`Monad::iterate`].
|
||||||
|
/// Reasoning for this method existing at all is that
|
||||||
|
/// most usecases are better modelled with [`FnMut`]
|
||||||
|
/// rather than some dedicated state type.
|
||||||
|
fn iterate_mut<'a, A: 'a, B: 'a>(
|
||||||
|
a: A,
|
||||||
|
f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
||||||
|
) -> Self::F<'a, B>
|
||||||
|
where
|
||||||
|
Self: 'a,
|
||||||
|
{
|
||||||
|
Self::iterate(BindableMut::new(a, f))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Monad> MonadExt for T {}
|
||||||
|
|
||||||
/// Part of [`MonadFail`] responsible for Haskell's `fail`.
|
/// Part of [`MonadFail`] responsible for Haskell's `fail`.
|
||||||
pub trait Fail<E>: WeakFunctor {
|
pub trait Fail<E>: WeakFunctor {
|
||||||
/// Equivalent of Haskell's `fail`.
|
/// Equivalent of Haskell's `fail`.
|
||||||
|
@ -119,29 +119,6 @@ impl<U: Monad, V: Monad + LocalFunctor> Monad for CompositionClass<U, V> {
|
|||||||
U::bind(fa, |ua| U::fmap(V::join, V::stuff::<_, U>(V::fmap(f, ua))))
|
U::bind(fa, |ua| U::fmap(V::join, V::stuff::<_, U>(V::fmap(f, ua))))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn iterate_mut<'a, A: 'a, B: 'a>(
|
|
||||||
a: A,
|
|
||||||
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
|
||||||
) -> Self::F<'a, B>
|
|
||||||
where
|
|
||||||
Self: 'a,
|
|
||||||
{
|
|
||||||
U::iterate_mut(a, move |a| {
|
|
||||||
let fstate = f(a);
|
|
||||||
U::fmap(|ustate| V::unstuff(ustate), fstate)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn iterate_argument<'a, A: 'a, B: 'a>(
|
|
||||||
a: A,
|
|
||||||
f: impl AIterative<'a, T = Self, A = A, B = B>,
|
|
||||||
) -> Self::F<'a, B>
|
|
||||||
where
|
|
||||||
Self: 'a,
|
|
||||||
{
|
|
||||||
U::iterate_argument(a, AComposedIterative(f))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn iterate<'a, B: 'a>(f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
|
fn iterate<'a, B: 'a>(f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
|
||||||
where
|
where
|
||||||
Self: 'a,
|
Self: 'a,
|
||||||
@ -158,33 +135,6 @@ impl<U: Monad, V: Monad + LocalFunctor> Monad for CompositionClass<U, V> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct AComposedIterative<F>(F);
|
|
||||||
|
|
||||||
impl<
|
|
||||||
'a,
|
|
||||||
U: 'a + Monad,
|
|
||||||
V: 'a + Monad + LocalFunctor,
|
|
||||||
F: AIterative<'a, T = CompositionClass<U, V>>,
|
|
||||||
> AIterative<'a> for AComposedIterative<F>
|
|
||||||
{
|
|
||||||
type A = F::A;
|
|
||||||
type B = <V as WeakFunctor>::F<'a, F::B>;
|
|
||||||
type T = U;
|
|
||||||
|
|
||||||
fn next(self, a: Self::A) -> AIterativeWrapped<'a, Self> {
|
|
||||||
let fstate = self.0.next(a);
|
|
||||||
U::fmap(
|
|
||||||
|ustate| match V::unstuff(ustate) {
|
|
||||||
ControlFlow::Continue((next_a, next_f)) => {
|
|
||||||
ControlFlow::Continue((next_a, Self(next_f)))
|
|
||||||
}
|
|
||||||
ControlFlow::Break(b) => ControlFlow::Break(b),
|
|
||||||
},
|
|
||||||
fstate,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct ComposedIterative<F>(F);
|
struct ComposedIterative<F>(F);
|
||||||
|
|
||||||
impl<
|
impl<
|
||||||
|
@ -108,40 +108,6 @@ impl Monad for FutureClass {
|
|||||||
Box::pin(async { f(fa.await).await })
|
Box::pin(async { f(fa.await).await })
|
||||||
}
|
}
|
||||||
|
|
||||||
fn iterate_mut<'a, A: 'a, B: 'a>(
|
|
||||||
mut a: A,
|
|
||||||
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
|
||||||
) -> Self::F<'a, B>
|
|
||||||
where
|
|
||||||
Self: 'a,
|
|
||||||
{
|
|
||||||
Box::pin(async move {
|
|
||||||
loop {
|
|
||||||
match f(a).await {
|
|
||||||
ControlFlow::Continue(next_a) => a = next_a,
|
|
||||||
ControlFlow::Break(b) => return b,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn iterate_argument<'a, A: 'a, B: 'a>(
|
|
||||||
mut a: A,
|
|
||||||
mut f: impl AIterative<'a, T = Self, A = A, B = B>,
|
|
||||||
) -> Self::F<'a, B>
|
|
||||||
where
|
|
||||||
Self: 'a,
|
|
||||||
{
|
|
||||||
Box::pin(async move {
|
|
||||||
loop {
|
|
||||||
match f.next(a).await {
|
|
||||||
ControlFlow::Continue((next_a, next_f)) => (a, f) = (next_a, next_f),
|
|
||||||
ControlFlow::Break(b) => return b,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn iterate<'a, B: 'a>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
|
fn iterate<'a, B: 'a>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
|
||||||
where
|
where
|
||||||
Self: 'a,
|
Self: 'a,
|
||||||
|
@ -90,36 +90,6 @@ impl Monad for LazyClass {
|
|||||||
Box::new(|| f(fa())())
|
Box::new(|| f(fa())())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn iterate_mut<'a, A: 'a, B: 'a>(
|
|
||||||
mut a: A,
|
|
||||||
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
|
||||||
) -> Self::F<'a, B>
|
|
||||||
where
|
|
||||||
Self: 'a,
|
|
||||||
{
|
|
||||||
Box::new(move || loop {
|
|
||||||
match f(a)() {
|
|
||||||
ControlFlow::Continue(next_a) => a = next_a,
|
|
||||||
ControlFlow::Break(b) => return b,
|
|
||||||
};
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn iterate_argument<'a, A: 'a, B: 'a>(
|
|
||||||
mut a: A,
|
|
||||||
mut f: impl AIterative<'a, T = Self, A = A, B = B>,
|
|
||||||
) -> Self::F<'a, B>
|
|
||||||
where
|
|
||||||
Self: 'a,
|
|
||||||
{
|
|
||||||
loop {
|
|
||||||
match f.next(a)() {
|
|
||||||
ControlFlow::Continue((next_a, next_f)) => (a, f) = (next_a, next_f),
|
|
||||||
ControlFlow::Break(b) => return Self::pure(b),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn iterate<'a, B: 'a>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
|
fn iterate<'a, B: 'a>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
|
||||||
where
|
where
|
||||||
Self: 'a,
|
Self: 'a,
|
||||||
|
@ -94,36 +94,6 @@ impl Monad for OptionClass {
|
|||||||
f(fa?)
|
f(fa?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn iterate_mut<'a, A: 'a, B: 'a>(
|
|
||||||
mut a: A,
|
|
||||||
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
|
||||||
) -> Self::F<'a, B>
|
|
||||||
where
|
|
||||||
Self: 'a,
|
|
||||||
{
|
|
||||||
loop {
|
|
||||||
match f(a)? {
|
|
||||||
ControlFlow::Continue(next_a) => a = next_a,
|
|
||||||
ControlFlow::Break(b) => return Self::pure(b),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn iterate_argument<'a, A: 'a, B: 'a>(
|
|
||||||
mut a: A,
|
|
||||||
mut f: impl AIterative<'a, T = Self, A = A, B = B>,
|
|
||||||
) -> Self::F<'a, B>
|
|
||||||
where
|
|
||||||
Self: 'a,
|
|
||||||
{
|
|
||||||
loop {
|
|
||||||
match f.next(a)? {
|
|
||||||
ControlFlow::Continue((next_a, next_f)) => (a, f) = (next_a, next_f),
|
|
||||||
ControlFlow::Break(b) => return Self::pure(b),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn iterate<'a, B: 'a>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
|
fn iterate<'a, B: 'a>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
|
||||||
where
|
where
|
||||||
Self: 'a,
|
Self: 'a,
|
||||||
|
@ -110,28 +110,6 @@ impl<F, O> OverloadIterative<F, O> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T: 'a + Monad, O: 'a + DeriveMonad, F: AIterative<'a, T = OverloadClass<T, O>>>
|
|
||||||
AIterative<'a> for OverloadIterative<F, O>
|
|
||||||
{
|
|
||||||
type A = F::A;
|
|
||||||
|
|
||||||
type B = F::B;
|
|
||||||
|
|
||||||
type T = T;
|
|
||||||
|
|
||||||
fn next(self, a: Self::A) -> AIterativeWrapped<'a, Self> {
|
|
||||||
T::fmap(
|
|
||||||
|state| match state {
|
|
||||||
ControlFlow::Continue((next_a, next_f)) => {
|
|
||||||
ControlFlow::Continue((next_a, Self::new(next_f)))
|
|
||||||
}
|
|
||||||
ControlFlow::Break(b) => ControlFlow::Break(b),
|
|
||||||
},
|
|
||||||
self.0.next(a),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, T: 'a + Monad, O: 'a + DeriveMonad, F: Iterative<'a, T = OverloadClass<T, O>>>
|
impl<'a, T: 'a + Monad, O: 'a + DeriveMonad, F: Iterative<'a, T = OverloadClass<T, O>>>
|
||||||
Iterative<'a> for OverloadIterative<F, O>
|
Iterative<'a> for OverloadIterative<F, O>
|
||||||
{
|
{
|
||||||
@ -161,26 +139,6 @@ impl<T: Monad, O: DeriveMonad> Monad for OverloadClass<T, O> {
|
|||||||
T::bind(fa, f)
|
T::bind(fa, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn iterate_mut<'a, A: 'a, B: 'a>(
|
|
||||||
a: A,
|
|
||||||
f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
|
||||||
) -> Self::F<'a, B>
|
|
||||||
where
|
|
||||||
Self: 'a,
|
|
||||||
{
|
|
||||||
T::iterate_mut(a, f)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn iterate_argument<'a, A: 'a, B: 'a>(
|
|
||||||
a: A,
|
|
||||||
f: impl AIterative<'a, T = Self, A = A, B = B>,
|
|
||||||
) -> Self::F<'a, B>
|
|
||||||
where
|
|
||||||
Self: 'a,
|
|
||||||
{
|
|
||||||
T::iterate_argument(a, OverloadIterative::new(f))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn iterate<'a, B: 'a>(f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
|
fn iterate<'a, B: 'a>(f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
|
||||||
where
|
where
|
||||||
Self: 'a,
|
Self: 'a,
|
||||||
|
@ -115,36 +115,6 @@ impl<E> Monad for ResultClass<E> {
|
|||||||
f(fa?)
|
f(fa?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn iterate_mut<'a, A: 'a, B: 'a>(
|
|
||||||
mut a: A,
|
|
||||||
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
|
||||||
) -> Self::F<'a, B>
|
|
||||||
where
|
|
||||||
Self: 'a,
|
|
||||||
{
|
|
||||||
loop {
|
|
||||||
match f(a)? {
|
|
||||||
ControlFlow::Continue(next_a) => a = next_a,
|
|
||||||
ControlFlow::Break(b) => return Self::pure(b),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn iterate_argument<'a, A: 'a, B: 'a>(
|
|
||||||
mut a: A,
|
|
||||||
mut f: impl AIterative<'a, T = Self, A = A, B = B>,
|
|
||||||
) -> Self::F<'a, B>
|
|
||||||
where
|
|
||||||
Self: 'a,
|
|
||||||
{
|
|
||||||
loop {
|
|
||||||
match f.next(a)? {
|
|
||||||
ControlFlow::Continue((next_a, next_f)) => (a, f) = (next_a, next_f),
|
|
||||||
ControlFlow::Break(b) => return Self::pure(b),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn iterate<'a, B: 'a>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
|
fn iterate<'a, B: 'a>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
|
||||||
where
|
where
|
||||||
Self: 'a,
|
Self: 'a,
|
||||||
|
@ -82,36 +82,6 @@ impl Monad for SoloClass {
|
|||||||
f(fa)
|
f(fa)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn iterate_mut<'a, A: 'a, B: 'a>(
|
|
||||||
mut a: A,
|
|
||||||
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
|
||||||
) -> Self::F<'a, B>
|
|
||||||
where
|
|
||||||
Self: 'a,
|
|
||||||
{
|
|
||||||
loop {
|
|
||||||
match f(a) {
|
|
||||||
ControlFlow::Continue(next_a) => a = next_a,
|
|
||||||
ControlFlow::Break(b) => return b,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn iterate_argument<'a, A: 'a, B: 'a>(
|
|
||||||
mut a: A,
|
|
||||||
mut f: impl AIterative<'a, T = Self, A = A, B = B>,
|
|
||||||
) -> Self::F<'a, B>
|
|
||||||
where
|
|
||||||
Self: 'a,
|
|
||||||
{
|
|
||||||
loop {
|
|
||||||
match f.next(a) {
|
|
||||||
ControlFlow::Continue((new_a, new_f)) => (a, f) = (new_a, new_f),
|
|
||||||
ControlFlow::Break(b) => return b,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn iterate<'a, B: 'a>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
|
fn iterate<'a, B: 'a>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
|
||||||
where
|
where
|
||||||
Self: 'a,
|
Self: 'a,
|
||||||
|
@ -226,36 +226,6 @@ impl Monad for StacklessClass {
|
|||||||
fa.bind(f)
|
fa.bind(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn iterate_mut<'a, A: 'a, B: 'a>(
|
|
||||||
a: A,
|
|
||||||
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
|
||||||
) -> Self::F<'a, B>
|
|
||||||
where
|
|
||||||
Self: 'a,
|
|
||||||
{
|
|
||||||
Self::pure(a).bind(move |a| {
|
|
||||||
f(a).bind(|state| match state {
|
|
||||||
ControlFlow::Continue(next_a) => Self::iterate_mut(next_a, f),
|
|
||||||
ControlFlow::Break(b) => Self::pure(b),
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn iterate_argument<'a, A: 'a, B: 'a>(
|
|
||||||
a: A,
|
|
||||||
f: impl AIterative<'a, T = Self, A = A, B = B>,
|
|
||||||
) -> Self::F<'a, B>
|
|
||||||
where
|
|
||||||
Self: 'a,
|
|
||||||
{
|
|
||||||
Self::pure(a).bind(move |a| {
|
|
||||||
f.next(a).bind(|state| match state {
|
|
||||||
ControlFlow::Continue((next_a, next_f)) => Self::iterate_argument(next_a, next_f),
|
|
||||||
ControlFlow::Break(b) => Self::pure(b),
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn iterate<'a, B: 'a>(f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
|
fn iterate<'a, B: 'a>(f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
|
||||||
where
|
where
|
||||||
Self: 'a,
|
Self: 'a,
|
||||||
|
@ -129,40 +129,6 @@ impl<E> Monad for TryFutureClass<E> {
|
|||||||
Box::pin(async { f(fa.await?).await })
|
Box::pin(async { f(fa.await?).await })
|
||||||
}
|
}
|
||||||
|
|
||||||
fn iterate_mut<'a, A: 'a, B: 'a>(
|
|
||||||
mut a: A,
|
|
||||||
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
|
||||||
) -> Self::F<'a, B>
|
|
||||||
where
|
|
||||||
Self: 'a,
|
|
||||||
{
|
|
||||||
Box::pin(async move {
|
|
||||||
loop {
|
|
||||||
match f(a).await? {
|
|
||||||
ControlFlow::Continue(next_a) => a = next_a,
|
|
||||||
ControlFlow::Break(b) => return Ok(b),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn iterate_argument<'a, A: 'a, B: 'a>(
|
|
||||||
mut a: A,
|
|
||||||
mut f: impl AIterative<'a, T = Self, A = A, B = B>,
|
|
||||||
) -> Self::F<'a, B>
|
|
||||||
where
|
|
||||||
Self: 'a,
|
|
||||||
{
|
|
||||||
Box::pin(async move {
|
|
||||||
loop {
|
|
||||||
match f.next(a).await? {
|
|
||||||
ControlFlow::Continue((next_a, next_f)) => (a, f) = (next_a, next_f),
|
|
||||||
ControlFlow::Break(b) => return Ok(b),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn iterate<'a, B: 'a>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
|
fn iterate<'a, B: 'a>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
|
||||||
where
|
where
|
||||||
Self: 'a,
|
Self: 'a,
|
||||||
|
@ -31,27 +31,7 @@ impl<C> Pure for ControlFlowClass<C> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Next [AIterative] state, wrapped.
|
pub struct BindableMut<T: ?Sized, A, B, F>(A, F, PhantomData<B>, PhantomData<T>);
|
||||||
pub type AIterativeWrapped<'a, F> = <<F as AIterative<'a>>::T as WeakFunctor>::F<
|
|
||||||
'a,
|
|
||||||
ControlFlow<<F as AIterative<'a>>::B, (<F as AIterative<'a>>::A, F)>,
|
|
||||||
>;
|
|
||||||
|
|
||||||
/// Value passed to [`Monad::iterate_argument`].
|
|
||||||
/// `A` prefix meaning "with an **a**rgument" given current implementation
|
|
||||||
/// relying on the argument getting passed around.
|
|
||||||
pub trait AIterative<'a>: 'a + Sized {
|
|
||||||
/// [`ControlFlow::Continue`].
|
|
||||||
type A: 'a;
|
|
||||||
/// [`ControlFlow::Break`].
|
|
||||||
type B: 'a;
|
|
||||||
/// Corresponding [`WeakFunctor`].
|
|
||||||
type T: 'a + ?Sized + WeakFunctor;
|
|
||||||
/// Get next state.
|
|
||||||
fn next(self, a: Self::A) -> AIterativeWrapped<'a, Self>;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct BindableMut<T: ?Sized, A, B, F>(F, PhantomData<A>, PhantomData<B>, PhantomData<T>);
|
|
||||||
|
|
||||||
impl<
|
impl<
|
||||||
'a,
|
'a,
|
||||||
@ -61,8 +41,8 @@ impl<
|
|||||||
F: 'a + FnMut(A) -> T::F<'a, ControlFlow<B, A>>,
|
F: 'a + FnMut(A) -> T::F<'a, ControlFlow<B, A>>,
|
||||||
> BindableMut<T, A, B, F>
|
> BindableMut<T, A, B, F>
|
||||||
{
|
{
|
||||||
pub fn new(f: F) -> Self {
|
pub fn new(a: A, f: F) -> Self {
|
||||||
BindableMut(f, PhantomData, PhantomData, PhantomData)
|
BindableMut(a, f, PhantomData, PhantomData)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,20 +52,21 @@ impl<
|
|||||||
A: 'a,
|
A: 'a,
|
||||||
B: 'a,
|
B: 'a,
|
||||||
F: 'a + FnMut(A) -> T::F<'a, ControlFlow<B, A>>,
|
F: 'a + FnMut(A) -> T::F<'a, ControlFlow<B, A>>,
|
||||||
> AIterative<'a> for BindableMut<T, A, B, F>
|
> Iterative<'a> for BindableMut<T, A, B, F>
|
||||||
{
|
{
|
||||||
type A = A;
|
|
||||||
type B = B;
|
type B = B;
|
||||||
type T = T;
|
type T = T;
|
||||||
|
|
||||||
fn next(mut self, a: Self::A) -> AIterativeWrapped<'a, Self> {
|
fn next(mut self) -> IterativeWrapped<'a, Self> {
|
||||||
let fa = self.0(a);
|
let fstate = self.1(self.0);
|
||||||
T::fmap(
|
T::fmap(
|
||||||
move |state| match state {
|
move |state| match state {
|
||||||
ControlFlow::Continue(next_a) => ControlFlow::Continue((next_a, self)),
|
ControlFlow::Continue(next_a) => {
|
||||||
|
ControlFlow::Continue(BindableMut::new(next_a, self.1))
|
||||||
|
}
|
||||||
ControlFlow::Break(b) => ControlFlow::Break(b),
|
ControlFlow::Break(b) => ControlFlow::Break(b),
|
||||||
},
|
},
|
||||||
fa,
|
fstate,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -103,26 +84,3 @@ pub trait Iterative<'a>: 'a + Sized {
|
|||||||
/// Get next state.
|
/// Get next state.
|
||||||
fn next(self) -> IterativeWrapped<'a, Self>;
|
fn next(self) -> IterativeWrapped<'a, Self>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ArgumentedIterative<A, F>(pub A, pub F);
|
|
||||||
|
|
||||||
impl<'a, A: 'a, F: AIterative<'a, A = A>> Iterative<'a> for ArgumentedIterative<A, F>
|
|
||||||
where
|
|
||||||
F::T: Functor,
|
|
||||||
{
|
|
||||||
type B = F::B;
|
|
||||||
|
|
||||||
type T = F::T;
|
|
||||||
|
|
||||||
fn next(self) -> IterativeWrapped<'a, Self> {
|
|
||||||
Self::T::fmap(
|
|
||||||
|state| match state {
|
|
||||||
ControlFlow::Continue((next_a, next_f)) => {
|
|
||||||
ControlFlow::Continue(ArgumentedIterative(next_a, next_f))
|
|
||||||
}
|
|
||||||
ControlFlow::Break(b) => ControlFlow::Break(b),
|
|
||||||
},
|
|
||||||
self.1.next(self.0),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user