Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions data-science-notes/programming/languages/scala/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,60 @@
---

## Overview

---
Scala is statically typed modern programming language. Scala allows for _both_ object-oriented and functional programming (it is **multi-paradigm**). Scala was created with the purpose of taking a concise, elegant, and type-safe approach to common programming patterns. Scala is used is many different application settings, including [Spark](https://makeuseofdata.com/programming/frameworks/apache_spark/index.html) and for Web Development. Like many other languages, Scala interoperates with other languages, specifically [Java](https://makeuseofdata.com/programming/languages/java/index.html). {cite}`scala_into`

## Basic Syntax
### Variable Types
In Scala, there are two types of variables and each is created differently. Immutable variables (variables that cannot be changed) are created using `val` while mutable variables (variables whose value will change) are created using using `var`. In Scala, you should use immutable variables unless your variable's value will change. An example of each type of variable is shown below:
```scala
val msg1 = "Hello, World!" // immutable variable
var msg2 = "Hello, World!" // mutable variable
```
If you try to reassign (change the value of) an immutable Scala variable, you will face a compiler error:
```scala
msg1 = "Aloha, World" // Will _not_ work!
```
However, mutable Scala variables can be changed:
```scala
msg2 = "Aloha, World" // Will work!
```
### Variable Type Declaration
In Scala, you can be explict and state a variable's type or be implict and let the complier infer the variable type:
```scala
val x: Int = 2 // Explict
val x = 2 // Implict
```
Scala suggests preferring implict assignment, which Scala says is less verbose. If you use the implict approach, Scala considers the `Int` and `Double` the default numeric types:
```scala
val x = 2 // An `Int`
val x = 2.345 // A `Double`
```
You may use `L`, `D`, and `F` (and their lowercase verisons) to explictly specify the type of values in Scala:
```scala
val i = 2_000L // val i: Long = 2000
val j = 3.14D // val j: Double = 3.14
val k = 2.71F // val k: Float = 2.71
```
For really large numbers, use `BigInt` and `BigDecimal`:
```scala
val x = BigInt(2718281828)
val y = BigDecimal(314159265.35)
```
Use `BigDecimal` for precise arithmetic, as `Double` and `Float` are approximations.

In addition, Scala offers `String` and `Char` data types:
```scala
val msg = "Hello, World!" // A `String`
val m = 'H' // A `Char`
```
For multiline string, use three double-qoutes {cite}`scala_vars`:
```scala
val book_title = """
Programming in Scala:
A Comprehensive Step-by-Step Guide
"""
```
---

## Sources
Expand All @@ -19,4 +68,4 @@

---

Contributions made by our wonderful GitHub Contributors:
Contributions made by our wonderful GitHub Contributors: [@madisonestabrook](https://github.com/madisonestabrook)
2 changes: 2 additions & 0 deletions data-science-notes/programming/languages/scala/references.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@misc{scala_into, title={Introduction - A Tour of Scala}, url={https://docs.scala-lang.org/tour/tour-of-scala.html}, journal={Scala Documentation}, author={Scala Community}}
@misc{scala_vars, title={Variables and Data Types}, url={https://docs.scala-lang.org/scala3/book/taste-vars-data-types.html}, journal={Scala Documentation}, author={Scala Community}}