karpal_arrow/
arrow_choice.rs1use crate::arrow::Arrow;
5
6pub trait ArrowChoice: Arrow {
15 fn left<A: Clone + 'static, B: Clone + 'static, C: Clone + 'static>(
17 pab: Self::P<A, B>,
18 ) -> Self::P<Result<A, C>, Result<B, C>>;
19
20 fn right<A: Clone + 'static, B: Clone + 'static, C: Clone + 'static>(
22 pab: Self::P<A, B>,
23 ) -> Self::P<Result<C, A>, Result<C, B>> {
24 let mirror_in = Self::arr(|r: Result<C, A>| match r {
25 Ok(c) => Err(c),
26 Err(a) => Ok(a),
27 });
28 let mirror_out = Self::arr(|r: Result<B, C>| match r {
29 Ok(b) => Err(b),
30 Err(c) => Ok(c),
31 });
32 Self::compose(mirror_out, Self::compose(Self::left(pab), mirror_in))
33 }
34
35 fn splat<A: Clone + 'static, B: Clone + 'static, C: Clone + 'static, D: Clone + 'static>(
37 f: Self::P<A, B>,
38 g: Self::P<C, D>,
39 ) -> Self::P<Result<A, C>, Result<B, D>> {
40 Self::compose(Self::right(g), Self::left(f))
41 }
42
43 fn fanin<A: Clone + 'static, B: Clone + 'static, C: Clone + 'static>(
45 f: Self::P<A, C>,
46 g: Self::P<B, C>,
47 ) -> Self::P<Result<A, B>, C> {
48 let merge = Self::arr(|r: Result<C, C>| match r {
49 Ok(c) | Err(c) => c,
50 });
51 Self::compose(merge, Self::splat(f, g))
52 }
53}