Skip to content

Instantly share code, notes, and snippets.

@JonNorman
Created October 15, 2018 14:03
Show Gist options
  • Save JonNorman/5ac83d270c057eda7b5b8911abd170d1 to your computer and use it in GitHub Desktop.
Save JonNorman/5ac83d270c057eda7b5b8911abd170d1 to your computer and use it in GitHub Desktop.
A quick overview of the concepts covered last week
/******************************************
* Declaring variables and using types
*******************************************/
/*
We can declare variables with val - once the value has been set, it cannot be changed
*/
val myName = "Jon"
myName = "Jonathan" // not valid!
/*
We can declare mutable variables with var, but we generally avoid doing this as it makes
reasoning about complicated systems and code more difficult
*/
var myAge = 26
myAge = 27
/*
When we create variables, the compiler understands the TYPE of the variable.
Above, it understands that name is a String and that age is an Int, but we
specify explicitly like so - and we MUST specify when defining functions:
*/
val myHomeTown: String = "Nottingham"
/*
If we try to perform an operation on two types for which that operation isn't supported
*/
val passedDrivingTestAtAge: String = "18"
val yearsDriving = myAge - passedDrivingTestAtAge
/*
There are lots of types provided by the scala Standard Library in the scala package, which
is automatically in scope whenever you write a scala program.
Take a look here: https://www.scala-lang.org/api/current/scala/index.html
HINT: the scala package provides lots of convenient constructs and not all of these
are technically "types", some are traits and classes - we will cover these at some point.
*/
val isAnAdult: Boolean = true
val isAPensioner: Boolean = false
/******************************************
* Declaring functions
*******************************************/
/*
We can define functions with the def keyword, followed by the name of the function,
the parameters it takes in parentheses and then optionally the return type, though
the compiler can normally work it out as it is whatever the type of the last expression
in the code block is.
*/
def generateGreeting(name: String, age: Int, homeTown: String) = {
"Hi! My name is " + name + ", I'm " + age + " and I come from " + homeTown
}
/*
We can call functions by writing the name of the function, opening the parentheses,
supplying the arguments (if any), and closing the parentheses.
*/
generateGreeting(myName, myAge, myHomeTown)
generateGreeting("Lucy", 22, "Edinburgh")
/*
If we specify an expected type that the compiler doesn't agree with then we will get
* a compiler error */
generateGreeting("Lucy", "22", false)
/*
For functions that have typical default values, we can supply these as part of the
function definition.
*/
def generateSeasonalGreeting(name: String, holiday: String = "Christmas"): String = {
"Hi " + name + "! Have a superb " + holiday
}
generateSeasonalGreeting("Jon")
generateSeasonalGreeting("Jon", "Diwali")
/******************************************
* Generic Types: Collections
*******************************************/
/*
There are other types - i.e. non-primitives - that can be used to express operations on
a number of variables of the same type e.g List
*/
/*
Lets say we have the names of three people that we want to greet with our function above.
With what we know so far we can do this like so:
*/
generateSeasonalGreeting("Jon")
generateSeasonalGreeting("Lucy")
generateSeasonalGreeting("Ben")
generateSeasonalGreeting("Abi")
generateSeasonalGreeting("Becky")
generateSeasonalGreeting("Jack")
/*
We could use a List to model this better and write less code
*/
val names = List("Jon", "Lucy", "Ben", "Abi", "Becky", "Jack")
val greetings1 = names.map(name => generateSeasonalGreeting(name))
// or with shorthand notation
val greetings2 = names.map(generateSeasonalGreeting(_))
/*
The List type also has a bunch of useful operations on it
*/
names.head
names.tail
names.filter(name => name.startsWith("B"))
names.filter(name => name.startsWith("B"))
names.max
names.min
names.map(name => name.toLowerCase)
names.map(name => name.length)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment