The goal of this project is to be a concise guide for those transitioning from Python to Scala. This is by no means meant to be a complete guide to Scala, but rather some (hopefully) helpful snippets of code to help you translate between the two languages.
This guide loosely follows along with the text of Scala for the Impatient, a great introductory book for those learning Scala. You might find it helpful to read it alongside the chapters from this repo.
Note that in general, you should not try to directly translate idioms from one language to another; you don't want to write Scala that looks like Python- you want to write Scala that looks like Scala! You should strive to write idiomatic code whenever possible. A good starting point is Twitter's Effective Scala
I recommend reading through the guide in the following order:
1.Variables and Arithmetic
2.Conditionals
3.Functions
4.Strings
5.Arrays
6.Maps
7.Tuples
8.Exceptions
9.Classes
Here are some Scala topics not discussed above that I think are important to review:
•Pattern Matching This is like a switch statement on turbo, and is very powerful and oft used. The Scala Cookbook has really great practical examples.
•Auxiliary Constructors Classes can have multiple constructors that operate on different argument types/number of args.
•Case Classes as an immutable, record-like data-structure that can be pattern-matched.
•Scala Collections There is a lot of power in all of the methods available to data structures like Vector, Array, List, Sequence, Set, etc. Just take a look at all of the available methods.
All of the code for this book can be found on Github.
This is going to be a very quick review, as there shouldn't be much surprising with the way Scala handles values (immutable) and variables (mutable):
Python:
>>> foo = "Apples"
>>> baz = foo + " and Oranges."
>>> baz
'Apples and Oranges.'
>>> baz = "Only Grapes."
Scala:
scala> val foo = "Apples"
foo: String = Apples
scala> val baz = foo + " and Oranges."
baz: String = Apples and Oranges.
scala> baz
res60: String = Apples and Oranges.
// In Scala, vals are immutable
scala> baz = "Only Grapes."
<console>:13: error: reassignment to val
baz = "Only Grapes."
// Create a var instead
scala> var baz = "Apples and Oranges."
baz: String = Apples and Oranges.
scala> baz = "Only Grapes."
baz: String = Only Grapes.
scala> var one = 1
one: Int = 1
scala> one += 1
scala> one
res21: Int = 2
Scala will also allow you to more strongly type your variables, rather than letting the compiler interpret the type:
Scala
scala> val foo: String = "Apples"
foo: String = Apples
Python and Scala will both let you perform multiple assignment. However, be careful with Python and pass by reference! You'll usually want to unpack rather than perform multiple assignment.
Scala:
scala> val foo, bar = Array(1, 2, 3)
foo: Array[Int] = Array(1, 2, 3)
bar: Array[Int] = Array(1, 2, 3)
// foo and bar reference different pieces of memory; changing one will not change the other.
scala> bar(0) = 4
scala> bar
res70: Array[Int] = Array(4, 2, 3)
scala> foo
res71: Array[Int] = Array(1, 2, 3)
The same can be achieved with Python unpacking:
>>> foo, bar = [1, 2, 3], [1, 2, 3]
# Are they referencing the same memory?
>>> foo is bar
False
# What happens when you change bar?
>>> bar[0] = 4
>>> bar
[4, 2, 3]
>>> foo
[1, 2, 3]
# You *can* assign both foo and bar the same value, but they reference the same memory!
>>> foo = bar = [1, 2, 3]
>>> foo is bar
True
>>> bar[0] = 4
>>> bar
[4, 2, 3]
>>> foo
[4, 2, 3]
Scala and Python largely share arithmetic operations. Behind the scenes, they are both using methods to implement the operaters- Scala uses the actual operator symbol, rather than an alphanumeric character:
For-loops and comprehensions, the latter of which is likely sorely missed by Python programmers writing Java. Scala supports a for (variable <- expression) syntax. Let's look at Python first:
Python
foo = "Apple"
n = 0
for a in foo:
n += 1
>>> n
5
Scala
scala> val foo = "Apple"
scala> var n = 0
scala>
for (x <- foo) {
n += 1
}
scala> n
res140: n: Int = 5
// This would actually be better expressed in a single line
scala> n = 0
scala> for (x <- foo) n += 1
scala> n
res141: n: Int = 5
Python comprehensions are a very important part of writing idiomatic Python; Scala supports the same with the yield syntax:
Python
>>> [f + 1 for f in [1, 2, 3, 4, 5]]
[2, 3, 4, 5, 6]
Scala
scala> for (f <- Array(1, 2, 3, 4, 5)) yield f + 1
res59: Array[Int] = Array(2, 3, 4, 5, 6)
Python has a very useful function called zip that will allow you to iterate over iterables at the same time. Scala will allow you to have multiple "generators" in an expression, which can replicate the zip behavior:
Python
foo, bar = [1, 2, 3], ['a', 'b', 'c']
foobars = {}
for f, b in zip(foo, bar):
foobars[b] = f
>>> foobars
{'a': 1, 'c': 3, 'b': 2}
# It's more Pythonic to use a comprehension
>>> {b: f for f, b in zip(foo, bar)}
{'a': 1, 'c': 3, 'b': 2}
Scala
val foo = Array(1, 2, 3)
val bar = Array("a", "b", "c")
import scala.collection.mutable.Map
// Let's go ahead and specify the types, since we know them
var foobars= Map[String, Int]()
for (f <- foo; b <- bar) foobars += (b -> f)
scala> foobars
res5: scala.collection.mutable.Map[String,Int] = Map(b -> 3, a -> 3, c -> 3)
// This is really powerful- we're not limited to two iterables
val baz = Array("apple", "orange", "banana")
val mapped = Map[String, (Int, String)]()
for (f <- foo; b <- bar; z <- baz) mapped += (z -> (f, b))