## Terms And Types

### Scala Tutorial

The following set of sections provides a quick tutorial on the Scala language.

The contents is based on the MOOCS Functional Programming Principles in Scala and Functional Program Design in Scala.

The target audience is people who already have some experience of programming and who are familiar with the JVM.

### Elements of Programming

Programming languages give programmers ways to express computations.

Every non-trivial programming language provides:

• primitive expressions representing the simplest elements ;
• ways to combine expressions ;
• ways to abstract expressions, which introduce a name for an expression by which it can then be referred to.

### Primitive Expressions

Here are some examples of primitive expressions:

• The number “1”:
``1``
• The boolean value “true”:
``true``
• The text “Hello, Scala!”:
``"Hello, Scala!"``

(Note the usage of double quotes, `"`).

### Compound Expressions

More complex expressions can be expressed by combining simpler expressions using operators. They can therefore express more complex computations:

• How many is one plus two?
``1 + 2``
• What is the result of the concatenation of the texts “Hello, ” and “Scala!”?
``"Hello, " ++ "Scala!"``

### Evaluation

A non-primitive expression is evaluated as follows.

1. Take the leftmost operator
2. Evaluate its operands (left before right)
3. Apply the operator to the operands

The evaluation process stops once it results in a value.

#### Example

Here is the evaluation of an arithmetic expression:

``````(1 + 2) * 3
3 * 3
9``````
``````1 + 2 shouldBe res0
"Hello, " ++ "Scala!" shouldBe res1``````

### Method Calls

Another way to make complex expressions out of simpler expressions is to call methods on expressions:

• What is the size of the text “Hello, Scala!”?
``"Hello, Scala!".size``

Methods are applied on expressions using the dot notation.

The object on which the method is applied is named the target object.

• What is the range of numbers between 1 and 10?
``1.to(10)``

Methods can have parameters. They are supplied between parentheses.

In the below examples, the `abs` method returns the absolute value of a number, and the `toUpperCase` method returns the target `String` in upper case.

``````"Hello, Scala!".toUpperCase shouldBe res0
-42.abs shouldBe res1``````

### Operators Are Methods

Actually, operators are just methods with symbolic names:

``3 + 2 == 3.+(2)``

The infix syntax allows you to omit the dot and the parentheses.

The infix syntax can also be used with regular methods:

``1.to(10) == (1 to 10)``

Any method with a parameter can be used like an infix operator.

### Values and Types

Expressions have a value and a type. The evaluation model defines how to get a value out of an expression. Types classify values.

Both `0` and `1` are numbers, their type is `Int`.

`"foo"` and `"bar"` are text, their type is `String`.

### Static Typing

The Scala compiler statically checks that you don’t combine incompatible expressions.

Fill the following blank with values whose type is different from `Int` and see the result:

``1 to res0``

### Common Types

• `Int`: 32-bit integers (e.g. `1`, `23`, `456`)
• `Double`: 64-bit floating point numbers (e.g. `1.0`, `2.3`, `4.56`)
• `Boolean`: boolean values (`true` and `false`)
• `String`: text (e.g. `"foo"`, `"bar"`)

Note that type names always begin with an upper case letter.

### Exercise

Here are some more methods of standard types. Can you guess what they do? If you get stuck, try evaluating each statement in turn in a scala REPL to see what the result is.

``````16.toHexString shouldBe res0
(0 to 10).contains(10) shouldBe true
(0 until 10).contains(10) shouldBe res1
"foo".drop(1) shouldBe "oo"
"bar".take(2) shouldBe res2``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.7762476,"math_prob":0.9096862,"size":2757,"snap":"2021-43-2021-49","text_gpt3_token_len":665,"char_repetition_ratio":0.12895024,"word_repetition_ratio":0.0,"special_character_ratio":0.2495466,"punctuation_ratio":0.14548802,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9744034,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-11-29T00:35:16Z\",\"WARC-Record-ID\":\"<urn:uuid:bf6be3bc-4210-4b3b-91a1-fdd241e60406>\",\"Content-Length\":\"37262\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:8a1d39bd-f765-4ea5-88e0-df2d380e63f9>\",\"WARC-Concurrent-To\":\"<urn:uuid:4618b2e2-9ec6-405e-82c3-e95bfca5ffe2>\",\"WARC-IP-Address\":\"54.91.59.199\",\"WARC-Target-URI\":\"https://www.scala-exercises.org/scala_tutorial/terms_and_types\",\"WARC-Payload-Digest\":\"sha1:IVOWQ3JX365AC4XCT7SJ2QC2SBB2QJP2\",\"WARC-Block-Digest\":\"sha1:6VIVIGFSF5QE6OA2WBGMH7EDFHUI5EPA\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964358673.74_warc_CC-MAIN-20211128224316-20211129014316-00109.warc.gz\"}"}