I struggled to find a way to stay true to the functional style for expressions, when I needed to collect several object parameters into a list.
As an example, let's say that I have a Notification object that has both fromId (the identifier of the user from which the notification comes) and objectOwnerId (identifier of the user who created the original object). They may differ in facebook style notifications ("X also commented on post Y").
I can compile userIds with such expression for expression
val userIds = for { notification <- notifications } yield notification.fromId
however, I will say that I want to collect both fromIds and objectOwnerIds into one list, is there a way to do this in one for an expression without the vars user?
In the past, I have done something like this:
var ids = List()
for {
notification <- notifications
ids = ids ++ List(notification.fromId, notification.objectOwnerId)
}
ids = ids.distinct
, . var . , , - .
!