I have the following function which deals with a series of search events which need to be grouped together in search flows in case they are related.
def split(eventsIterator: Iterator[SearchFlowSearchEvent]): Iterator[SearchFlow] = {
val sortedEventsIterator = eventsIterator.toList.sortBy(_.evTimeMillis).iterator
val searchFlowsEvents: mutable.MutableList[mutable.MutableList[SearchFlowSearchEvent]] = mutable.MutableList()
var currentSearchFlowEvents: mutable.MutableList[SearchFlowSearchEvent] = mutable.MutableList()
var previousEvent: SearchFlowSearchEvent = null
while (sortedEventsIterator.hasNext) {
val currentEvent = sortedEventsIterator.next()
if (isSameFlow(previousEvent, currentEvent)) {
currentSearchFlowEvents += currentEvent
} else {
currentSearchFlowEvents = mutable.MutableList()
currentSearchFlowEvents += currentEvent
searchFlowsEvents += currentSearchFlowEvents
}
previousEvent = currentEvent
}
searchFlowsEvents
.map(searchFlowEvents => model.SearchFlow(searchFlowEvents.toList))
.iterator
}
The approach of performing the grouping of the events listed above is iterative (I'm coming from the Java world).
Can anybody provide me some hints on how to achieve the same results in a functional fashion.