scala-2.10

Scala: Parse JSON directly into a case class

坚强是说给别人听的谎言 提交于 2019-11-30 11:48:13
问题 Given a string of JSON, and a case class that corresponds to it, what's a simple way to parse the JSON into the case class? There are many libraries available, but it seems that Scala might now do this out of the box. What about if the JSON should be parsed into a list of the case class? UPDATE: Jerkson seems to be abandoned, and I don't want to install the full Play or Lift framework or anything else heavy. 回答1: There are several frameworks which can exactly do that. circe Used a lot nowdays

Is it possible to use IN clause in plain sql Slick?

99封情书 提交于 2019-11-30 11:16:19
For example, I want to create the following query: SELECT c.* FROM Coffees c WHERE c.name IN ('robusta', 'arabica') My attempt failed: val cnames = List("robusta", "arabica") sql""" SELECT c.* FROM Coffees c WHERE c.name IN ${cnames} """ could not find implicit value for parameter pconv: scala.slick.jdbc.SetParameter[List[String]] Is it possible to somehow use in clause in Slick plain sql queries? I don't see anything out of the box to handle this. You're best bet is probably something like this: val cnames = List("robusta", "arabica").map("'" + _ + "'").mkString(",") val query = sql""" SELECT

converting Akka's Future[A] to Future[Either[Exception,A]]

纵然是瞬间 提交于 2019-11-30 09:49:17
Is there a method in Akka (or in the standard library in Scala 2.10) to convert a Future[A] which might fail into a Future[Either[Exception,A]] ? I know that you can write f.map(Right(_)).recover { case e:Exception => Left(e) } It just seems to be such a common task that I wonder whether I have overlooked something. I'm interested in answers for Scala 2.9/Akka and Scala 2.10. The primary reason why this method is missing is that it does not really have good semantics: the static type Future[Either[Throwable, T]] does not ensure that that future cannot fail, hence the type change does not gain

Kill or timeout a Future in Scala 2.10

点点圈 提交于 2019-11-30 03:19:02
问题 Hi, I'm using Scala 2.10 with the new futures library and I'm trying to write some code to test an infinite loop. I use a scala.concurrent.Future to run the code with the loop in a separate thread. I would then like to wait a little while to do some testing and then kill off the separate thread/future. I have looked at Await.result but that doesn't actually kill the future. Is there any way to timeout or kill the new Scala 2.10 futures? I would prefer not having to add external dependencies

How do I set the default number of threads for Scala 2.10 parallel collections?

和自甴很熟 提交于 2019-11-30 01:39:21
问题 In Scala before 2.10, I can set the parallelism in the defaultForkJoinPool (as in this answer scala parallel collections degree of parallelism). In Scala 2.10, that API no longer exists. It is well documented that we can set the parallelism on a single collection (http://docs.scala-lang.org/overviews/parallel-collections/configuration.html) by assigning to its taskSupport property. However, I use parallel collections all over my codebase and would not like to add an extra two lines to every

Scala: Parse JSON directly into a case class

拜拜、爱过 提交于 2019-11-30 01:22:43
Given a string of JSON, and a case class that corresponds to it, what's a simple way to parse the JSON into the case class? There are many libraries available, but it seems that Scala might now do this out of the box. What about if the JSON should be parsed into a list of the case class? UPDATE: Jerkson seems to be abandoned, and I don't want to install the full Play or Lift framework or anything else heavy. There are several frameworks which can exactly do that. circe Used a lot nowdays. Many great features. Will pull cats in. https://circe.github.io/circe/ https://github.com/circe/circe

How do the new Scala TypeTags improve the (deprecated) Manifests? [duplicate]

核能气质少年 提交于 2019-11-29 22:58:49
Possible Duplicate: Scala 2.10: What is a TypeTag and how do I use it? I have been reading about the new TypeTags which come along with the new reflection api. It seems that Manifests are supposed to be replaced with that new concept. Can anyone post some code examples to show the benefits? Some references: TypeTags API SIP: Self-cleaning macros Metaprogramming in Scala Manifests are a lie. It has no knowledge of variance (assumes all type parameters are co-variants), and it has no support for path-dependent, existential or structural types. TypeTags are types as the compiler understands them

Documenting Scala 2.10 macros [closed]

落爺英雄遲暮 提交于 2019-11-29 20:07:00
I'll start with an example. Here's an equivalent of List.fill for tuples as a macro in Scala 2.10: import scala.language.experimental.macros import scala.reflect.macros.Context object TupleExample { def fill[A](arity: Int)(a: A): Product = macro fill_impl[A] def fill_impl[A](c: Context)(arity: c.Expr[Int])(a: c.Expr[A]) = { import c.universe._ arity.tree match { case Literal(Constant(n: Int)) if n < 23 => c.Expr( Apply( Select(Ident("Tuple" + n.toString), "apply"), List.fill(n)(a.tree) ) ) case _ => c.abort( c.enclosingPosition, "Desired arity must be a compile-time constant less than 23!" ) }

How can I get the actual object referred to by Scala 2.10 reflection?

一世执手 提交于 2019-11-29 19:08:04
问题 Please consider this code: object ResponseType extends Enumeration { val Listing, Album = Value } I can get the Symbol referring to this object like so: import reflect.runtime.universe._ val s = typeOf[ResponseType.Value].asInstanceOf[TypeRef].pre.typeSymbol Now, having this symbol, how can I get the actual ResponseType object? 回答1: scala> val moduleClass = typeOf[ResponseType.Value].asInstanceOf[TypeRef].pre.typeSymbol moduleClass: reflect.runtime.universe.Symbol = object ResponseType scala>

Is it possible to use IN clause in plain sql Slick?

孤者浪人 提交于 2019-11-29 16:52:20
问题 For example, I want to create the following query: SELECT c.* FROM Coffees c WHERE c.name IN ('robusta', 'arabica') My attempt failed: val cnames = List("robusta", "arabica") sql""" SELECT c.* FROM Coffees c WHERE c.name IN ${cnames} """ could not find implicit value for parameter pconv: scala.slick.jdbc.SetParameter[List[String]] Is it possible to somehow use in clause in Slick plain sql queries? 回答1: I don't see anything out of the box to handle this. You're best bet is probably something