From 4f717b01e267b31c96de6272c2343b47559b111e Mon Sep 17 00:00:00 2001 From: timofey Date: Fri, 16 Jun 2023 09:17:25 +0000 Subject: [PATCH] `BinaryTreesBindable::bounds_bind` --- src/flow/binary/bounds/bound.rs | 67 ++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/src/flow/binary/bounds/bound.rs b/src/flow/binary/bounds/bound.rs index a18b6b0..5ae76fe 100644 --- a/src/flow/binary/bounds/bound.rs +++ b/src/flow/binary/bounds/bound.rs @@ -24,6 +24,17 @@ impl<'a, BT: FunctorContext<'a>> FunctorContext<'a> for BoundTrees { trait BinaryTreesBindable<'a>: BinaryTrees<'a> { fn bounds_error(&self, error: BoundsError) -> BTWrap<'a, Self, T>; + + fn bounds_bind( + &self, + ra: Result>, + f: impl FnOnce(A) -> BTWrap<'a, Self, B>, + ) -> BTWrap<'a, Self, B> { + match ra { + Ok(a) => f(a), + Err(e) => self.bounds_error(e), + } + } } impl<'a, BT: BinaryTreesBindable<'a>> BinaryTrees<'a> for BoundTrees { @@ -70,15 +81,17 @@ impl<'a, BT: BinaryTreesBindable<'a>> BinaryTrees<'a> for BoundTrees { let bounds = reference.bounds.clone(); Self::bind(self.0.resolve(&reference.bound), move |node| { let (_, _, key) = ctx.0.split(&node); - match bounds.clone().split(&key, ctx.comparator()) { - Ok((boundsl, boundsr)) => Self::pure(BoundNode2 { - boundsl, - boundsr, - bounds, - node, - }), - Err(e) => ctx.0.bounds_error(e), - } + ctx.0.bounds_bind( + bounds.clone().split(&key, ctx.comparator()), + |(boundsl, boundsr)| { + Self::pure(BoundNode2 { + boundsl, + boundsr, + bounds, + node, + }) + }, + ) }) } @@ -110,8 +123,9 @@ impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesEmpty<'a>> BinaryTreesEmpty<'a tree: Self::Tree, key: Self::Key, ) -> BTWrap<'a, Self, KeySplit<'a, Self>> { - match tree.bounds.split(&key, self.comparator()) { - Ok((boundsl, boundsr)) => { + self.0.bounds_bind( + tree.bounds.split(&key, self.comparator()), + |(boundsl, boundsr)| { Self::bind(self.0.split_key_empty(tree.bound, key), |(tl, tr)| { Self::pure(( Bound { @@ -124,9 +138,8 @@ impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesEmpty<'a>> BinaryTreesEmpty<'a }, )) }) - } - Err(e) => self.0.bounds_error(e), - } + }, + ) } } @@ -146,17 +159,19 @@ impl<'a, BT: BinaryTreesBindable<'a> + BinaryTreesAvl<'a>> BinaryTreesAvl<'a> fo tr: Self::Tree, ) -> BTWrap<'a, Self, Self::Node> { let (boundsl, boundsr) = (tl.bounds, tr.bounds); - match Bounds::join(boundsl.clone(), boundsr.clone(), &key, self.comparator()) { - Ok(bounds) => Self::fmap( - self.0.join_key_unbalanced(tl.bound, key, tr.bound), - |node| BoundNode2 { - boundsl, - boundsr, - bounds, - node, - }, - ), - Err(e) => self.0.bounds_error(e), - } + self.0.bounds_bind( + Bounds::join(boundsl.clone(), boundsr.clone(), &key, self.comparator()), + |bounds| { + Self::fmap( + self.0.join_key_unbalanced(tl.bound, key, tr.bound), + |node| BoundNode2 { + boundsl, + boundsr, + bounds, + node, + }, + ) + }, + ) } }