Menu
  • HOME
  • TAGS

Special characters >: and <: meaning in scala?

scala,scala-2.10

The type keyword defines some abstract type. A specific DatabaseComponent subclass (a concrete implementation) can implement this type as a concrete class. The >: and <: are type bounds on the abstract type Session. The ones in the example mean that an abstract type member Session is null-able and it...

Strange behaviour of Scala in pattern matching in case of JSON fields (json4s framework)

scala,scala-2.10,json4s

The last three patterns are essentially the same because of type erasure on the JVM. Since a JSON array can contain multiple (Scala/Java/...) types it is difficult to match the type of the elements of the list. You could check only the first item of the array : case (f,...

Compare data in two RDD in spark

apache-spark,scala-2.10,cloudera-cdh,rdd

You need to key the data in each RDD so that there is something to join records on. Have a look at groupBy for example. Then you join the resulting RDDs. For each key, you get the matching values in both. If you are interested in finding the unmatched keys,...

Scala: Parse JSON directly into a case class

json,scala,parsing,scala-2.10,case-class

There are several frameworks which can exactly do that. JSON4s JSON4s is quite mature and supports jackson or a native JSON-Parser. Used it in many projects to replace jerkson. https://github.com/json4s/json4s play-json Can be used without the full play stack. Great support as part of the play project at typesafe. http://www.playframework.com/documentation/2.0/ScalaJson...

How to filter methods by return type with scala reflection?

scala,scala-2.10,scala-reflect

Use =:= to compare types. scala> import beans._ import beans._ scala> :pa // Entering paste mode (ctrl-D to finish) class VarAndValue { @BeanProperty val value = ""; } // Exiting paste mode, now interpreting. defined class VarAndValue scala> import reflect.runtime._, universe._ import reflect.runtime._ import universe._ scala> val str = typeOf[String]...

Scala regex pattern match groups different from that using findAllIn

regex,scala,scala-2.10

This is because you have not specified to what extent the second lazy match should go. So after = it consumes just one character and stops as it is in lazy mode. See here. https://regex101.com/r/dU7oN5/10 Change it to .+?=.+ to get full array...

TypeTags of type aliases with an upper type bound

scala,reflection,types,scala-2.10

You example actually works when you define the subclass of A explicitly, like this: object A { class Ab extends A { class B2 extends B type T = B2 def newT = new B2 } def apply() = new Ab } Alternatively you could use a WeakTypeTag, like this:...

Scala Object - get reference from string (in Scala 2.10)

scala,reflection,scala-2.10

Same in 2.10: Welcome to Scala version 2.11.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0). Type in expressions to have them evaluated. Type :help for more information. scala> import reflect.runtime._ import reflect.runtime._ scala> currentMirror staticModule "scala.Array" res0: reflect.runtime.universe.ModuleSymbol = object Array scala> currentMirror reflectModule res0 res1: reflect.runtime.universe.ModuleMirror = module mirror...

How to instantiate a Scala object using reflection

scala,reflection,scala-2.10,scala-2.11,scala-reflect

Found the answer: Refer to this link. Added the following code: import scala.reflect.runtime.universe._ def getObjectInstance(clsName: String):AnyRef = { val mirror = runtimeMirror(getClass.getClassLoader) val module = mirror.staticModule(clsName) mirror.reflectModule(module).instance.asInstanceOf[AnyRef] } val c:Class[_] = loadIt("Foo", "someJar.jar") // ok val o = try c.newInstance.asInstanceOf[AnyRef] catch { case i:java.lang.InstantiationException => getObjectInstance("Foo") } // works ...

Scala Class[_$1] where type _$1

scala,reflection,types,scala-2.10,jackson-modules

runtimeClass is retuning a Class with the wrong type parameter. Try: new JSONConverter(classTag[T].runtimeClass.asInstanceOf[Class[T]], bucketName(clientId)) ...

Scala inheritance in function signature

scala,inheritance,scala-2.10

Methods are contravariant in their argument types. Let me take your example and explain: val b: B = new B val y: Y = new Y b.run(y) // everything is ok val a: A = b // B is A val x: X = new X {} a.run(x) // run...

Vector of Any to Shapeless HList

scala,scala-collections,scala-2.10,scalaz,shapeless

