scala-2.8

Iterating circular way

时间秒杀一切 提交于 2019-11-30 22:35:48
问题 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? 回答1: 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

Passing Java array to Scala

六眼飞鱼酱① 提交于 2019-11-30 20:17:57
Although I've been using Scala for a while and have mixed it with Java before, I bumped on a problem. How can I pass a Java array to Scala? I know that the other way around is fairly straightforward. Java to Scala is not so however. Should I declare my method in Scala? Here is a small example of what I'm trying to achieve: Scala: def sumArray(ar: Array[Int]) = ... Java: RandomScalaClassName.sumArray(new int[]{1,2,3}); Is this possible? absolutely! The Array[T] class in Scala is mapped directly to the Java type T[] . They both have exactly the same representation in bytecode. At least, this is

Equality relations in Scala

这一生的挚爱 提交于 2019-11-30 19:26:46
I just stumbled on one of Tony Morris' blog-posts about Java and a fundamental problem with the language: that of defining a bespoke equality-relation for a collection. This is something that I think is a big deal and wondered whether there was some scala solution. The classic issue manifests itself in thinking about, say, a trade. Let's say I make two trades of +100 vodafone shares @150p. The two trades are equal, yes? Except they are not the same trade . In the case of a normal real-world system, with persistence or serialization, I cannot rely on identity to tell me whether two references

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

时光怂恿深爱的人放手 提交于 2019-11-30 18:51:21
问题 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

How do I use the trait scala.Proxy

走远了吗. 提交于 2019-11-30 18:24:34
I just found it in the API and would like to see one or two examples along with an explanation what it is good for. The Proxy trait provides a useful basis for creating delegates, but note that it only provides implementations of the methods in Any ( equals , hashCode , and toString ). You will have to implement any additional forwarding methods yourself. Proxy is often used with the pimp-my-library pattern : class RichFoo(val self: Foo) extends Proxy { def newMethod = "do something cool" } object RichFoo { def apply(foo: Foo) = new RichFoo(foo) implicit def foo2richFoo(foo: Foo): RichFoo =

What is the concept of “weak conformance” in Scala?

≯℡__Kan透↙ 提交于 2019-11-30 11:21:59
问题 I just recently encountered the term "Weak Conformance" (in Stack Overflow user retronym's answer to How to set up implicit conversion to allow arithmetic between numeric types? ). What is it? 回答1: 3.5.3 Weak Conformance In some situations Scala uses a more general conformance relation. A type S weakly conforms to a type T , written S <: w T , if S <: T or both S and T are primitive number types and S precedes T in the following ordering. Byte <: w Short Byte <: w Character Short <: w Int Int

What are the good Scala IDEs at the start of 2010?

烂漫一生 提交于 2019-11-30 11:01:30
I know this is an exact duplicate , but a year has gone by and Scala seems to be a fast moving thing, so I figure it might be acceptable to ask again: What is the best IDE for Scala development right now? I know the Eclipse plugin is a work in progress and undergoing a complete re-write for Scala 2.8 but I saw a colleague use a nightly-build recently and it was extremely poor. I use IntelliJ IDEA (the Community Edition 9 is free) and the scala plugin for it is really good. Excellent syntax highlighting, code navigation etc. It's not as good as the Java support but then I wouldn't necessarily

Scala - implicit conversion of Int to Numeric[Int]

早过忘川 提交于 2019-11-30 09:47:46
I've created a class that can be parameterised by anything that can be converted to Numeric class Complex[T <% Numeric[T]] (val real : T, val imag : T) { //... complex number methods ... } Then elsewhere in the code I try: var myComplex = new Complex(0, 1) This raises a compilation error because (surprisingly) there's no implicit conversion between Int and Numeric[Int] or even between Int and Integral[Int]. Am I missing something? Is there an implicit conversion somewhere I'm not seeing? There's an implicit object called IntIsIntegral defined in Numeric.scala. I've tried using this to create

How to choose a random element from an array in Scala?

不问归期 提交于 2019-11-30 06:28:56
问题 For example, there is a Scala array val A = Array("please", "help", "me") . How to choose a random element from this array? 回答1: import java.util.Random // ... val rand = new Random(System.currentTimeMillis()) val random_index = rand.nextInt(A.length) val result = A(random_index) 回答2: import scala.util.Random val A = Array("please", "help", "me") Random.shuffle(A.toList).head 回答3: import scala.util.Random val A = List(1, 2, 3, 4, 5, 6) A(Random.nextInt(A.size)) 回答4: We can also add some

Why is ClassManifest needed with Array but not List?

此生再无相见时 提交于 2019-11-30 05:03:20
Define the following code: import scala.collection.JavaConversions._ val iter:java.util.Iterator[Any] = Array[Any](1, 2, 3).iterator def func(a:Any):String = a.toString def test[T:ClassManifest](iter:java.util.Iterator[Any], func:Any=>T):Array[T] = iter.map(i=>func(i)).toArray def testFunc = test(iter, func) Here, I need to use ClassManifest for it to compile correctly, otherwise I get the error: scala> def test[T](iter:java.util.Iterator[Any], func:Any=>T):Array[T] = | iter.map(i=>func(i)).toArray <console>:11: error: could not find implicit value for evidence parameter of type ClassManifest