scala-2.8

What's the right way to use scala.io.Source?

不羁的心 提交于 2019-11-28 22:21:44
问题 In many examples, it is described that you can use scala.io.Source to read a whole file like this: val str = scala.io.Source.fromFile("test.txt").mkString() But closing the underlying stream is not mentioned. Why does Scala not provide a convenient way to do that such as with clause in Python? It looks useful but not difficult. Is there any other better way to do that safely in Scala, I means to read a whole file? 回答1: For the sake of completeness val testTxtSource = scala.io.Source.fromFile(

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

余生长醉 提交于 2019-11-28 19:05:50
For example, there is a Scala array val A = Array("please", "help", "me") . How to choose a random element from this array? topless import java.util.Random // ... val rand = new Random(System.currentTimeMillis()) val random_index = rand.nextInt(A.length) val result = A(random_index) import scala.util.Random val A = Array("please", "help", "me") Random.shuffle(A.toList).head import scala.util.Random val A = List(1, 2, 3, 4, 5, 6) A(Random.nextInt(A.size)) We can also add some safety with the Option monad (using the lift function, and a condition) Actually, if you use this function on Arrays

What's the new way to iterate over a Java Map in Scala 2.8.0?

半城伤御伤魂 提交于 2019-11-28 17:08:38
问题 How does scala.collection.JavaConversions supercede the answers given in Stack Overflow question Iterating over Java collections in Scala (it doesn't work because the "jcl" package is gone) and in Iterating over Map with Scala (it doesn't work for me in a complicated test which I'll try to boil down and post here later). The latter is actually a Scala Map question, but I think I need to know both answers in order to iterate over a java.util.Map . 回答1: In 2.8, you import scala.collection

Does the @inline annotation in Scala really help performance?

夙愿已清 提交于 2019-11-28 16:47:53
Or does it just clutter up the code for something the JIT would take care of automatically anyway? I have yet to find a case where it improves performance, and I've tried in quite a few different spots. The JVM seems to be quite good at inlining when it's possible, and even if you ask for @inline in Scala, it can't always do it (and sometimes I've noticed that it doesn't even when I think it ought to be able to). The place where you expect to see a bytecode difference is in something like this: object InlineExample { final class C(val i: Int) { @inline def t2 = i*2 @inline def t4 = t2*2 }

Function syntax puzzler in scalaz

*爱你&永不变心* 提交于 2019-11-28 13:30:54
问题 Following watching Nick Partidge's presentation on deriving scalaz, I got to looking at this example, which is just awesome: import scalaz._ import Scalaz._ def even(x: Int) : Validation[NonEmptyList[String], Int] = if (x % 2 ==0) x.success else "not even: %d".format(x).wrapNel.fail println( even(3) <|*|> even(5) ) //prints: Failure(NonEmptyList(not even: 3, not even: 5)) I was trying to understand what the <|*|> method was doing, here is the source code: def <|*|>[B](b: M[B])(implicit t:

Iterators for mutable collections in Scala?

情到浓时终转凉″ 提交于 2019-11-28 12:23:50
I just found out that there are such iterators in Java. Does Scala have iterators with 'set' and 'remove' methods for iterating (and modifying) mutable collections like array? If there is no such iterator then is there a good reason for that? Scala does not currently have such an iterator. I suspect that it does not because Such iterators are not general (i.e they are only usable with mutable collections) but consume namespace. Because they can rapidly become confusing to think about in conjunction with lazy operations like takeWhile (Is it always obvious what x.takeWhile(_<5).add(5) should do

How do I exclude/rename some classes from import in Scala?

大憨熊 提交于 2019-11-28 10:39:32
Language FAQ says import scala.collection.mutable.{_, Map => _, Set => _} should import all classes from package scala.collection.mutable , except Map and Set . But it gives me this error: error: '}' expected but ',' found. import scala.collection.mutable.{_, Map => _, Set => _} Is there still a way to do this? Patrick The _ has to be put at the end - not at the beginning: Exclude Map and Set from the import import scala.collection.mutable.{Map => _, Set => _, _} Exclude Set and rename Map to ScalaMutableMap import scala.collection.mutable.{Map=>ScalaMutableMap, Set => _, _} See the detailed

Scala: is it possible to override default case class constructor?

蓝咒 提交于 2019-11-28 09:44:56
Just wondering if this is possible. What I would actually like to do is check and possibly modify one of the arguments before it is stored as a val. Alternatively, I could use an overload and make the default constructor private. In which case I would also like to make private the default factory constructor in the companion object, how would I do that? Many thanks. Adam edit: well i figured out that making the default constructor private also makes the default factory constructor private, so i have a solution, i'm still interested to know if the default constructor is overridable though The

What are nested/unnested packages in Scala 2.8?

三世轮回 提交于 2019-11-28 08:12:13
In Scala 2.7, I could write: package com.acme.bar class Bar . package com.acme.foo class Foo { new bar.Bar } This doesn't compile in Scala 2.8 -- however this does: package com.acme package bar class Bar . package com.acme package foo class Foo { new bar.Bar } What was the motivation for this? What is the precise meaning, with regards to scope and visibility? When should I use one form over the other? There were several long discussions on the mailing lists about this. See this thread for the problem and this thread for the solution . As to meaning, only the package A package B form will open

How to create a wrapper of List with a specific type

。_饼干妹妹 提交于 2019-11-28 05:44:42
问题 I am trying to create a wrapper of List with a specific type (e.g. List[Int] ) such that methods that take an implicit CanBuildFrom parameter return an instance of my wrapper instead of List . One possible solution, which feels rather heavyweight, is: import scala.collection._ import generic.{CanBuildFrom, SeqForwarder} import mutable.{Builder, ListBuffer} class MyList(list: List[Int]) extends immutable.LinearSeq[Int] with LinearSeqLike[Int, MyList] with SeqForwarder[Int] { override def