How to express (implicit conv: String => A) as a view bound

I am asking myself what would be the view bound equivalent to(implicit conv: String => A)My first attempt was to simply declare the type parameter A as follows:[String <% A]But the Scala compiler complains with "not found: type A".Any suggestions? That is not a view bound. A view bound says that a type parameter A is bounded in that it can be viewed (converted to) as a type B. What you have inverted type and type parameter, so it doesn't qualify.To make things more clear, a bound is a limit on a "free" type -- a type parameter. For example:type A <: String // A ...

Why can't a class extend traits with method of the same signature?

Why is the error below? How to workaround it? EDIT: I assumed that since A and B compile to (interface,class) pairs, it's a matter of choosing the right static method call to implement when compiling C. I would expect the priority to be according to order. scala> trait A {def hi = println("A")}defined trait Ascala> trait B {def hi = println("B")}defined trait Bscala> class C extends B with A:6: error: error overriding method hi in trait B of type => Unit; method hi in trait A of type => Unit needs `override' modifier class C extends B with Ascala> trait A {override de...

Writing generic takeWhile with an offset in Scala

I have a use case for takeWhile, but where I want to keep a fixed number of items after predicate is true. I'm also trying to write it to be as generic as possible for the type of collection. So something like:def takeWhileWithOffset[A, Iter[_] <: Iterable[A]](iter: Iter[A], p: A => Boolean, offset: Int)I chose Iterable as the bound, because want it to work with Stream. But I'm having a tough time figuring out how to make this work. If I were using a strict collection, I could use dropRight if offset isn't positive. But Iterable doesn't have dropRight.The positive cas...

How to create a scala object in clojure

I'm trying to create a scala object, kafka.utils.ZKStringSerializer in clojure. (It's in org.apache.kafka/kafka_2.10 "0.8.0") Since I know little about scala, I don't know how to call its constructor. I tried like this:(import 'kafka.utils.ZKStringSerializer)(ZKStringSerializer.) ; or (new ZKStringSerializer)And got an error: CompilerException java.lang.IllegalArgumentException: No matching ctor found for class kafka.utils.ZKStringSerializerI tried using (clojure.reflect/reflect ZKStringSerializer) to see its methods but there are only some static metho...

could not find implicit value for evidence parameter for implicit json formats

I am trying to simply create a json format for my case classes: case class ImageRequest (inputs: Array[ImageObject])case class ImageObject(data: Data)case class Data(image: Image)case class Image(url: String)and here are the implicit formats: object ApplicationProtocol extends DefaultJsonProtocol { lazy implicit val ImageRequestFormat = jsonFormat(ImageRequest, "inputs") lazy implicit val ImageObjectFormat = jsonFormat(ImageObject, "data") lazy implicit val DataFormat = jsonFormat(Data, "image") lazy implicit val ImageFormat = jsonFormat(Image, "url") }I am get...

What was the reason to restrict on combining implicit parameters and view/context bounds?

One of the recent commits to Scala master removes restriction on combining context/view bounds with implicit parameters. That's a great improvement that reduces amount of boilerplate, but what was the reason of making that restriction before, and what consequences can we expect now? Context and view bounds add implicit parameters. So if you write:def f[T : Manifest](l: List[T])(implicit ord: Ordering[T]) There are, in fact, two implicit parameters being passed instead of one. Allowing this kind of syntax will certainly result in some confusion down the road. As implicit ...

Stuck at "Hello World" with IntelliJ IDEA 9.0.1 for Scala

