Scala

From bibbleWiki
Revision as of 11:57, 11 January 2021 by Iwiseman (talk | contribs) (Functions)
Jump to navigation Jump to search

Introduction

Some features of the language

  • Functional and Object Orientated
  • Use of immutable Data Structure
  • Rich Collections Library
  • Better Concurrency Support

To get this to work I downloaded scala and intellij.

Functional Programming

Some definitions

  • Immutability is when we can not allowed to change a variable. Not changing a value means parallelism will work better
  • Expression are something yields a value. e.g. x+y and have not side effects
  • Statements is code which does something. e.g. do(something) and have side effects
  • Function is a a relation between a set of inputs and a set of outputs with the property that each input is related to exactly one output.
    • Pure always has the same output, easier to test, parallelisation
    • Impure may not have the same output

High Order Functions

High Order Functions are function which take functions as an argument High Order functions allow you to provide some of the method as an argument to extract the part which is different
So given the following to get apples

def getApples(basket: List[Fruilt]) = getFruits(fruitBasket,
  (fruit: Fruit) => fruit.name = "apple")


Can be replaced with a more generic

def getFruits(basket: List[Fruilt], filterByFruit: Fruit => Boolean) = 
    for (fruit <- basket if filterByFruit(fruit)) yield fruit


Scala provide many other HOFs not KnightRider such as

  • map
  • reduce
  • filter
  • fold

Language Features

val and var

Like Kotlin, scala supports val and var where only vars can be changed

Conditions

If conditions are expressions and not statements. We generally do not need the return statement. I think this is like Ruby from memory e.g.

val arguments = Array("Monday")
val day = if (!arguments.isEmpty) arguments(0) else "Sunday"

Loops

Imperative

Where we do something val letters = List("a","b","c","d","e")

for(letter <- letters) {
   println(letter)
}

We can add filters on the end e.g. to print even numbers

for(letter <- letters if (number %2) == 0) {
   println(letter)
}

And not just one, many, which I think is looking really really ugly

for(
 letter <- letters 
 if (number %2) == 0) 
 if (number > 2) 
{
   println(letter)
}

We can iterate within an iteration though using the same approach e.g. we could have

for(number <- numbers) {
  for(letter <- letters) {
     println(number + " => " + letter)
  }
}

But with the new way this could be written with curly braces

for{ 
  number <- numbers 
  letter <- letters 
 } println(number + " => " + letter)

Functional

Where we return values. Using the example above we could yield a List[String] of values.

for{ 
  number <- numbers 
  letter <- letters 
 } yield number + " => " + letter

Functions

Introduction

VS Code

I used https://shunsvineyard.info/2020/11/20/setting-up-vs-code-for-scala-development-on-wsl/ for installing with VS Code.