Reputation: 15423
What is the most Scala way solution for an algorithm for don't do in case at least one exists?
For example, in Java I would do:
private void handle(Event event, AnotherEvent anotherEvent) {
boolean alreadyExists = false;
for (AnotherEvent existingEvent: anotherEvent.events()) {
if (existingEvent.isSame(event)) {
alreadyExists = true;
break;
}
}
if (!alreadyExists) {
//do
}
}
Upvotes: 1
Views: 63
Reputation: 17528
You could use exists(predicate: A => Boolean)
that is defined on every Iterable
:
if (!anotherEvent.events().exists(_.isSame(event))) {
//do
}
Upvotes: 5