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 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 and styles. Its key features are:

  • Static typing
  • Advanced type system with type inference and declaration-site variance
  • Function types (including anonymous) which support lexical closures
  • Pattern-matching
  • Implicit parameters and conversions which support the typeclass and enrich-my-library patterns
  • Mixin composition
  • Full interoperability with Java
  • Powerful concurrency constructs
  • Advanced language constructs such as delimited continuations and an experimental macro system

For more information, see the official Scala Introduction and Scala Documentation.

To search for Scala symbols such as => in Stack Overflow, you can use symbolhound search.

To search Scala documentation, you can use scalex.

A community list of available libraries is available at Awesome Scala

There is Scala Library Index by Scala Center: Scaladex.

Free Scala programming books and guides

Stack Overflow Scala Tutorial

  1. Introduction to Scala
  1. Variables/values
  1. Methods
  1. Literals, statements and blocks
  1. Loops/recursion
  1. Data structures / Collections
  1. For-comprehension
  1. Enumeration
  1. Pattern matching
  1. Classes, objects and types
  1. Packages, imports and visibility identifiers
  1. Inheritance
  1. Extractors
  1. Case classes
  1. Parameterized types
  1. Traits
  1. Self references
  1. Error handling
  1. Type handling
  1. Annotations
  1. Functions/Function literals
  1. Type safety
  1. Implicits
  1. Reflection
  1. Enrich-my-library pattern (formerly known as pimp-my-library)
  1. Concurrency overview
  2. Actors
  1. Use Java from Scala and vice versa
  1. XML literals
  • Explanation
  1. Scala Swing
  1. Type Programming
  1. Functional Scala

Further learning

  1. Learning Resources
  1. REPL
  1. Working with scalac and scala
  1. Operator precedence
  1. Scala style
  1. Functional Programming Principles in Scala, a functional programming course on Coursera taught by Martin Odersky, the creator of Scala.
  2. Principles of Reactive Programming, a reactive functional programming course on Coursera taught by Martin Odersky, Erik Meijer, Roland Kuhn.
  3. Parallel Programming, a parallel programming course on Coursera taught by Viktor Kuncak and Aleksandar Prokopec.
111781 questions
194
votes
10 answers

How to select the first row of each group?

I have a DataFrame generated as follow: df.groupBy($"Hour", $"Category") .agg(sum($"value") as "TotalValue") .sort($"Hour".asc, $"TotalValue".desc)) The results look…
Rami
  • 8,044
  • 18
  • 66
  • 108
192
votes
16 answers

How to define "type disjunction" (union types)?

One way that has been suggested to deal with double definitions of overloaded methods is to replace overloading with pattern matching: object Bar { def foo(xs: Any*) = xs foreach { case _:String => println("str") case _:Int =>…
Aaron Novstrup
  • 20,967
  • 7
  • 70
  • 108
191
votes
5 answers

(Why) do we need to call cache or persist on a RDD

When a resilient distributed dataset (RDD) is created from a text file or collection (or from another RDD), do we need to call "cache" or "persist" explicitly to store the RDD data into memory? Or is the RDD data stored in a distributed way in the…
user1261215
188
votes
16 answers

Best way to merge two maps and sum the values of same key?

val map1 = Map(1 -> 9 , 2 -> 20) val map2 = Map(1 -> 100, 3 -> 300) I want to merge them, and sum the values of same keys. So the result will be: Map(2->20, 1->109, 3->300) Now I have 2 solutions: val list = map1.toList ++ map2.toList val merged =…
Freewind
  • 193,756
  • 157
  • 432
  • 708
187
votes
2 answers

Scala equivalent of Java java.lang.Class Object

The question is best explained by an example: In Java for a JPA EntityManager, I can do the following(Account is my Entity class): Account result = manager.find(Account.class, primaryKey); In Scala, my naive attempt is: val result =…
Kekoa
  • 27,892
  • 14
  • 72
  • 91
185
votes
16 answers

How to turn off INFO logging in Spark?

I installed Spark using the AWS EC2 guide and I can launch the program fine using the bin/pyspark script to get to the spark prompt and can also do the Quick Start quide successfully. However, I cannot for the life of me figure out how to stop all…
horatio1701d
  • 8,809
  • 14
  • 48
  • 77
183
votes
2 answers

Build.scala, % and %% symbols meaning

I'm new to Play! Framework 2.1 (java version) and have no experience with scala. I don't understand what are and what does % and %% mean in Build.scala. I googled about them but couldn't find their meaning. In my Build.scala file I…
Franco
  • 11,845
  • 7
  • 27
  • 33
182
votes
1 answer

Getting a structural type with an anonymous class's methods from a macro

Suppose we want to write a macro that defines an anonymous class with some type members or methods, and then creates an instance of that class that's statically typed as a structural type with those methods, etc. This is possible with the macro…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
181
votes
23 answers

How can I change column types in Spark SQL's DataFrame?

Suppose I'm doing something like: val df = sqlContext.load("com.databricks.spark.csv", Map("path" -> "cars.csv", "header" -> "true")) df.printSchema() root |-- year: string (nullable = true) |-- make: string (nullable = true) |-- model: string…
kevinykuo
  • 4,600
  • 5
  • 23
  • 31
180
votes
14 answers

Logging in Scala

What is a good way to do logging in a Scala application? Something that is consistent with the language philosophy, does not clutter the code, and is low-maintenance and unobtrusive. Here's a basic requirement list: simple does not clutter the…
George
  • 3,433
  • 4
  • 27
  • 25
178
votes
13 answers

Scala best way of turning a Collection into a Map-by-key?

If I have a collection c of type T and there is a property p on T (of type P, say), what is the best way to do a map-by-extracting-key? val c: Collection[T] val m: Map[P, T] One way is the following: m = new HashMap[P, T] c foreach { t => m add…
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
178
votes
6 answers

How to sort by column in descending order in Spark SQL?

I tried df.orderBy("col1").show(10) but it sorted in ascending order. df.sort("col1").show(10) also sorts in ascending order. I looked on stackoverflow and the answers I found were all outdated or referred to RDDs. I'd like to use the native…
Vedom
  • 3,027
  • 3
  • 14
  • 16
176
votes
17 answers

Scala: write string to file in one statement

For reading files in Scala, there is Source.fromFile("file.txt").mkString Is there an equivalent and concise way to write a string to file? Most languages support something like that. My favorite is Groovy: def f = new File("file.txt") // Read def…
smartnut007
  • 6,324
  • 6
  • 45
  • 52
176
votes
16 answers

Is asynchronous jdbc call possible?

I wonder if there is a way to make asynchronous calls to a database? For instance, imagine that I've a big request that take a very long time to process, I want to send the request and receive a notification when the request will return a value (by…
Steve Gury
  • 15,158
  • 6
  • 38
  • 42
175
votes
4 answers

What is the Scala identifier "implicitly"?

I have seen a function named implicitly used in Scala examples. What is it, and how is it used? Example here: scala> sealed trait Foo[T] { def apply(list : List[T]) : Unit }; object Foo { | implicit def stringImpl = new…
oluies
  • 17,694
  • 14
  • 74
  • 117