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

Anything equivalent to Scala's Stream.diff? #379

Closed
mandrean opened this issue Oct 31, 2019 · 2 comments
Closed

Anything equivalent to Scala's Stream.diff? #379

mandrean opened this issue Oct 31, 2019 · 2 comments

Comments

@mandrean
Copy link

mandrean commented Oct 31, 2019

I'm trying to find something similar to Scala's Stream.diff:

Computes the multiset difference between this sequence and another sequence.

that: the sequence of elements to remove

returns: a new sequence which contains all elements of this sequence except some of occurrences of elements that also appear in that. If an element value x appears n times in that, then the first n occurrences of x will not form part of the result, but any following occurrences will.

I see there's https://docs.rs/itertools/0.8.1/itertools/fn.diff_with.html, but it doesn't seem to do the same thing? Maybe we call it something else?

@mandrean
Copy link
Author

mandrean commented Oct 31, 2019

Hmm, I guess you can do:

use itertools::Itertools;
use itertools::EitherOrBoth::{Left, Right, Both};

let a = (0..=10).step_by(2);
let b = (0..=10).step_by(4);
let a_not_b = a
    .merge_join_by(b, |i, j| i.cmp(j))
    .flat_map(|either| {
        match either {
            Left(l) => Some(l),
            _ => None
        }
    })
    .collect::<Vec<i32>>();

assert_eq!(a_not_b, vec![2, 6, 10]);

But it would be nice with a .diff(&self, other: J) or .left_join(&self, other: J) to cut down the boiler plate!

@Philippe-Cholet
Copy link
Member

After #629, you should be able to do this (untested)

let a_not_b = a
    .merge_join_by(b, |i, j| i.cmp(j))
    .filter_map(EitherOrBoth::just_left)
    .collect_vec();

It cuts down much of the boilerplate so I think it's good enough.

@Philippe-Cholet Philippe-Cholet closed this as not planned Won't fix, can't repro, duplicate, stale Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants