112,704
questions
871
votes
18
answers
116k
views
Is the Scala 2.8 collections library a case of "the longest suicide note in history"? [closed]
I have just started to look at the Scala collections library re-implementation which is coming in the imminent 2.8 release. Those familiar with the library from 2.7 will notice that the library, from ...
746
votes
13
answers
246k
views
Difference between object and class in Scala
I'm just going over some Scala tutorials on the Internet and have noticed in some examples an object is declared at the start of the example.
What is the difference between class and object in Scala?
676
votes
6
answers
180k
views
Scala vs. Groovy vs. Clojure [closed]
Can someone please explain the major differences between Scala, Groovy and Clojure. I know each of these compiles to run on the JVM but I'd like a simple comparison between them.
622
votes
12
answers
179k
views
What are the best use cases for Akka framework [closed]
I have heard lots of raving about Akka framework (Java/Scala service platform), but so far have not seen many actual examples of use cases it would be good for. So I would be interested in hearing ...
611
votes
7
answers
173k
views
What are all the uses of an underscore in Scala?
I've taken a look at the list of surveys taken on scala-lang.org and noticed a curious question: "Can you name all the uses of “_”?". Can you? If yes, please do so here. Explanatory examples are ...
495
votes
17
answers
151k
views
What is the difference between Scala's case class and class?
I searched in Google to find the differences between a case class and a class. Everyone mentions that when you want to do pattern matching on the class, use case class. Otherwise use classes and also ...
449
votes
1
answer
14k
views
Scalaz iteratees: "Lifting" `EnumeratorT` to match `IterateeT` for a "bigger" monad
If I have an EnumeratorT and a corresponding IterateeT I can run them together:
val en: EnumeratorT[String, Task] = EnumeratorT.enumList(List("a", "b", "c"))
val it: IterateeT[String, Task, Int] = ...
438
votes
11
answers
137k
views
What do all of Scala's symbolic operators mean?
Scala syntax has a lot of symbols. Since these kinds of names are difficult to find using search engines, a comprehensive list of them would be helpful.
What are all of the symbols in Scala, and what ...
438
votes
2
answers
58k
views
Where does Scala look for implicits?
An implicit question to newcomers to Scala seems to be: where does the compiler look for implicits? I mean implicit because the question never seems to get fully formed, as if there weren't words for ...
422
votes
11
answers
75k
views
What is the difference between self-types and trait subclasses?
A self-type for a trait A:
trait B
trait A { this: B => }
says that "A cannot be mixed into a concrete class that does not also extend B".
On the other hand, the following:
trait B
trait A ...
405
votes
8
answers
109k
views
What is the advantage of using abstract classes instead of traits?
What is the advantage of using an abstract class instead of a trait (apart from performance)? It seems like abstract classes can be replaced by traits in most cases.
404
votes
4
answers
213k
views
Scala list concatenation, ::: vs ++
Is there any difference between ::: and ++ for concatenating lists in Scala?
scala> List(1,2,3) ++ List(4,5)
res0: List[Int] = List(1, 2, 3, 4, 5)
scala> List(1,2,3) ::: List(4,5)
res1: List[...
390
votes
11
answers
85k
views
How do I get around type erasure on Scala? Or, why can't I get the type parameter of my collections?
It's a sad fact of life on Scala that if you instantiate a List[Int], you can verify that your instance is a List, and you can verify that any individual element of it is an Int, but not that it is a ...
384
votes
5
answers
202k
views
Difference between a Seq and a List in Scala
I've seen in many examples that sometimes a Seq is being used, while other times is the List...
Is there any difference, other than the former one being a Scala type and the List coming from Java?
384
votes
1
answer
81k
views
What is a TypeTag and how do I use it?
All I know about TypeTags is that they somehow replaced Manifests. Information on the Internet is scarce and doesn't provide me with a good sense of the subject.
So I'd be happy if someone shared a ...
379
votes
6
answers
117k
views
What is a sealed trait?
Sealed classes are described in 'Programming in Scala', but sealed traits are not.
Where can I find more information about a sealed trait?
I would like to know, if a sealed trait is the same as a ...
373
votes
9
answers
64k
views
What is the formal difference in Scala between braces and parentheses, and when should they be used?
What is the formal difference between passing arguments to functions in parentheses () and in braces {}?
The feeling I got from the Programming in Scala book is that Scala's pretty flexible and I ...
366
votes
7
answers
158k
views
What is the apply function in Scala?
I never understood it from the contrived unmarshalling and verbing nouns ( an AddTwo class has an apply that adds two!) examples.
I understand that it's syntactic sugar, so (I deduced from context) ...
364
votes
19
answers
280k
views
Read entire file in Scala?
What's a simple and canonical way to read an entire file into memory in Scala? (Ideally, with control over character encoding.)
The best I can come up with is:
scala.io.Source.fromPath("file.txt&...
357
votes
4
answers
30k
views
What does "coalgebra" mean in the context of programming?
I have heard the term "coalgebras" several times in functional programming and PLT circles, especially when the discussion is about objects, comonads, lenses, and such. Googling this term gives pages ...
347
votes
11
answers
155k
views
What is Scala's yield?
I understand Ruby and Python's yield. What does Scala's yield do?
347
votes
7
answers
160k
views
Understanding implicit in Scala
I was making my way through the Scala playframework tutorial and I came across this snippet of code which had me puzzled:
def newTask = Action { implicit request =>
taskForm.bindFromRequest.fold(
...
335
votes
17
answers
492k
views
How to show full column content in a Spark Dataframe?
I am using spark-csv to load data into a DataFrame. I want to do a simple query and display the content:
val df = sqlContext.read.format("com.databricks.spark.csv").option("header", "true").load("my....
331
votes
13
answers
133k
views
What is the difference between a var and val definition in Scala?
What is the difference between a var and val definition in Scala and why does the language need both? Why would you choose a val over a var and vice versa?
328
votes
11
answers
710k
views
How to use java.String.format in Scala?
I am trying to use a .format method of a string. But if I place %1, %2, etc. in the string, java.util.UnknownFormatConversionException is thrown pointing to a confusing Java source code piece:
...
319
votes
1
answer
174k
views
Scala: join an iterable of strings
How do I "join" an iterable of strings by another string in Scala?
val thestrings = Array("a","b","c")
val joined = ???
println(joined)
I want this code to output a,b,c (join the elements by ",").
317
votes
19
answers
263k
views
How do I break out of a loop in Scala?
How do I break out a loop?
var largest=0
for(i<-999 to 1 by -1) {
for (j<-i to 1 by -1) {
val product=i*j
if (largest>product)
// I want to break out here
...
315
votes
7
answers
108k
views
How to model type-safe enum types?
Scala doesn't have type-safe enums like Java has. Given a set of related constants, what would be the best way in Scala to represent those constants?
307
votes
5
answers
81k
views
What is a higher kinded type in Scala?
You can find the following on the web:
Higher kinded type == type constructor?
class AClass[T]{...} // For example, class List[T]
Some say this is a higher kinded type, because it
abstracts over ...
289
votes
1
answer
43k
views
What are Scala context and view bounds?
In a simple way, what are context and view bounds and what is the difference between them?
Some easy-to-follow examples would be great too!
286
votes
4
answers
48k
views
What is "lifting" in Scala?
Sometimes when I read articles in the Scala ecosystem I read the term "lifting" / "lifted". Unfortunately, it is not explained what that exactly means. I did some research, and it seems that lifting ...
281
votes
12
answers
85k
views
Difference between method and function in Scala
I read Scala Functions (part of Another tour of Scala). In that post he stated:
Methods and functions are not the same thing
But he didn't explain anything about it. What was he trying to say?
274
votes
1
answer
7k
views
How to use Shapeless in a Quasiquote?
I'm trying to call a Shapeless macro from inside a quasiquote with Scala and I'm not getting what I would like to get.
My macro doesn't return any errors but it doesn't expand Witness(fieldName) ...
273
votes
5
answers
43k
views
Scala: Abstract types vs generics
I was reading A Tour of Scala: Abstract Types. When is it better to use abstract types?
For example,
abstract class Buffer {
type T
val element: T
}
rather that generics, for example,
abstract ...
272
votes
7
answers
128k
views
What does a lazy val do?
I noticed that Scala provide lazy vals. But I don't get what they do.
scala> val x = 15
x: Int = 15
scala> lazy val y = 13
y: Int = <lazy>
scala> x
res0: Int = 15
scala> y
res1: ...
263
votes
9
answers
67k
views
Scala Programming for Android
I have followed the tutorial at Scala and Android with Scala 2.7.3 final. The resulting Android App works but even the most basic application takes several minutes (!) to compile and needs 900 kb ...
262
votes
26
answers
162k
views
How do I parse command line arguments in Scala? [closed]
What is a good way of parsing command line arguments in Scala?
Related:
How do I parse command line arguments in Java?
258
votes
11
answers
255k
views
Task not serializable: java.io.NotSerializableException when calling function outside closure only on classes not objects
Getting strange behavior when calling function outside of a closure:
when function is in a object everything is working
when function is in a class get :
Task not serializable: java.io....
257
votes
17
answers
103k
views
Call by name vs call by value in Scala, clarification needed
As I understand it, in Scala, a function may be called either
by-value or
by-name
For example, given the following declarations, do we know how the function will be called?
Declaration:
def f (x:...
257
votes
6
answers
314k
views
Appending an element to the end of a list in Scala
I can't add an element of type T into a list List[T].
I tried with myList ::= myElement but it seems it creates a strange object and accessing to myList.last always returns the first element that was ...
250
votes
14
answers
130k
views
Case objects vs Enumerations in Scala
Are there any best-practice guidelines on when to use case classes (or case objects) vs extending Enumeration in Scala?
They seem to offer some of the same benefits.
249
votes
7
answers
58k
views
Difference between case object and object
Is there any difference between case object and object in scala?
246
votes
9
answers
72k
views
What is the difference between "def" and "val" to define a function
What is the difference between:
def even: Int => Boolean = _ % 2 == 0
and
val even: Int => Boolean = _ % 2 == 0
Both can be called like even(10).
245
votes
4
answers
347k
views
Get item in the list in Scala?
How in the world do you get just an element at index i from the List in scala?
I tried get(i), and [i] - nothing works. Googling only returns how to "find" an element in the list. But I ...
244
votes
4
answers
66k
views
What does `:_*` (colon underscore star) do in Scala?
I have the following piece of code from this question:
def addChild(n: Node, newChild: Node) = n match {
case Elem(prefix, label, attribs, scope, child @ _*) => Elem(prefix, label, attribs, ...
233
votes
5
answers
84k
views
How to clone a case class instance and change just one field in Scala?
Let's say I have a case class that represents personas, people on different social networks. Instances of that class are fully immutable, and are held in immutable collections, to be eventually ...
232
votes
4
answers
39k
views
Scala 2.8 breakOut
In Scala 2.8, there is an object in scala.collection.package.scala:
def breakOut[From, T, To](implicit b : CanBuildFrom[Nothing, T, To]) =
new CanBuildFrom[From, T, To] {
def apply(from: ...
226
votes
1
answer
35k
views
How to get started with Akka Streams? [closed]
The Akka Streams library already comes with quite a wealth of documentation. However, the main problem for me is that it provides too much material - I feel quite overwhelmed by the number of concepts ...
224
votes
4
answers
61k
views
Akka Kill vs. Stop vs. Poison Pill?
Newbie question of Akka - I'm reading over Akka Essentials, could someone please explain the difference between Akka Stop/Poison Pill vs. Kill ? The book offers just a small explaination "Kill is ...
220
votes
1
answer
64k
views
Spark performance for Scala vs Python
I prefer Python over Scala. But, as Spark is natively written in Scala, I was expecting my code to run faster in the Scala than the Python version for obvious reasons.
With that assumption, I thought ...