Questions tagged [scala]
Scala is a general-purpose programming language principally targeting the Java Virtual Machine. Designed to express common programming patterns in a concise, elegant, and type-safe way, it fuses both imperative and functional programming styles. Its key features are: an advanced static type system with type inference; function types; pattern-matching; implicit parameters and conversions; operator overloading; full interoperability with Java; concurrency
scala
112,379
questions
872
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
243k
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.
623
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
171k
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 ...
494
votes
17
answers
149k
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] = ...
439
votes
2
answers
57k
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 ...
438
votes
11
answers
136k
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 ...
422
votes
11
answers
74k
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
108k
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.
401
votes
4
answers
210k
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
1
answer
80k
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 ...
381
votes
5
answers
198k
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?
376
votes
6
answers
115k
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 ...
371
votes
9
answers
63k
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 ...
365
votes
7
answers
157k
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) ...
362
votes
19
answers
275k
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&...
355
votes
4
answers
29k
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 ...
345
votes
11
answers
151k
views
What is Scala's yield?
I understand Ruby and Python's yield. What does Scala's yield do?
345
votes
7
answers
157k
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(
...
331
votes
13
answers
132k
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:
...
323
votes
17
answers
469k
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....
318
votes
1
answer
172k
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 ",").
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?
315
votes
19
answers
259k
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
...
306
votes
5
answers
80k
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 ...
288
votes
1
answer
42k
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 ...
280
votes
12
answers
84k
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) ...
272
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 ...
271
votes
7
answers
126k
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 ...
261
votes
26
answers
161k
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?
257
votes
17
answers
102k
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:...
256
votes
6
answers
306k
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 ...
256
votes
11
answers
250k
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....
250
votes
14
answers
129k
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.
247
votes
7
answers
58k
views
Difference between case object and object
Is there any difference between case object and object in scala?
245
votes
4
answers
341k
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
9
answers
71k
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).
243
votes
4
answers
65k
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
83k
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: ...
227
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 ...
223
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 ...
219
votes
8
answers
77k
views
difference between foldLeft and reduceLeft in Scala
I have learned the basic difference between foldLeft and reduceLeft
foldLeft:
initial value has to be passed
reduceLeft:
takes first element of the collection as initial value
throws exception if ...