Skip to content

Commit

Permalink
Implement subtraction of arrays. (#164)
Browse files Browse the repository at this point in the history
Thanks to @kklingenberg for having provided the initial implementation of this functionality in jaq.

This should close #161.
  • Loading branch information
01mf02 authored Mar 11, 2024
1 parent 18b4bad commit 03cbbcc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
5 changes: 5 additions & 0 deletions jaq-interpret/src/val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,11 @@ impl core::ops::Sub for Val {
(Float(x), Float(y)) => Ok(Float(x - y)),
(Num(n), r) => Self::from_dec_str(&n) - r,
(l, Num(n)) => l - Self::from_dec_str(&n),
(Arr(mut l), Arr(r)) => {
let r = alloc::collections::BTreeSet::from_iter(r.iter());
Rc::make_mut(&mut l).retain(|x| !r.contains(x));
Ok(Arr(l))
}
(l, r) => Err(Error::MathOp(l, MathOp::Sub, r)),
}
}
Expand Down
2 changes: 2 additions & 0 deletions jaq-interpret/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ fn sub() {
give(json!(1.0), ". - 1", json!(0.0));
}

yields!(sub_arr, "[1, 2, 3] - [2, 3, 4]", json!([1]));

#[test]
fn mul() {
give(json!(1), ". * 2", json!(2));
Expand Down

0 comments on commit 03cbbcc

Please sign in to comment.