Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Merge MergeBy and MergeJoinBy #701

@phimuemue

Description

@phimuemue

It seems that merge_join_by/MergeJoinBy is actually a more generic variant of merge/MergeBy.

However, currently, the MergeJoinBy and MergeBy are implemented independently (PutBack vs Peekable, fused behavior, requiring Ordering vs bool). The past showed that uniform implementations may be better.

This change should not introduce breaking changes for users: merge, merge_by and merge_join_by should stay usable as they are.

However, I'd imagine that the general merge_join_by could also accept a comparator returning bool (instead of Ordering) - this would free the users from having to deal with the EitherOrBoth::Both case. I.e. I suggest that users should be able to write:

use itertools; // 0.10.5
use itertools::{Either, EitherOrBoth};

fn main() {
    let numbers = [1,2,3,4,5];
    let strings = ["one", "two", "three", "four"];
    
    for x in itertools::merge_join_by(
        numbers,
        strings,
        |number, string| number.cmp(&string.len()),
    ) {
        match x {
            EitherOrBoth::Left(number) => {dbg!(number);},
            EitherOrBoth::Right(string) => {dbg!(string);},
            EitherOrBoth::Both(number, string) => {dbg!(number, string);},
        };
    }
    
    for x in itertools::merge_join_by(
        numbers,
        strings,
        |number, string| number < &string.len(), // <-- Note that we return bool instead of Ordering
    ) {
        match x {
            Either::Left(number) => {dbg!(number);},
            Either::Right(string) => {dbg!(string);},
            // <-- Note that we do not need to deal with the "Both" case, as our comparator returns bool
        };
    }
}

When we tackle this, we should - as a first step - probably move all the merge-related functions into an own module.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions