scala-2.8

What is the difference between a view and a stream?

回眸只為那壹抹淺笑 提交于 2019-12-02 20:28:43
In the Scala 2.8 collections framework, what is the difference between view and toStream ? In a view elements are recomputed each time they are accessed. In a stream elements are retained as they are evaluated. For example: val doubled = List(1,2,3,4,5,6,7,8,9,10).view.map(_*2) println(doubled.mkString(" ")) println(doubled.mkString(" ")) will re-evaluate the map for each element twice. Once for the first println, and again for the second. In contrast val doubled = List(1,2,3,4,5,6,7,8,9,10).toStream.map(_*2) println(doubled.mkString(" ")) println(doubled.mkString(" ")) will only double the

In Scala, is there a way to take convert two lists into a Map?

耗尽温柔 提交于 2019-12-02 20:19:35
I have a two lists, a List[A] and a List[B] . What I want is a Map[A,B] but I want the semantics of zip . So started out like so: var tuplesOfAB = listOfA zip listOfB Now I'm not sure how to construct a Map from my tuplesOfAB . As a follow-up question, I also want to invert my map so that from a Map[A,B] I can create a Map[B,A] . Can anyone hit me with a clue-stick? oxbow_lakes In 2.8 this is really simple using the CanBuildFrom functionality ( as described by Daniel ) and using breakOut with a type instruction to the compiler as to what the result type should be: import scala.collection

Using scala.util.control.Exception

拥有回忆 提交于 2019-12-02 19:05:45
Does anybody have good examples of using scala.util.control.Exception version 2.12.0 ( version 2.8.0 ), ? I am struggling to figure it out from the types. Indeed - I also find it pretty confusing! Here's a problem where I have some property which may be a parseable date: def parse(s: String) : Date = new SimpleDateFormat("yyyy-MM-dd").parse(s) def parseDate = parse(System.getProperty("foo.bar")) type PE = ParseException import scala.util.control.Exception._ val d1 = try { parseDate } catch { case e: PE => new Date } Switching this to a functional form: val date = catching(classOf[PE]) either

Why does the type parameter bound T <: Comparable[T] fail for T = Int?

我的梦境 提交于 2019-12-01 14:04:29
问题 scala> class Foo[T <: Comparable[T]](val x : T) defined class Foo scala> (3: Int).asInstanceOf[Comparable[Int]] res60: java.lang.Comparable[Int] = 3 scala> new Foo(3) <console>:13: error: inferred type arguments [Int] do not conform to class Foo's type parameter bounds [T <: java.lang.Comparable[T]] new Foo(3) ^ Is the 2nd expression the result of type erasure? How would I go about defining Foo so that I could parameterize it with Int but still be able to perform some ordering behavior with

Help Drools integration with Scala

我的梦境 提交于 2019-12-01 09:04:54
I am new with Drools. I am integrating Drools with Scala. I am trying the project given at http://www.gettingcirrius.com/2010/12/using-jboss-rules-drools-in-scala.html . I have made a small change in the code to supply the Drools rule file as given below: File f = new File("WeatherRules.drl"); kbuilder.add(ResourceFactory.newFileResource(f), ResourceType.DRL); The code is running fine and there is no error but the rules are not working. The output I got is: Creating Knowledge Session Creating and insertng Temperature Firing all rules It seems that the configured rules are not working. The

Help Drools integration with Scala

拜拜、爱过 提交于 2019-12-01 07:04:57
问题 I am new with Drools. I am integrating Drools with Scala. I am trying the project given at http://www.gettingcirrius.com/2010/12/using-jboss-rules-drools-in-scala.html. I have made a small change in the code to supply the Drools rule file as given below: File f = new File("WeatherRules.drl"); kbuilder.add(ResourceFactory.newFileResource(f), ResourceType.DRL); The code is running fine and there is no error but the rules are not working. The output I got is: Creating Knowledge Session Creating

How to define a ternary operator in Scala which preserves leading tokens?

末鹿安然 提交于 2019-12-01 03:38:46
I'm writing a code generator which produces Scala output. I need to emulate a ternary operator in such a way that the tokens leading up to '?' remain intact. e.g. convert the expression c ? p : q to c something . The simple if(c) p else q fails my criteria, as it requires putting if( before c . My first attempt (still using c/p/q as above) is c match { case(true) => p; case _ => q } another option I found was: class ternary(val g: Boolean => Any) { def |: (b:Boolean) = g(b) } implicit def autoTernary (g: Boolean => Any): ternary = new ternary(g) which allows me to write: c |: { b: Boolean =>

Iterating circular way

六眼飞鱼酱① 提交于 2019-12-01 02:49:38
I need iterate through a List but circular way. I need too add new elements to the list and iterate over all elements (olds and news elements), How I do it? Is there any data structure for them? I think maybe this is what you want; the ability to add new elements to your list even as you are iterating it. The code is ugly but it seems to work. import scala.collection.mutable.Queue class Circular[A](list: Seq[A]) extends Iterator[A]{ val elements = new Queue[A] ++= list var pos = 0 def next = { if (pos == elements.length) pos = 0 val value = elements(pos) pos = pos + 1 value } def hasNext =

In Scala, how can a constructor refer to the object it is creating?

与世无争的帅哥 提交于 2019-12-01 00:37:02
I want to implement a prototype-based system in Scala. At the root of the type hierarchy is the ROOT node, which has a prototype that refers to itself. The following code demonstrates what I'm trying to do: class Node(val prototype: Node) { private def this() = this(this) } object Node { val ROOT = new Node } Unfortunately, this does not compile an error: "this can only be used in a class, object, or template". The argument "this" for the call to the primary constructor is not accepted. This sounds reasonable, since the object is not yet created. However, since prototype is immutable, I can't

How to define a ternary operator in Scala which preserves leading tokens?

流过昼夜 提交于 2019-12-01 00:21:08
问题 I'm writing a code generator which produces Scala output. I need to emulate a ternary operator in such a way that the tokens leading up to '?' remain intact. e.g. convert the expression c ? p : q to c something . The simple if(c) p else q fails my criteria, as it requires putting if( before c . My first attempt (still using c/p/q as above) is c match { case(true) => p; case _ => q } another option I found was: class ternary(val g: Boolean => Any) { def |: (b:Boolean) = g(b) } implicit def