Study Scala with Coursera – Part 2

Tram Ho

Continuing the series, today we will cover the rest of Week 1, including one part of GitHub CheatSheet, a few tutorials and the entire chapter 2 (If possible: V). OK, let’s start …

Getting Started

3. Cheat Sheet


The definition of Class in scala is not too different from object-oriented programming languages ​​(Because Scala carries both FP and OOP blood lines: V). In Scala there is a new statement:

The above command requires that when initializing an Object from Class, the variable y must be greater than 0. We will learn more when going into this lesson in the following chapters.


Notice this first line: myObject myMethod 1 is the same as calling myObject.myMethod (1). Recall the homework program for calculating a fraction, instead of writing (fraction2), we can write fraction1 plus fraction2. And more specifically, Scala allows naming with special characters, so we absolutely have to rename the function plus to + and then, fraction1 plus fraction2 will become fraction1 + fraction2. Looks a lot better, doesn’t it. However, teachers, coders and myself do not encourage you to abuse this too much, it will make your code extremely difficult to understand, and very difficult and time-consuming when maintaining code.

Class hierarchies

Temporarily is class hierarchy, one thing to note is that Object – this is a Singleton, that is, you only have one object that inherits from abstract class exists at a time. Take a small example, you have 1 class is Animals, you create 1 Object is a Dog inheriting from the Animals class, then throughout the program you can only have 1 Dog, manipulate and work (Feeding, bathing, doing meat, …) on 1 Dog, no 2nd Dog. Back in HelloWorld, we have another way to write a program, try:

Class Organization

Class organization: Class and Object can be inserted into a Package. And we can manipulate the class, object or methods inside it through something called Package Name – the package name contains your class and object. For example, I have Class Dog, there are 2 methods are eat () and sleep (), and Class Bird, there are 2 methods are fly () and eat () – both in one package are Animal. Now I want to call the sleep () method of the Dog class via the package name that contains it, I can write the following:

And there is an interesting thing in Scala called Trait, which helps us perform the multiple inheritance function in OOP, I will discuss in later chapters. Anyway, this is a cheat sheet summary, but trait requires more lines and words.

Type Parameters

Like templates in C ++ and generics in Java, can be applied to functions, trait and class. Help us reuse code with different data types without rewriting the code. A small example for those of you who do not know the two concepts above from C ++ or Java: Instead of writing 2 addition functions, 1 to add 2 real numbers (float), 1 to add 2 integers (int), we can write 1 plus function for both, then call this function and pass it the data type with 2 parameters that are 2 numbers to calculate.

Variance – Skip

Pattern Matching

Quite similar to Switch – case in other programming languages, but it can be used a lot more, let’s see an example with 1 List

The structure of List in Scala is quite special, look at the following image to make it easier to visualize

The left part, called the head, and the right part, called the tail. The character you write var x = List (1,2) is the same when you write var x = 1 :: 2. Explanation: x :: Nil means that the right part is an x ​​element, the left part has nothing, representing a List with only 1 element being x. Next, x :: xs represents a List with at least 1 element (usually> 2 elements) … similar to the other cases. We will dive deeper into List in the following chapters, and now let’s return to pattern matching. The above function has the effect of checking a List and dealing with each possible case with a List. When writing “case _” is similar to writing “case default” in C ++ or other languages, understand the remaining cases. The _ character is used quite a lot in Scala, when we don’t care about the other elements. For example, import package._ means to import the whole package. Note that pattern matching can be used with the Optional data type (a data type may or may not be available – for example, for a List of user information including name, age and gender. gender, so that some people will have information about gender, some will not. We call the gender variable here is Optional variable.When working, if not assigned the default value, or not to consider in case that variable is null, the program will crash – you will work a lot with the optional variable when programming mobile). Pattern matching is also used in Anonymous functions, which I will discuss in the following articles.


In Scala there are quite a few Collections, each collection has its own methods and from there also has its own usage in different cases. There are two main types of collections: Immutable and Mutable – cannot and can change values. Which collection type to use will greatly influence the performance of your program. Read this article to know more:


Pair – as the name implies. There is nothing to explain ?


Provide functions that make sorting easier. However, when studying on Coursera, you have to hand-write almost all of the arrangement functions and do a multitude of exercises before I introduce this class: V (This is a generics class in Scala’s math library – scala.math.Ordering [T]). Thanks to that, I gained a lot of knowledge about sorting, but it also took a lot of time: V.


Often the for loop in other programming languages ​​will be introduced in the first post, but Scala shoves it down to the end of the cheat sheet – similarly it is the last ones of week 6 on Coursera. Why? Because it’s incredibly @ # [email protected] &. Ordinary people often write a for loop like this:

Translate through C ++ then it will be:

But we don’t do so in Scala, but we rewrite like this:

Looking at the loop, I want to learn Scala ? So the end of the cheat sheet. Start the first lesson offline.

4. Scala Tutorial

Oops, it seems to be … yet. Let’s come to the general rules before getting into the first lessons. I will skip the 2 parts, IntelliJ Tutorials and submit exercises – because it’s not directed to the majority. The Learning Resource section, is also an English document, and I suggest you some of the following resources to learn about Scala: Scala Document, Coursera, edX, Udemy, Amazon Top Book (Search for Scala keywords and choose the top books to read) – similar to other programming languages ​​when you don’t know how to choose a source). Study each section one at a time, study one section at a time and learn to part another. Avoid enrolling a bundle, each see a little bit and then leave. Importantly, you learn and practice often. This section will give a very brief tutorial on the Scala programming language – which will probably be repeated with the introduction at his Cheat Sheet above. Translated from: .

Classes, Traits, Objects and Packages

The Scala class is very similar to the Java class (You may not know it, Scala and Java are brothers when they run on the JVM platform, so Scala’s performance is equivalent to Java). contains fields (like Name, Age, …) and methods (Like getName (), setAge (), …). Like Java, classes can be instantiated using the new constructor, which can have multiple instances or objects of the same type.

In Scala, there exists a special case of Class called case class. You will learn about case classes during this course. Classes in Scala do not have static members (members: members of the class, which can be fields or methods). You can use Objects (see below) to perform functions similar to static members in Java.

Traits: Similar to Interfaces in Java, but they can implement methods or define fields.

Objects: Objects in Scala are similar to Class, but each object is defined with only one instance. Cannot create an instance of an object with the keyword new, instead you can only access the members of an object by its name.

Packages: Adding a command like package at the beginning of the file makes the code in a file part of the package (This section is quite cumbersome, you can understand when putting the package at the beginning of the file 1 code file, all the code behind will be in that package – the code can be an object, class or function, … – that is, you are putting them into a package that can be And you can Put package in many files when you want to add more classes / object / functions to that package.). You can then “import” to use everything from the package into your code. The content of a package can be scattered across many files. If you define a class as MyClass in the package, you can import that class specifically (And don’t import anything else from the package) with the import command

In Scala, everything can be imported, not just a class name. Therefore, if there is an instance in object baz in package, then the command import will import all members of that object ….

End of part 2. See you in part 3 of the rest of the translation.

Share the news now

Source : Viblo