collect should not have side-effects. You can use a simple pattern match here:
```
selectedItems match {
}
```