Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate objects lazily & remove itertools dependency #119

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion jaq-interpret/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ ahash = "0.7.6"
dyn-clone = "1.0"
hifijson = { version = "0.2.0", optional = true }
indexmap = "2.0"
itertools = "0.10.3"
once_cell = "1.16.0"
serde_json = { version = "1.0.81", optional = true }

Expand Down
42 changes: 31 additions & 11 deletions jaq-interpret/src/filter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::box_iter::{box_once, flat_map_with, map_with};
use crate::box_iter::{box_once, flat_map_with, map_with, BoxIter};
use crate::results::{fold, recurse, then, Results};
use crate::val::{Val, ValR, ValRs};
use crate::{rc_lazy_list, Bind, Ctx, Error};
Expand Down Expand Up @@ -120,6 +120,30 @@ where
}
}

type ObjVec = Vec<(ValR, ValR)>;
fn obj_cart<'a, I>(mut args: I, cv: Cv<'a>, prev: ObjVec) -> BoxIter<'a, ObjVec>
where
I: Iterator<Item = (Ref<'a>, Ref<'a>)> + Clone + 'a,
{
match args.next() {
Some((l, r)) => flat_map_with(
l.run(cv.clone()),
(args, cv, prev),
move |l, (args, cv, prev)| {
flat_map_with(
r.run(cv.clone()),
(l, args, cv, prev),
|r, (l, args, cv, mut prev)| {
prev.push((l, r));
obj_cart(args, cv, prev)
},
)
},
),
None => box_once(prev),
}
}

fn reduce<'a, T: Clone + 'a, F>(xs: Results<'a, T, Error>, init: Val, f: F) -> ValRs
where
F: Fn(T, Val) -> ValRs<'a> + 'a,
Expand Down Expand Up @@ -188,7 +212,6 @@ impl<'a> FilterT<'a> for &'a Owned {
impl<'a> FilterT<'a> for Ref<'a> {
fn run(self, cv: Cv<'a>) -> ValRs<'a> {
use core::iter::once;
use itertools::Itertools;
// wrap a filter AST with the filter definitions
let w = move |id: &Id| Ref(*id, self.1);
match &self.1[self.0 .0] {
Expand All @@ -200,15 +223,12 @@ impl<'a> FilterT<'a> for Ref<'a> {
Ast::Array(f) => box_once(w(f).run(cv).collect::<Result<_, _>>().map(Val::arr)),
Ast::Object(o) if o.is_empty() => box_once(Ok(Val::Obj(Default::default()))),
Ast::Object(o) => Box::new(
o.iter()
.map(|(k, v)| Self::cartesian(w(k), w(v), cv.clone()).collect::<Vec<_>>())
.multi_cartesian_product()
.map(|kvs| {
kvs.into_iter()
.map(|(k, v)| Ok((k?.to_str()?, v?)))
.collect::<Result<_, _>>()
.map(Val::obj)
}),
obj_cart(o.iter().map(move |(k, v)| (w(k), w(v))), cv, Vec::new()).map(|kvs| {
kvs.into_iter()
.map(|(k, v)| Ok((k?.to_str()?, v?)))
.collect::<Result<_, _>>()
.map(Val::obj)
}),
),
Ast::Try(f, c) => Box::new(w(f).run((cv.0.clone(), cv.1)).flat_map(move |y| {
y.map_or_else(
Expand Down
3 changes: 2 additions & 1 deletion jaq-interpret/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ impl ParseCtx {

let inputs = RcIter::new(core::iter::empty());
let out = f.run((Ctx::new([], &inputs), x));
itertools::assert_equal(out, ys);

assert!(out.eq(ys))
}
}
2 changes: 0 additions & 2 deletions jaq-interpret/src/lir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ pub fn root_def(def: mir::Def) -> filter::Owned {
filter::Owned::new(id, ctx.defs)
}

// TODO: remove itertools dependency

impl Ctx {
fn init_constants(&mut self) {
for (f, id) in [(Filter::Id, IDENTITY), (Filter::ToString, TOSTRING)] {
Expand Down
4 changes: 2 additions & 2 deletions jaq-interpret/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ fn prod<'a, T: 'a + Clone>(
l: impl Iterator<Item = T> + 'a,
r: impl Iterator<Item = T> + 'a,
) -> impl Iterator<Item = (T, T)> + 'a {
use itertools::Itertools;
l.cartesian_product(r.collect::<Vec<T>>())
let r: Vec<_> = r.collect();
l.flat_map(move |l| r.clone().into_iter().map(move |r| (l.clone(), r)))
}

fn skip_take(from: usize, until: usize) -> (usize, usize) {
Expand Down
Loading