ListenableFuture to scala Future

The second option is best, it keeps everything asynchronous. but… you can do one better and abstract the solution into a reusable pattern: implicit class RichListenableFuture[T](lf: ListenableFuture[T]) { def asScala: Future[T] = { val p = Promise[T]() Futures.addCallback(lf, new FutureCallback[T] { def onFailure(t: Throwable): Unit = p failure t def onSuccess(result: T): Unit = p … Read more

How to create an immutable map/set from a seq?

In Scala 2.8: Welcome to Scala version 2.8.0.r20327-b20091230020149 (Java HotSpot(TM) Client VM, Java 1.6. Type in expressions to have them evaluated. Type :help for more information. scala> val seq: Seq[(String,Object)] = (“a”,”A”)::(“b”,”B”)::Nil seq: Seq[(String, java.lang.Object)] = List((a,A), (b,B)) scala> val map = Map(seq: _*) map: scala.collection.immutable.Map[String,java.lang.Object] = Map(a -> A, b -> B) scala> val … Read more

In scala can I pass repeated parameters to other methods?

Java makes an assumption that you want to automatically convert the Array args to varargs, but this can be problematic with methods that accept varargs of type Object. Scala requires that you be explicit, by ascribing the argument with : _*. scala> def bar(args:String*) = println(“count=”+args.length) bar: (args: String*)Unit scala> def foo(args:String*) = bar(args: _*) … Read more

Conversion from scala parallel collection to regular collection

As you go explicitly from sequential to parallel collection via .par, you go back to sequential via .seq. Since sets and maps have parallel implementations, toMap and toSet calls leave the collection in the current domain. The example of reduce works because it, well, reduces the collection (the outer ParSeq disappears, leaving you with the … Read more

Char or String -> Unicode value in Scala?

When you do x.foldLeft(1)(_ * _.toInt), the result type will be inference to an Int, but 9415087488 is too large for an Int to store it. So you need to tell Scala using Long to store it. scala> val x = “Hello” x: java.lang.String = Hello scala> x.foldLeft(1L)(_ * _.toInt) res1: Long = 9415087488 scala> … Read more

In Scala, why is NaN not being picked up by pattern matching?

It is a general rule how 64-bit floating point numbers are compared according to IEEE 754 (not Scala or even Java related, see NaN): double n1 = Double.NaN; double n2 = Double.NaN; System.out.println(n1 == n2); //false The idea is that NaN is a marker value for unknown or indeterminate. Comparing two unknown values should always … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)