flow docs
This commit is contained in:
parent
f776bf05ca
commit
8d74cd6f3f
@ -1 +1,3 @@
|
|||||||
|
//! Data structures and algorithms, independent of [crate::core] concepts.
|
||||||
|
|
||||||
pub mod traversible;
|
pub mod traversible;
|
||||||
|
@ -1,9 +1,14 @@
|
|||||||
|
//! Traversible binary trees.
|
||||||
|
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use crate::func::*;
|
use crate::func::*;
|
||||||
|
|
||||||
|
/// Result returned by [`Comparator::pick_smaller`].
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum Comparison {
|
pub enum Comparison {
|
||||||
L,
|
L,
|
||||||
|
/// Compared values were equal.
|
||||||
E,
|
E,
|
||||||
R,
|
R,
|
||||||
}
|
}
|
||||||
@ -12,10 +17,33 @@ fn and(left: bool, right: bool) -> bool {
|
|||||||
left && right
|
left && right
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns [`Comparison`] saying which value is smaller.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use radn_rs::flow::traversible::*;
|
||||||
|
/// assert_eq!(DefaultComparator.pick_smaller(&1, &3), Comparison::L);
|
||||||
|
/// assert_eq!(DefaultComparator.pick_smaller(&2, &2), Comparison::E);
|
||||||
|
/// assert_eq!(DefaultComparator.pick_smaller(&3, &1), Comparison::R);
|
||||||
|
/// ```
|
||||||
pub trait Comparator<A> {
|
pub trait Comparator<A> {
|
||||||
fn pick_smaller(&self, kl: &A, kr: &A) -> Comparison;
|
fn pick_smaller(&self, kl: &A, kr: &A) -> Comparison;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Implementation of a [Comparator] relying on [`PartialOrd`].
|
||||||
|
pub struct DefaultComparator;
|
||||||
|
|
||||||
|
impl<A: PartialOrd> Comparator<A> for DefaultComparator {
|
||||||
|
fn pick_smaller(&self, kl: &A, kr: &A) -> Comparison {
|
||||||
|
if kl < kr {
|
||||||
|
Comparison::L
|
||||||
|
} else if kr < kl {
|
||||||
|
Comparison::R
|
||||||
|
} else {
|
||||||
|
Comparison::E
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub type Split<'a, T, A, D> = (
|
pub type Split<'a, T, A, D> = (
|
||||||
Rc<dyn TraversibleBinaryTree<'a, T, A, D>>,
|
Rc<dyn TraversibleBinaryTree<'a, T, A, D>>,
|
||||||
Rc<dyn TraversibleBinaryTree<'a, T, A, D>>,
|
Rc<dyn TraversibleBinaryTree<'a, T, A, D>>,
|
||||||
|
@ -20,6 +20,7 @@ pub mod tests;
|
|||||||
///
|
///
|
||||||
/// <https://hackage.haskell.org/package/base-4.18.0.0/docs/Data-Functor.html>
|
/// <https://hackage.haskell.org/package/base-4.18.0.0/docs/Data-Functor.html>
|
||||||
pub trait WeakFunctor {
|
pub trait WeakFunctor {
|
||||||
|
/// Type of the wrapped value.
|
||||||
type F<'a, A: 'a>: 'a
|
type F<'a, A: 'a>: 'a
|
||||||
where
|
where
|
||||||
Self: 'a;
|
Self: 'a;
|
||||||
@ -233,10 +234,15 @@ pub trait LocalFunctor: WeakFunctor {
|
|||||||
Self: 'a;
|
Self: 'a;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Represents a (collection of) [Monad]\(s) that can hold any type of error.
|
||||||
pub trait MonadFailAny {
|
pub trait MonadFailAny {
|
||||||
|
/// [`MonadFail`] for a specific error type.
|
||||||
type W<E>: MonadFail<E>;
|
type W<E>: MonadFail<E>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Represents a (collection of) [Monad]\(s),
|
||||||
|
/// wrapped values of which are interchangeable with another [Monad]'s
|
||||||
|
/// wrapped [Result]s.
|
||||||
pub trait MonadFailOver<T: Monad>: MonadFailAny {
|
pub trait MonadFailOver<T: Monad>: MonadFailAny {
|
||||||
fn unstuff<'a, A: 'a, E: 'a>(
|
fn unstuff<'a, A: 'a, E: 'a>(
|
||||||
wa: <Self::W<E> as WeakFunctor>::F<'a, A>,
|
wa: <Self::W<E> as WeakFunctor>::F<'a, A>,
|
||||||
|
10
src/std.rs
10
src/std.rs
@ -36,13 +36,19 @@ impl<S: Serializable> ExtSerializable for S {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait ExtContext: Context {
|
/// Extention trait for simpler conversion between [`Context::T`] and [`Context::Fallible`].
|
||||||
|
///
|
||||||
|
/// Until either Rust type system or [`crate::func`] take serious changes,
|
||||||
|
/// this is the preferred way to switch between [Wrapped] and [fallible].
|
||||||
|
pub trait FallibleContext: Context {
|
||||||
|
/// Convert a fallible wrapped into a wrapped result.
|
||||||
fn unstuff<'a, A: 'a, E: 'a>(
|
fn unstuff<'a, A: 'a, E: 'a>(
|
||||||
wa: <<Self::Fallible as MonadFailAny>::W<E> as WeakFunctor>::F<'a, A>,
|
wa: <<Self::Fallible as MonadFailAny>::W<E> as WeakFunctor>::F<'a, A>,
|
||||||
) -> <Self::T as WeakFunctor>::F<'a, Result<A, E>>
|
) -> <Self::T as WeakFunctor>::F<'a, Result<A, E>>
|
||||||
where
|
where
|
||||||
Self: 'a;
|
Self: 'a;
|
||||||
|
|
||||||
|
/// Convert a wrapped result into a fallible wrapped.
|
||||||
fn stuff<'a, A: 'a, E: 'a>(
|
fn stuff<'a, A: 'a, E: 'a>(
|
||||||
fa: <Self::T as WeakFunctor>::F<'a, Result<A, E>>,
|
fa: <Self::T as WeakFunctor>::F<'a, Result<A, E>>,
|
||||||
) -> <<Self::Fallible as MonadFailAny>::W<E> as WeakFunctor>::F<'a, A>
|
) -> <<Self::Fallible as MonadFailAny>::W<E> as WeakFunctor>::F<'a, A>
|
||||||
@ -50,7 +56,7 @@ pub trait ExtContext: Context {
|
|||||||
Self: 'a;
|
Self: 'a;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Ctx: Context> ExtContext for Ctx {
|
impl<Ctx: Context> FallibleContext for Ctx {
|
||||||
fn unstuff<'a, A: 'a, E: 'a>(
|
fn unstuff<'a, A: 'a, E: 'a>(
|
||||||
wa: <<Self::Fallible as MonadFailAny>::W<E> as WeakFunctor>::F<'a, A>,
|
wa: <<Self::Fallible as MonadFailAny>::W<E> as WeakFunctor>::F<'a, A>,
|
||||||
) -> <Self::T as WeakFunctor>::F<'a, Result<A, E>>
|
) -> <Self::T as WeakFunctor>::F<'a, Result<A, E>>
|
||||||
|
Loading…
Reference in New Issue
Block a user