Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert in less than 30 lines
val before = List(
List('A', 'B', 'C'),
List('A', 'C', 'E'),
List('E', 'F', 'D'),
List('D', 'A', 'J'),
List('E', 'D', 'J')
)
val expected = List(
List('A', 'B', 1),
List('A', 'C', 2),
List('A', 'D', 1),
List('A', 'E', 1),
List('A', 'J', 1),
List('B', 'C', 1),
List('C', 'E', 1),
List('D', 'E', 2),
List('D', 'F', 1),
List('D', 'J', 2),
List('E', 'F', 1),
List('E', 'J', 1)
)
val map = scala.collection.mutable.Map[List[Char], Int]()
before.foreach { row =>
(row :+ row(0)).sliding(2).foreach { e =>
map(e.sorted) = map.getOrElse(e.sorted, 0) + 1
}
}
val after = map.map { case(l, e) => l :+ e }.toList.sortBy { case v:List[Char] => v(0).toByte * 100 + v(1).toByte }
assert(expected == after, "List is not what we spected to be: "+after)
@mauricioszabo

This comment has been minimized.

Show comment
Hide comment
@mauricioszabo

mauricioszabo May 2, 2012

In scala, using "List".

It's shorter, but issues a warning. To remove the warning is to give it 3 more lines at least...

Owner

mauricioszabo commented May 2, 2012

In scala, using "List".

It's shorter, but issues a warning. To remove the warning is to give it 3 more lines at least...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment