Trait naan::alt::Alt

source ·
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§

source

fn alt(self, b: Self) -> Self

See Alt

Implementations on Foreign Types§

source§

impl<A, E> Alt<ResultOk<E>, A> for Result<A, E>

source§

fn alt(self, b: Self) -> Self

source§

impl<A> Alt<Vec, A> for Vec<A>

source§

fn alt(self, b: Self) -> Self

source§

impl<K, A> Alt<BTreeMapValues<K>, A> for BTreeMap<K, A>where K: Ord,

source§

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)]))
source§

impl<A> Alt<Option, A> for Option<A>

source§

fn alt(self, b: Self) -> Self

source§

impl<const N: usize, A> Alt<ArrayVec<N>, A> for ArrayVec<[Option<A>; N]>

source§

fn alt(self, b: Self) -> Self

source§

impl<K, A> Alt<HashMapValues<K>, A> for HashMap<K, A>where K: Eq + Hash,

source§

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)]))

Implementors§

source§

impl<T> Alt<Id, T> for Id<T>