Showing posts with label Scala. Show all posts
Showing posts with label Scala. Show all posts

Saturday, September 22, 2018

Monday, July 28, 2014

Java/Scala benchmark and tools

http://shipilev.net/blog/2014/java-scala-divided-we-fail/
http://openjdk.java.net/projects/code-tools/jmh/
http://hg.openjdk.java.net/code-tools/jmh/file/tip/jmh-samples/src/main/java/org/openjdk/jmh/samples/

As a good tradition, we will take some diversions into benchmarking methodology, so that even though the post itself is targeted to platform people, the non-platform people can still learn a few tricks. As usual, if you still haven’t learned about JMH and/or haven’t looked through the JMH samples, then I suggest you do that first before reading the rest of this post for the best experience.

Benchmark

The discussion in that particular StackOverflow thread dates back a few questions, so instead of digging there, we will just take the latest benchmark code, and wrap it up with JMH. JMH already has the bindings for Java and Scala, which somewhat alleviates the difference in testing methodology. You can find full benchmark code here (warning, it contains spoilers).

Thursday, July 17, 2014

Scala covariant and invariant

http://abstractlayers.com/2014/03/06/covariance-and-contravariance-in-scala/

If you have 2 classes A,B such that A is subtype of B, and
if you have 2 parameterized types C[A],C[B] such that C[A] is subtype of C[B] then C is said to be covariant
Examples
Immutable List and Queue are examples of covariant Types in scala libraries.
1
2
3
sealed abstract class List[+A] extends .........
class Queue[+A] extends ........
Covariance is indicated by notation + on type parameter passed

Immutablity and Covariance

If you notice in api-docs covariance is generally associated with immutable types , eg scala.collection.immutable.Queue, scala.collection.immutable.List
There is a very good reason for this. From designers perspective, there were 2 reasons where covariance relation could fail
  • Re-Assignment of values. Classic example being java array reassignment problem, resulting in ArrayStore exception
  • Passing generic type as a parameter in method.
Thus to avoid above pitfalls , covariant types are immutable

If you have 2 classes A, B such that A is subtype of B, and
if you have 2 parametrized Types C[A], C[B] such that C[B] is subtype of C[A] then C is said to be contravariant
Examples
OuputChannel trait from scala api is example of contravariant type
1
trait OutputChannel[-Msg] extends AnyRef
Contravariance is indicated by notation – on type parameter passed. 

*** Function parameters are contravariant ***
*** Function’s return value type is covariant ***

https://twitter.github.io/scala_school/type-basics.html
Variance
Scala’s type system has to account for class hierarchies together with polymorphism. Class hierarchies allow the expression of subtype relationships. A central question that comes up when mixing OO with polymorphism is: if T’ is a subclass of T, is Container[T’] considered a subclass of Container[T]? Variance annotations allow you to express the following relationships between class hierarchies & polymorphic types:
MeaningScala notation
covariantC[T’] is a subclass of C[T][+T]
contravariantC[T] is a subclass of C[T’][-T]
invariantC[T] and C[T’] are not related[T]
The subtype relationship really means: for a given type T, if T’ is a subtype, can you substitute it?
scala> class Covariant[+A]
defined class Covariant

scala> val cv: Covariant[AnyRef] = new Covariant[String]
cv: Covariant[AnyRef] = Covariant@4035acf6

scala> val cv: Covariant[String] = new Covariant[AnyRef]
:6: error: type mismatch;
 found   : Covariant[AnyRef]
 required: Covariant[String]
       val cv: Covariant[String] = new Covariant[AnyRef]
                                   ^

Monday, July 14, 2014

Scala tips

http://stackoverflow.com/questions/12273872/scala-using-span-with-modular-arithmetic

The method you need to use is partition, not span:
scala> (1 to 10).partition(_ % 2 == 0)
res0: (IndexedSeq[Int], IndexedSeq[Int]) = (Vector(2, 4, 6, 8, 10),Vector(1, 3, 5, 7, 9))
Since you want a List[List[Int]], you could do this:
val lst = (1 to 10).toList
val (evens, odds) = lst.partition(_ % 2 == 0)
val newList = List(evens,odds) // List(List(2, 4, 6, 8, 10), List(1, 3, 5, 7, 9))
The span method can only be used to split a sequence at a single point:
scala> (1 to 10).span(_ < 5)
res1: (Range, Range) = (Range(1, 2, 3, 4),Range(5, 6, 7, 8, 9, 10))
When you tried lst.span(_ % 2 == 0), the program found that the first item, 1, did not pass the test (_ % 2 == 0), so all the elements were put in the second list, leaving none in the first.