There are a few libraries such as Spark and other Scala extensions that have the "groupWith" function available. This function allows you to compare an element to the rest of the collection and then group it using one or more predicates. There doesn't seem to be any native functionality in Scala for this but they do have the sortWith function that behaves similarly but only sorts the items instead of grouping them. If the explanation isn't sufficient here's a small code sample that should display what I'm trying to do:
val list = List(1,2,3,4,5,5)
val groupedList = list.groupWith{ (e,c) =>
e == c
}
This is a very simple example and I want to do more complicated comparisons such as
e + 1 == c
So again the question is are there any native Scala functions that do this? Any suggestions or workarounds?
Update:
From the simple examples given it seems it's not exactly clear what I'm trying to do, here's a better example:
Say I have a case class and a list of these objects:
case class Item(num: Int, color: String)
val list = List(new Item(13, "red"), new Item(14,"red"), new Item(15, "blue"), new Item(16, "red"))
list.groupWith{ (e,c) =>
(e.num -1 == c.num || e.num + 1 == c.num ) && e.color == c.color
}
And this should return something like this:
res8: List[List[Item]] = List(List(Item(13,red), Item(14,red)), List(Item(15,blue)), List(Item(16,red)))
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…