pub trait Alt<F, A>where
Self: Functor<F, A>,
F: HKT1<T<A> = Self>,{
// Required method
fn alt(self, b: Self) -> Self;
}
Expand description
An associative operation for a type with 1 generic parameter.
Similar to Semigroup
, but for generic types.
Examples
Vec
’s Alt implementation concatenates the two Vecs.Option
’s Alt implementation yields the first encountered Some.Result
’s Alt implementation yields the first encountered Ok.
Laws
- Associative (
a.alt(b.alt(c)) == a.alt(b).alt(c)
) - Distributive (
a.alt(b).map(foo) == a.map(foo).alt(b.map(foo))
)
Required Methods§
Implementations on Foreign Types§
source§impl<K, A> Alt<HashMapValues<K>, A> for HashMap<K, A>where
K: Eq + Hash,
impl<K, A> Alt<HashMapValues<K>, A> for HashMap<K, A>where K: Eq + Hash,
source§fn alt(self, b: Self) -> Self
fn alt(self, b: Self) -> Self
Combine the two maps, preferring keys from self
when self
and b
both have an entry for a given key.
use std::collections::HashMap;
use naan::prelude::*;
let a_union_b = HashMap::from([("a", 1), ("b", 2)]).alt(HashMap::from([("b", 3), ("c", 3)]));
assert_eq!(a_union_b, HashMap::from([("a", 1), ("b", 2), ("c", 3)]))
source§impl<K, A> Alt<BTreeMapValues<K>, A> for BTreeMap<K, A>where
K: Ord,
impl<K, A> Alt<BTreeMapValues<K>, A> for BTreeMap<K, A>where K: Ord,
source§fn alt(self, b: Self) -> Self
fn alt(self, b: Self) -> Self
Combine the two maps, preferring keys from self
when self
and b
both have an entry for a given key.
use std::collections::BTreeMap;
use naan::prelude::*;
let a_union_b = BTreeMap::from([("a", 1), ("b", 2)]).alt(BTreeMap::from([("b", 3), ("c", 3)]));
assert_eq!(a_union_b, BTreeMap::from([("a", 1), ("b", 2), ("c", 3)]))