use crate::{Foldable, Functor, HKT, HKT2, NoConstraint, Placeholder, Satisfies};
use alloc::collections::BTreeMap;
pub struct BTreeMapWitness<K>(Placeholder, K);
impl<K> HKT2<K> for BTreeMapWitness<K> {
type Type<V> = BTreeMap<K, V>;
}
impl<K> HKT for BTreeMapWitness<K> {
type Constraint = NoConstraint;
type Type<V> = BTreeMap<K, V>;
}
impl<K> Functor<BTreeMapWitness<K>> for BTreeMapWitness<K>
where
K: Ord + Clone + 'static,
{
fn fmap<A, B, Func>(
m_a: <BTreeMapWitness<K> as HKT2<K>>::Type<A>,
mut f: Func,
) -> <BTreeMapWitness<K> as HKT2<K>>::Type<B>
where
A: Satisfies<NoConstraint>,
B: Satisfies<NoConstraint>,
Func: FnMut(A) -> B,
{
m_a.into_iter().map(|(k, v)| (k, f(v))).collect()
}
}
impl<K> Foldable<BTreeMapWitness<K>> for BTreeMapWitness<K>
where
K: Ord + 'static,
{
fn fold<A, B, Func>(fa: BTreeMap<K, A>, init: B, f: Func) -> B
where
Func: FnMut(B, A) -> B,
{
fa.into_values().fold(init, f)
}
}