Skip to content

Commit

Permalink
uniplate: fill in Uniplate and Biplate implementation for iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasdewally committed Apr 13, 2024
1 parent 674ab8e commit 86c6ee0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
5 changes: 5 additions & 0 deletions crates/uniplate/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
// this and/or devirtualise the Box<dyn Fn()> when necessary to make this fast.
// https://users.rust-lang.org/t/why-box-dyn-fn-is-the-same-fast-as-normal-fn/96392

use im::Vector;
use std::collections::VecDeque;

use crate::biplate::*;
use crate::derive_iter;
use crate::derive_unplateable;
Expand All @@ -32,3 +35,5 @@ derive_unplateable!(String);
// Implement Biplate for collections by converting them to iterators.

derive_iter!(Vec);
derive_iter!(VecDeque);
derive_iter!(Vector);
26 changes: 23 additions & 3 deletions crates/uniplate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,29 @@ macro_rules! derive_iter {
// Unwrap iterator
impl<T> Biplate<T> for $t<T>
where
T: Clone + Eq + Uniplate + Sized + 'static,
T: Clone + Eq + Uniplate + Biplate<T> + Sized + 'static,
{
fn biplate(&self) -> (Tree<T>, Box<dyn Fn(Tree<T>) -> Self>) {
todo!()
let mut children = ::im::vector![];
let mut ctxs = ::std::collections::VecDeque::new();
for child in self.clone().into_iter() {
let (subtree, ctx) = <T as Biplate<T>>::biplate(&child);
children.push_back(subtree);
ctxs.push_back(ctx);
}

let ctx = Box::new(move |x| {
let Tree::<T>::Many(xs) = x else {
panic!("");
};
let mut out: ::std::collections::VecDeque<T> =
::std::collections::VecDeque::new();
for (x, ctx) in ::std::iter::zip(xs, &ctxs) {
out.push_back(ctx(x));
}
out.into_iter().collect()
});
(Tree::Many(children), ctx)
}
}

Expand All @@ -162,7 +181,8 @@ macro_rules! derive_iter {
T: Clone + Eq + Uniplate + Sized + 'static,
{
fn biplate(&self) -> (Tree<$t<T>>, Box<dyn Fn(Tree<$t<T>>) -> Self>) {
todo!()
let val = self.clone();
(Tree::One(val.clone()), Box::new(move |_| val.clone()))
}
}

Expand Down

0 comments on commit 86c6ee0

Please sign in to comment.