I've been using Eclipse since 2.x and IDEs in general for over 20 years (since Turbo Pascal and Turbo C in the late '80s!).(that preamble is supposed to imply, "I'm not an idiot" ... but doesn't sound so smart as I read it... LOL :-] )Now I'm trying to use the Scala debugger in IntelliJ 9.0.1. I've resigned myself to an old standby, the "hello world" trick to check if the environment is setup correctly:class hello { def main(a: Array[String]) = println("got args: " + a)}I also tried this version, just in case:object hello extends Application { println("hi")}Alas, I'm un...

What are the differences between mapcat in Clojure and flatMap in Scala in terms of what they operate on?

I understand the equivalent to flatMap in Scala is mapcat in Clojure. I have an inkling that mapcat in clojure only works with sequences, unlike flatMap in Scala which is more flexible. My question is - what are the differences between mapcat in Clojure and flatMap in Scala in terms of what they operate on? Assumptions:I understand that Scala has a rich type system and Clojure has optional typing - I'm interested to know if the is a limitation in the parameters that mapcat accepts that make it only have a subset of flatMaps functionality. In the standard Scala library: ...

Using scala template code blocks and values as argument to template call (Play Framework)

I've tried to use both reusable code blocks and reusable values as argument to a function call (another template), but always end up in a compilation error: "illegal start of simple expression".Let's say I have the header template, which takes a string as argument. It is called this way: @header("My title")Now, I want "My title" to be the result of a code block, or a value.For example, I have tried this way for a code block:@headerText(workbookArea: WorkbookArea, workbookItemName: String) = @{ workbookArea.className + " > " + workbookItemName}@header(@headerText(workb...

How to use ScalaTest "contain allOf" on two lists?

I was looking for a ScalaTest matcher to check that a list contains all of the needed elements (given within another list), but that may also be others.contain allOf demands to get two fixed elements, for some reason, the rest as varargs.I can do a workaround like this, but it's tremendously ugly:val list = List(1,2,3,4)val wanted = List(1,2,3)list should contain allOf ( wanted.head, wanted.tail.head, wanted.tail.tail :_* ) // ugly workaroundFor giving a list as the match, there is contain theSameElementsAs. However, it does not allow extraneous elements to be in the pr...

How do I kill a RemoteActor?

Not sure whether I am missing something. When making an actor remote, the main method does not terminate. Here is a snippet that demonstrates the problem. import scala.actors._import scala.actors.remote._object TestMe { def main(args : Array[String]) : Unit = { object jim extends DaemonActor { // comment out these two lines and the application will terminate RemoteActor.alive(12345) RemoteActor.register('jim,this) def act { loop { receive { case 'quit => ...

Clojure to Scala translation: L System update

I'm attempting to model an L-system for a class project, but I'm struggling with Scala's reduce/fold and typesystem for achieving this end after many months of Clojure.In Clojure, I would writeuser> (defn update-state [translation-map, state] (mapcat #(get translation-map %1 [%1]) state))user> (def translations {"a" ["b", "a", "b"]})user> (update-state translations ["a"])["b", "a", "b"]This trick works because I implicitly defined (update-state "b") => ["b"], and I use clojure.core/mapcat to take care of concating the translated values together.So if we...

Why does it should "" {} type check in scalatest?

How does it should accept a String and then a function without parenthesis : import org.scalatest.FlatSpecimport scala.collection.mutable.Stackclass StackSpec extends FlatSpec { it should "pop values in last-in-first-out order" in { }}why should it not be : it(should("pop values in last-in-first-out order" in { }))Closest I came to allowing similar to compile is : object st { class fs { def it(f: => Unit) = { } def should(s: String)(f: => Unit): Unit = { Unit } it(should("pop values in last-in-first-out order") { }) }} The . to call func...

Monads for Control Flow - Sequence, Selection and Iteration

I can see how you'd use a Monad is Haskell for IO - to create a container around the computation for this operation. It makes sense that you can use Monads to 'plug computations together' - as you would compose functions for a dataflow operation. What I'm only just grokking is that you can use Monads for Control Flow. Now I understand Control flow to be about Sequence, Selection and Iteration. Now I'm comfortable with higher order functions like map, foldl, filter and zipwith/mapcat to perform operations on lists. My question is - can I do sequence, selection and iteration ...

Why a Play template expects Scala classes

The following template displays a PNG image by taking the image as a byte array and encoding it into Base64:@(img: Array[Byte])<img src="data:image/png;base64,@java.util.Base64.getEncoder.encodeToString(img)">The template works for a Play project that uses Scala as its main language:(lazy val root = (project in file(".")).enablePlugins(PlayScala))However, if a project uses Java as its main language:(lazy val root = (project in file(".")).enablePlugins(PlayJava))The following error is seen when running the project:[error] C:\Java\workspace\test\app\views\imgbase64.scal...

Scala Script Error on MAC OSX

I am receiving the following errors when I try to run a sample Hello world in Scala. The file has this statement saved in it. println("Hello World from a Scala Script!"). How to avoid these errors?Sandeeps-MacBook-Pro:ScalaScripts sandeep$ scala Hello.scala/Users/sandeep/Documents/ScalaScripts/Hello.scala:1: error: not found: value \{\rtf1\ansi\ansicpg1252\cocoartf1187\cocoasubrtf370 ^/Users/sandeep/Documents/ScalaScripts/Hello.scala:3: error: not found: value \{\colortbl;\red255\green255\blue255;\red38\green38\blue38;} ^/Users/sandeep/Documents/ScalaScripts/Hello.scala:3:...

Page 1 of 136  |  Show More Pages:  Top Prev Next Last