A scala example doing various operations on a TreeSet containing keys organized in rows and columns and who have a certain type. Output is an html table.
def myPrint(i_type: Int, ts_keys: TreeSet[KbdKey]) {
val lcol = (0 to 16)
print(lcol.tail.foldLeft("Column [" + lcol.head +"]")(_ + "Column [" + _ +"]"))
val ts_matrixFilteredByType = ts_keys.filter((k: KbdKey) => k.i_type==i_type)
val m_matrixGroupByRow = ts_matrixFilteredByType.groupBy((k: KbdKey) => k.i_row)
val m_matrixGroupBySortedRow = ListMap(m_matrixGroupByRow.toList.sortBy{_._1}:_*)
m_matrixGroupBySortedRow.foreach((p:(Int, TreeSet[KbdKey])) =>
print(p._2.tail.foldLeft("Row["+p._1+"]" + p._2.head.myprint)(_ + _.myprint) +"-"))
}
Things to notice are:
- use of foldLeft to do some printing and not some plain summing "as usual".
- groupBy returns pairs.
Aucun commentaire:
Enregistrer un commentaire