eric.mariacher@gmail.com
jeudi, mars 17, 2011
jeudi, mars 10, 2011
Scala TreeSet: foreach vs foldLeft
Here is the best article I found to understand what is foldLeft. And here is an example on how to replace foreach with foldLeft:
class KbdKey() { def generateQuartets(): ListSet[Quartet] = {...} } class KbdMatrix() { var ts_keys = new TreeSet[KbdKey]() var l_quartets = ListSet.empty[Quartet]; ts_keys.foreach(l_quartets ++= _.generateQuartets()) // 2 lines above can be replaced by the line below val l_quartets = ts_keys.tail.foldLeft(ts_keys.head.generateQuartets)(_ ++ _.generateQuartets) }
mercredi, mars 09, 2011
Scala Treeset example: partition
var ts_keys = new TreeSet[KbdKey]()(new CompareRowThenCol()) val (ts_inferiorOrEqualRowKeys,ts_superiorRowKeys) = ts_Keys.partition(_.i_row<=i_row) class CompareRowThenCol extends Ordering[KbdKey] { def compare(k1: KbdKey, k2: KbdKey) = k1.value-k2.value }
lundi, mars 07, 2011
Scala Treeset example: filter, groupBy, foldLeft, sortBy, etc...
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.
Scala and Java libraries in Eclipse
Don't try to mix scala and java in the same scala project in eclipse. You will 1st have the feeling that it works, but as they are not compiled the same way, it's better to have java and scala files in separate projects. Scala project should have the java project as a dependency.
To get it working, you have to do a lot of project cleaning, to "synchronize" both projects.
A Scala file using a Java class:
import kbdmatrix_java._
class KbdMatrix(val L: MyLog) {
The Java file:
package kbdmatrix_java;
public class MyLog {
Do not forget to advertize the Java class and its methods as public.
Scala: code to find worksheet name in an excel workbook saved in microsoft xml 2003 format
Scala: code to find worksheet name in an excel workbook saved in microsoft xml 2003 format:
def matchName(n: scala.xml.NodeSeq, s_attributeValue: String): Boolean = { n match { case xml.Elem(_, "Worksheet", xml.PrefixedAttribute("ss", "Name", v, _), _, _*) => if(v.text==s_attributeValue) true else false case _ => false } }
jeudi, mars 03, 2011
Social Media Landscape In China
Inscription à :
Articles (Atom)