scala-2.8

Why does Scala define a “+=” operator for Short and Byte types?

独自空忆成欢 提交于 2020-01-02 04:09:07
问题 Given the following scala code: var short: Short = 0 short += 1 // error: type mismatch short += short // error: type mismatch short += 1.toByte // error: type mismatch I don't questioning the underlying typing - it's clear that "Short + value == Int". My questions are: 1. Is there any way at all that the operator can be used? 2. If not, then why is the operator available for use on Short & Byte? [And by extension *=, |= &=, etc.] 回答1: The problem seems to be that "+(Short)" on Short class is

Why does Scala define a “+=” operator for Short and Byte types?

孤人 提交于 2020-01-02 04:09:05
问题 Given the following scala code: var short: Short = 0 short += 1 // error: type mismatch short += short // error: type mismatch short += 1.toByte // error: type mismatch I don't questioning the underlying typing - it's clear that "Short + value == Int". My questions are: 1. Is there any way at all that the operator can be used? 2. If not, then why is the operator available for use on Short & Byte? [And by extension *=, |= &=, etc.] 回答1: The problem seems to be that "+(Short)" on Short class is

Scala MouseEvent - How to know which button was pressed?

大城市里の小女人 提交于 2020-01-02 02:31:06
问题 I'm writing a scala application using scala swing. I can listen for MouseClicked to get notified whenever the mouse is clicked, but how do i know which button was pressed. The documentation is pretty bad, so i have looked in the Java documentation for MouseEvent , which says that the key can be retrieved from the modifiers field, so i tried to output the modifiers field of the scala MouseClicked event, which turned out to be an integer, 0 for left click, mouse button 3 and mouse button 4 and

How to find same-value rectangular areas of a given size in a matrix most efficiently?

假如想象 提交于 2019-12-31 22:25:14
问题 My problem is very simple but I haven't found an efficient implementation yet. Suppose there is a matrix A like this: 0 0 0 0 0 0 0 4 4 2 2 2 0 0 4 4 2 2 2 0 0 0 0 2 2 2 1 1 0 0 0 0 0 1 1 Now I want to find all starting positions of rectangular areas in this matrix which have a given size. An area is a subset of A where all numbers are the same. Let's say width=2 and height=3. There are 3 areas which have this size: 2 2 2 2 0 0 2 2 2 2 0 0 2 2 2 2 0 0 The result of the function call would be

How to find same-value rectangular areas of a given size in a matrix most efficiently?

隐身守侯 提交于 2019-12-31 22:23:43
问题 My problem is very simple but I haven't found an efficient implementation yet. Suppose there is a matrix A like this: 0 0 0 0 0 0 0 4 4 2 2 2 0 0 4 4 2 2 2 0 0 0 0 2 2 2 1 1 0 0 0 0 0 1 1 Now I want to find all starting positions of rectangular areas in this matrix which have a given size. An area is a subset of A where all numbers are the same. Let's say width=2 and height=3. There are 3 areas which have this size: 2 2 2 2 0 0 2 2 2 2 0 0 2 2 2 2 0 0 The result of the function call would be

How to find same-value rectangular areas of a given size in a matrix most efficiently?

假如想象 提交于 2019-12-31 22:23:33
问题 My problem is very simple but I haven't found an efficient implementation yet. Suppose there is a matrix A like this: 0 0 0 0 0 0 0 4 4 2 2 2 0 0 4 4 2 2 2 0 0 0 0 2 2 2 1 1 0 0 0 0 0 1 1 Now I want to find all starting positions of rectangular areas in this matrix which have a given size. An area is a subset of A where all numbers are the same. Let's say width=2 and height=3. There are 3 areas which have this size: 2 2 2 2 0 0 2 2 2 2 0 0 2 2 2 2 0 0 The result of the function call would be

Scala: Implementing a subtype of Numeric[T]

浪尽此生 提交于 2019-12-31 10:51:46
问题 How does one go about implementing a subtype of Numeric[T]? I have been looking for at guide on this but haven't found any. Example of subtypes could be Rational or Complex? Thanks in advance Troels 回答1: An absolutely useless String Numeric: trait StringIsNumeric extends Numeric[String] { def plus(x: String, y: String): String = "%s+%s" format (x, y) def minus(x: String, y: String): String = "%s-%s" format (x) def times(x: String, y: String): String = "%s*%s" format (x, y) def quot(x: String,

Scala: Implementing a subtype of Numeric[T]

喜夏-厌秋 提交于 2019-12-31 10:51:09
问题 How does one go about implementing a subtype of Numeric[T]? I have been looking for at guide on this but haven't found any. Example of subtypes could be Rational or Complex? Thanks in advance Troels 回答1: An absolutely useless String Numeric: trait StringIsNumeric extends Numeric[String] { def plus(x: String, y: String): String = "%s+%s" format (x, y) def minus(x: String, y: String): String = "%s-%s" format (x) def times(x: String, y: String): String = "%s*%s" format (x, y) def quot(x: String,

Extend Scala Set with concrete type

女生的网名这么多〃 提交于 2019-12-30 10:43:26
问题 Really struggling to figure out extending the immutable Set with a class that will represent a Set of concrete type. I'm doing this to try and create a nice DSL. I'd like to have a class Thing, and when you add 'things' together you get a ThingSet object, which extends Set. class Thing(val name:String){ def +(other: Thing):ThingSet = new ThingSet() + other } I just can't figure out how to make the ThingSet object. I know I need to mix in traits like GenericSetTemplate, SetLike etc. But I just

Why is ClassManifest needed with Array but not List?

允我心安 提交于 2019-12-30 01:09:05
问题 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))