Skip to content

Instantly share code, notes, and snippets.

@mauricioszabo
Forked from isa/gist:2571012
Created May 2, 2012 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauricioszabo/2580938 to your computer and use it in GitHub Desktop.
Save mauricioszabo/2580938 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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