Yes, it's possible, but you have to specify the types, and since this is a cast that can fail at runtime, you'll get the results wrapped in Option: import shapeless._, syntax.std.traversable._ val hlists = frame.map(_.toHList[Int :: String :: String :: Boolean :: HNil]) Now hlists has type Vector[Option[Int :: String...

How to convert an anonymous function to a method value?

scala,scala-2.10

I believe it's saying that you could simply have val foo = List('a', 'b', 'c') aString.forall(foo.contains) Note that we're not explicitly converting the foo.contains method here to an anonymous function....

Can Scala projects be compiled into web application

scala,scala-2.10

Scala code is converted into byte code after compilation, hence yes it can be done. here this should help for starting...

How do I eliminate type erasure warning?

scala,scala-2.10

Eliminate the u, eliminate the warning? [email protected]:~$ scala210 Welcome to Scala version 2.10.4 (OpenJDK 64-Bit Server VM, Java 1.7.0_25). Type in expressions to have them evaluated. Type :help for more information. scala> import scala.reflect.runtime.universe import scala.reflect.runtime.universe scala> import universe._ import universe._ scala> class X { val x = 7 ;...

NoSuchMethodError while running Scalatest

scala,scala-2.10,scalatest

I got the resolution. Thanks all for you reply. Problem is with my Scatatest vesrion. I am using Scala version 11 and scalatest version is not compatible with with Scala version. libraryDependencies += "org.scalatest" % "scalatest_2.11" % "2.2.1" % "test" Above line added in .sbt file and refreshed. Now it...

Traversing multiple JSON arrays in Play/Scala

json,scala,playframework,playframework-2.0,scala-2.10

If your eventual goal is to parse this into case classes, just define those case classes and let Play do the heavy lifting. case class Renting(name: String, pets: String) case class Resident(renting: List[Renting]) case class Location(residents: List[Resident]) implicit val rentingFormat = Json.format[Renting] implicit val residentFormat = Json.format[Resident] implicit val locationFormat...

Convert Vector to Tuple scala

scala,scala-collections,scala-2.10,scalaz7

Explicitly convert every inner Vector to Tuple3: vector.map { case Vector(f, s, t) => Tuple3(f, s, t) }.toList If you have vectors of variadic size you can use more general approach: def toTuple(seq: Seq[_]): Product = { val clz = Class.forName("scala.Tuple" + seq.size) clz.getConstructors()(0).newInstance(seq.map(_.asInstanceOf[AnyRef]): _*).asInstanceOf[Product] } vector.map(toTuple).toList But it has...

How to enable SQL on SchemaRDD via the JDBC interface? (is it even possible ?)

hive,apache-spark,scala-2.10,apache-spark-sql

createSchemaRDD code snippet from above works fine on spark 1.2.1 There was a CTAS defect in 1.2.0...

Suppressing @unchecked warning for a higher-kinded existential type

scala,scala-2.10,higher-kinded-types

With pattern matching you can keep the warnings away: x match {case _: Foo[_] => ???} It is also a bit less verbose in my opinion. In case you name the case variable (starting with lower case letter or escaped with back quotes, ie. not _ as in the above...

Scala type mismatch with generics

scala,generics,scala-2.10,type-mismatch

I found the problem: This is the head of LazyDataModel<T>: public abstract class LazyDataModel<T> extends DataModel<T> implements SelectableDataModel<T>, Serializable { /*...*/ } The class inherit from DataModel<T>. In my pom.xml I have <dependency> <groupId>javax.faces</groupId> <artifactId>javax.faces-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> and also <dependency> <groupId>javaee</groupId>...

Omit input data of map function in Scala

scala,apache-spark,scala-collections,scala-2.10

distinct and map are both methods on the RDD class (source), so distinct is just calling another method on the same RDD. The map function is a higher-order function - i.e. it accepts a function as one of its parameters (f: T => U) /** * Return a new RDD...

Scala: Using the App trait

scala,scala-2.10

Not sure I understand exactly what you are trying to do. How about that: trait OptionParser { protected def args: Array[String] } This way, you can mixin OptionParser with anything that provides a args, not necessarily App. Edit: since App defines args visibility as protected you need to do that...

How do I convert a Scala Map consisting of String and Seq[String] values to JSON?

json,scala,playframework,scala-2.10

Can you not change the map to val objMap = Map("id" -> Seq("id"), "tags" -> Seq("tag1", "tag2")) OR You could define a case class and an implicit writes, it is easier to define for a case class case class Post(id: String, tags: Seq[String]) implicit val postWrites = Json.writes[Post] val objMap...

Scala macro to print code?

scala,scala-2.10,scala-macros,scala-2.11,scala-macro-paradise

Dude, isn't an assert macro one of the basic use cases you implement to learn how to use macros? Well, that's what I thought, too. By "glean snippets" in my other answer I meant what specs2 does in its s2 macro. Or, you can do an arbitrary representation, as in...

Is it possible to convert a TypeTag to a Manifest?

scala,reflection,scala-2.10

gourlaysama's anwer uses Class[_], thus type arguments are being erased. I've come up with an implementation that preserves the type arguments here: How to maintain type parameter during TypeTag to Manifest conversion? Here's the code: def toManifest[T:TypeTag]: Manifest[T] = { val t = typeTag[T] val mirror = t.mirror def toManifestRec(t:...

Interrupt a long computation in Play 2 after a timeout

playframework,playframework-2.0,akka,scala-2.10

To fulfill the requirements of this question, it's necessary to be able to interrupt the long-running computation if its running time exceeds a maximum duration. Additionally, it's necessary to deal with the possibility of this interruption in the controller action. Assuming the computation involves several steps and/or repetitions, one approach...

Is there anyway we can give two conditions in Scalatest using ShouldMatchers

scala,scala-2.10,scalatest

You should be able to do val b: Option[Array[Int]] = ??? b should (be ('empty) or be (Some(Array.empty[Int])) See this section of the scalatest manual: Logical Expressions...

Scala: class type required but T found

scala,types,scala-2.10,trait

Here's an intermediary solution, though it leaves out the converter registration (which I may leave permanently for this use case, not sure yet). /** * trait for adding write methods to classes */ trait RiakWriteable[T] { /** * bucket name of data in Riak holding class data */ def bucketName:...

How to assign the same value to more var in scala

scala,variables,scala-2.10,scala-2.8

You can't do a = b = c because a has already been defined as a Int var, and with the a = b = c statement you are giving a a Unit, 'b = c'. When you assign a value to a variable in Scala you don't get as...