Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

scala - Is there a native grouping function that works like the sortWith function?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's an implementation:

// Takes the list as a parameter, can use pimp-my-library if you want
def groupWith[A](xs: List[A], f: (A, A) => Boolean) = {
  // helper function to add "e" to any list with a member that matches the predicate
  // otherwise add it to a list of its own
  def addtoGroup(gs: List[List[A]], e: A): List[List[A]] = {
    val (before, after) = gs.span(_.exists(!f(_, e)))
    if (after.isEmpty)
      List(e) :: gs
    else
      before ::: (e :: after.head) :: after.tail
  }
  // now a simple foldLeft adding each element to the appropriate list
  xs.foldLeft(Nil: List[List[A]])(addtoGroup)
} 

groupWith(list, { (e: Item, c: Item) =>
                    (e.num - 1 == c.num || e.num + 1 == c.num) && e.color == c.color})

//| res0: List[List[groups.groups.Item]] =
//         List(List(Item(16,red)),
//              List(Item(15 ,blue)), 
//              List(Item(14,red), Item(13,red)))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...