Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import scala.collection.mutable
- type KeyType = Long
- def grouping(xs: mutable.Map[KeyType, Option[String]], box: Int): List[List[(KeyType, Option[String])]] = {
- (for ((x, count) <- xs.toList.zipWithIndex) yield (count, x)).groupBy(x => x._1 % box).map(x => x._2.map(c => c._2)).toList
- }
- def mergeGroup(xs: List[List[(KeyType, Option[String])]]): mutable.Map[KeyType, Option[String]] = {
- def flatten(ls: List[_]): List[_] = ls flatMap {
- case i: List[_] => flatten(i)
- case e => List(e)
- }
- collection.mutable.Map(flatten(xs).map{case(k, v) => (k, v)}: _*).asInstanceOf[mutable.Map[KeyType, Option[String]]]
- }
- val xs: mutable.Map[KeyType, Option[String]] = mutable.Map(
- (24, Some("flag")),
- (83, Some("setup")),
- (89, Some("fighter")),
- (94, Some("curiosities")),
- (114, Some("strong")),
- (133, Some("brutish")),
- (177, Some("blockade")),
- (247, Some("fort")),
- (299, Some("bad")),
- (302, Some("burial")),
- (317, Some("contestant")),
- (329, Some("anonymous")),
- (337, Some("cube")),
- (341, Some("elimination")),
- (345, Some("contractual")),
- (434, Some("everlasting")),
- (445, Some("daydreamer")),
- (468, Some("femur")),
- (475, Some("decipherer")),
- (479, Some("serpent")),
- (602, Some("chain")),
- (604, Some("rum")),
- (612, Some("terminus")),
- (623, Some("degrading")),
- (640, Some("loneliness")),
- (649, Some("blob")),
- (685, Some("land")),
- (692, Some("confidential")),
- (696, Some("herd")),
- (699, Some("beyond"))
- )
- val groupedData: List[List[(KeyType, Option[String])]] = grouping(xs, 5)
- groupedData.map(println)
- val mergedData: mutable.Map[KeyType, Option[String]] = mergeGroup(groupedData)
- println(xs)
- println(mergedData)
Add Comment
Please, Sign In to add comment