Johnny
Johnny

Reputation: 15423

Scala don't do in case at least one exist

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

Answers (1)

zagyi
zagyi

Reputation: 17528

You could use exists(predicate: A => Boolean) that is defined on every Iterable:

if (!anotherEvent.events().exists(_.isSame(event))) {
  //do
}

Upvotes: 5

Related Questions