Collections operations: How to determine the iteration?

Let’s say we are filtering a list of strings and want to create a log entry if a string has been filtered out:

val strings = IndexedSeq("foo", "", "bar")

val filtered = strings.filter(string => {
  if (string.isEmpty) {
    logger.trace("value ignored")
    false
  } else {
    true
  }
})

What is the best way to find out which element of the sequence has been ignored to add this information to the logfile?

In what way the code you posted is not good enough?

1 Like

I want to have the logging message: value 1 ignored.

you can use zipWithIndex to get an index value pair

2 Likes