Why is Function[-A1,…,+B] not about allowing any supertypes as parameters?

Covariance and contravariance are qualities of the class not qualities of the parameters. (They are qualities that depend on the parameters, but they make statements about the class.) So, Function1[-A,+B] means that a function that takes superclasses of A can be viewed as a subclass of the original function. Let’s see this in practice: class … Read more

Use Scala parser combinator to parse CSV files

What you missed is whitespace. I threw in a couple bonus improvements. import scala.util.parsing.combinator._ object CSV extends RegexParsers { override protected val whiteSpace = “””[ \t]”””.r def COMMA = “,” def DQUOTE = “\”” def DQUOTE2 = “\”\”” ^^ { case _ => “\”” } def CR = “\r” def LF = “\n” def CRLF … Read more

running scala apps with java -jar

If you define -jar the -classpath is ignored: Java manual: -jar When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored. You can define the classpath dependencies in the Manifest metadata. The easiest way to start your app is using the scala … Read more

scala parallel collections degree of parallelism

With the newest trunk, using the JVM 1.6 or newer, use the: collection.parallel.ForkJoinTasks.defaultForkJoinPool.setParallelism(parlevel: Int) This may be a subject to changes in the future, though. A more unified approach to configuring all Scala task parallel APIs is planned for the next releases. Note, however, that while this will determine the number of processors the query … Read more

How to make a right-associative infix operator?

I found a solution. Scala reference says in section 6.12.3 Infix Operations: The associativity of an operator is determined by the operator’s last character. Operators ending in a colon ‘:’ are right-associative. All other operators are left-associative. Therefore it was enough to rename >> to >>:. It took me some time to realize that while … Read more

Scala: short form of pattern matching that returns Boolean

This is exactly why I wrote these functions, which are apparently impressively obscure since nobody has mentioned them. scala> import PartialFunction._ import PartialFunction._ scala> cond(“abc”) { case “def” => true } res0: Boolean = false scala> condOpt(“abc”) { case x if x.length == 3 => x + x } res1: Option[java.lang.String] = Some(abcabc) scala> condOpt(“abc”) … Read more

Convert Java Map to Scala Map

Edit: the recommended way is now to use JavaConverters and the .asScala method: import scala.collection.JavaConverters._ val myScalaMap = myJavaMap.asScala.mapValues(_.asScala.toSet) This has the advantage of not using magical implicit conversions but explicit calls to .asScala, while staying clean and consise. The original answer with JavaConversions: You can use scala.collection.JavaConversions to implicitly convert between Java and Scala: … Read more

tech