Skip to content
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
9 changes: 7 additions & 2 deletions jaq-core/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ type Results<'a, T, V> = box_iter::Results<'a, T, Exn<'a, V>>;
pub(crate) struct Vars<'a, V>(RcList<Bind<V, usize, (&'a Id, Self)>>);

impl<'a, V> Vars<'a, V> {
/// Initialise new variables from values.
pub fn new(vars: impl IntoIterator<Item = V>) -> Self {
Self(RcList::new().extend(vars.into_iter().map(Bind::Var)))
}

fn get(&self, i: usize) -> Option<&Bind<V, usize, (&'a Id, Self)>> {
self.0.get(i)
}
Expand Down Expand Up @@ -480,7 +485,7 @@ impl<F: FilterT<F>> FilterT<F> for Id {
}
}
Ast::Native(id, args) => {
let cvs = bind_vars(args, lut, Ctx::new([], cv.0.inputs), cv);
let cvs = bind_vars(args, lut, cv.0.with_vars(Vars::new([])), cv);
flat_map_then(cvs, |cv| lut.funs[*id].run(lut, cv))
}
Ast::Label(id) => {
Expand Down Expand Up @@ -565,7 +570,7 @@ impl<F: FilterT<F>> FilterT<F> for Id {
}
Ast::Native(id, args) => {
let init = cv.1.clone();
let cvs = bind_vars(args, lut, Ctx::new([], cv.0.inputs), cv);
let cvs = bind_vars(args, lut, cv.0.with_vars(Vars::new([])), cv);
reduce(cvs, init, move |cv, v| {
lut.funs[*id].update(lut, (cv.0, v), f.clone())
})
Expand Down
11 changes: 11 additions & 0 deletions jaq-std/tests/funs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,17 @@ yields!(round_floor, "-1.4 | round", -1);
yields!(floor_floor, "-1.4 | floor", -2);
yields!(ceili_floor, "-1.4 | ceil ", -1);

yields!(
sort_break_out,
"[1, 2] | (label $x | sort_by(label $y | ., break $x)), 3",
3
);
yields!(
sort_break_in,
"[1, 2] | label $x | sort_by(label $y | ., break $y)",
[1, 2]
);

#[test]
fn startswith() {
give(json!("foobar"), r#"startswith("")"#, json!(true));
Expand Down