scala-2.8

How to get Ponter/Reference semantics in Scala

自闭症网瘾萝莉.ら 提交于 2019-12-12 12:08:27
问题 In C++ I would just take a pointer (or reference) to arr[idx]. In Scala I find myself creating this class to emulate a pointer semantic. class SetTo (val arr : Array[Double], val idx : Int) { def apply (d : Double) { arr(idx) = d } } Isn't there a simpler way? Doesn't Array class have a method to return some kind of reference to a particular field? 回答1: Arrays used in Scala are JVM arrays (in 2.8) and JVM arrays have no concept of a slot reference. The best you can do is what you illustrate.

Strange behavior of Set4 in scala 2.9.1?

橙三吉。 提交于 2019-12-12 09:35:44
问题 Making a migration from 2.8.1 to 2.9.1 found interesting thing. Tried to write this in console: >>import collection.immutable.Set.Set4 >>new Set4[Int](1,2,3,4) It gives: java.lang.Error: Unexpected New at scala.tools.nsc.symtab.SymbolTable.abort(SymbolTable.scala:34) at scala.tools.nsc.backend.icode.GenICode$ICodePhase.scala$tools$nsc$bac .......................... That entry seems to have slain the compiler. Shall I replayscala:660) your session? I can re-run each line except the last one

scala Remove (in place) all elements of a ListBuffer that meet a condition

情到浓时终转凉″ 提交于 2019-12-12 07:44:05
问题 I have a ListBuffer. I want to remove all elements that meet a certain condition. I could iterate over it and remove each element. But what doe Scala say about mutating a list that you are iterating over? Will it work, or will it delete the wrong elements/not return all elements? (A quick attempt with the REPL suggests that yes, it will mess up) I could repeatedly call find and then remove the found element until I don't find any more, but that sounds inefficient. .filter will return me a new

What do <:<, <%<, and =:= mean in Scala 2.8, and where are they documented?

本小妞迷上赌 提交于 2019-12-12 02:19:00
问题 I can see in the API docs for Predef that they're subclasses of a generic function type (From) => To, but that's all it says. Um, what? Maybe there's documentation somewhere, but search engines don't handle "names" like "<:<" very well, so I haven't been able to find it. Follow-up question: when should I use these funky symbols/classes, and why? 回答1: These are called generalized type constraints . They allow you, from within a type-parameterized class or trait, to further constrain one of its

In a multidimensional sequence created with tabulate, is the innermost seq the 1. dimension?

让人想犯罪 __ 提交于 2019-12-11 13:57:23
问题 As the title says, when creating a multidimensional sequence in scala with tabulate, is the innermost or outermost sequence the 1. dimension? For example, in a 2-dimensional Vector v, will v(2) give the second element of the 1. or of the 2. dimension? 回答1: scala> Array.tabulate(2,3)(_ + 3*_) res2: Array[Array[Int]] = Array(Array(0, 3, 6), Array(1, 4, 7)) As you can see, the first number refers to the outermost grouping, while the second refers to the second (and so on). In general, all the

Need help with Continuations-Error “found cps expression in non-cps position”

六眼飞鱼酱① 提交于 2019-12-11 02:09:36
问题 I try building the following simple Generator using the Scala 2.8 Continuations-PlugIn. Where does the following error come from? None/None/Some((Unit,Unit)) GenTest.scala:8: error: found cps expression in non-cps position yieldValue(1) None/None/Some((Unit,Unit)) GenTest.scala:9: error: found cps expression in non-cps position yieldValue(2) None/None/Some((Unit,Unit)) GenTest.scala:10: error: found cps expression in non-cps position yieldValue(3) Code: import scala.util.continuations._

Pass null to a method expects Long

纵然是瞬间 提交于 2019-12-10 18:21:25
问题 I have a Scala method that takes 2 parameters: def test(x:Long,y:Int){} On some occasion I need to pass null instead of long ... something like that: test(null,x) The result: scala> test(null,2) :7: error: type mismatch; found : Null(null) required: Long test(null,2) Why do I need to pass null? Actually ,for some reason,I can't pass any default values. Thus, I need such a null. * Note: *I know that the solution would be making it Option. However let's say I have no control over this method

What are the limitations and walkarounds of Scala interpreter?

不问归期 提交于 2019-12-10 09:34:58
问题 What kind of constructs need 'scalac' compile and how to make an equivalent that will work in interpreter? Edit: I want to use scala instead of python as a scripting language. (with #!/usr/bin/scala) 回答1: You ought to be able to do anything in the REPL that you can do in outside code. Keep in mind that: Things that refer to each other circularly need to be inside one block. So the following can't be entered as-is; you have to wrap it inside some other object: class C(i : Int) { def succ = C(i

How do I create an XML root node in Scala without a literal element name?

こ雲淡風輕ζ 提交于 2019-12-10 03:43:49
问题 I'm looking to create a document like this: <root/> That I can add children to programatically. Theoretically, it would look like this: val root_node_name = "root" val doc = <{root_node_name}/> But that doesn't seem to work: error: not found: value < So, what I tried instead was this: val root_node_name = "root" val doc = new scala.xml.Elem(null, root_node_name, null, scala.xml.TopScope, null) That compiles but at runtime I get this null pointer exception: java.lang.NullPointerException at

Scala collection type for filter

北战南征 提交于 2019-12-10 00:44:49
问题 Assume you have a List(1,"1") it is typed List[Any], which is of course correct and expected. Now if I map the list like this scala> List(1, "1") map { | case x: Int => x | case y: String => y.toInt | } the resulting type is List[Int] which is expected as well. My question is if there is an equivalent to map for filter because the following example will result in a List[Any]. Is this possible? I assume this could be solved at compile time and possibly not runtime? scala> List(1, "1") filter {