-
Notifications
You must be signed in to change notification settings - Fork 274
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
DistinctUnique does not filter unique string when 'equals' parameter is provided #503
Comments
DistinctUnique uses a HashSet underneath, alpha and gamma aren't the same hashCode, so you'd need to provide a different hashing method, next to the equals handler. For example (e) => e.length ...that said maybe we could just do without the hassle of needing a hasher, and just require an equals only |
@Santosh07 extension DistinctUniqueExtension<T> on Stream<T> {
/// WARNING: More commonly known as distinct in other Rx implementations.
///
/// Creates a Stream where data events are skipped if they have already
/// been emitted before, based on [hashCode] and [equals] comparison of the objects
/// returned by the key selector function.
///
/// Equality is determined by the provided equals and hashCode methods.
/// If these are omitted, the '==' operator and hashCode on the last provided
/// data element are used.
///
/// The returned stream is a broadcast stream if this stream is. If a
/// broadcast stream is listened to more than once, each subscription will
/// individually perform the equals and hashCode tests.
///
/// [Interactive marble diagram](http://rxmarbles.com/#distinct)
Stream<T> distinctUniqueBy<R>(
R Function(T) keySelector, {
bool Function(R e1, R e2) equals,
int Function(R e) hashCode,
}) =>
distinctUnique(
equals: (e1, e2) {
final k1 = keySelector(e1);
final k2 = keySelector(e2);
return equals?.call(k1, k2) ?? (k1 == k2);
},
hashCode: (e) {
final k = keySelector(e);
return hashCode?.call(k) ?? k.hashCode;
},
);
} and use it void main() {
Stream.fromIterable(['Alpha', 'Beta', 'Gamma'])
.distinctUniqueBy((e) => e.length)
.listen(print);
} @frankpepermans Should add |
@hoc081098 Dunno if we should have both |
@frankpepermans
I have a another option: add |
For anyone interested in |
Stream.fromIterable(['Alpha', 'Beta', 'Gamma']).distinctUnique( equals: (e1, e2) { return e1.length == e2.length; }).listen((event) { print(event); });
The output is : Alpha beta Gamma
Expected Output: Since distinceUnique is performing equality based on length of string, the output should be 'Alpha beta' only.
There is no difference in functionality in 'distinctUnique' and 'distinct' when parameter is provided.
Version: 0.24.1
The text was updated successfully, but these errors were encountered: