Skip to content

Instantly share code, notes, and snippets.

@felipeeflores
Last active August 29, 2018 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipeeflores/d63eacbb7ede8d7ae16fe416595a3700 to your computer and use it in GitHub Desktop.
Save felipeeflores/d63eacbb7ede8d7ae16fe416595a3700 to your computer and use it in GitHub Desktop.
Presenter notes for the IntroExercises.scala
/*
# Presenter Notes
## Intro
- Start by explaining course structure
- levels
- tests for each level
- `build.sbt`
- start `sbt`
- run sbt commands
- it is better to leave sbt open to run commands interactively, instead of batch mode
- run all tests
- run one test
- hot reload
## IntroExercises
- The idea is to get familiar with the language ans its basic constructs
- How do we create functions/methods, declare parameters
- create values, use vals and not vars, objects
### Scala objects
- explain scala objects:
> An object is a class that has exactly one instance. An object is a value and a singleton.
- objects are very special and useful. You will see them everywhere in REA codebases
### Functions
- A function takes arguments (inputs) and returns a result (output)
- when a function has the property that always returns the same output for a given set of inputs, we say the function is referentially transparent
- briefly: explain what referential transparency is and maybe why it is a good thing. Mention that student will see more of that throughout the course
- In Scala all functions are expressions, which means the y always return something
- The last statement in its body is the output of a function
- there is a return statement, but you will never see it used.
### Exercises
*/
def add(x: Int, y: Int): Int = x + y
// [A] -> polymorphic type in Scala, also know as Generics in other languages
def foo[A](a: A): A = a
def bar(a: Int): Int = Random.nextInt
// More parametricity supporting examples
def other[A](items: List[A]): Int = ???
def other[A](items: List[A]) = items.size
def another[A](items: List[A]): Boolean = ???
def another[A](items: List[A]) = items.isEmpty
def timesTwoIfEven(x: Int): Int = if (x % 2 == 0) x * 2 else x
//Not total?
def timesTwoIfEven(x: Int): Int = if (x % 2 == 0) x * 2
// Error:(55, 37) type mismatch;
// found : Unit
// required: Int
// In Scala if statements are expressions
scala> val foo = if (5 > 3) "foo" else "bar"
foo: String = foo
def showNumber(x: Int): String = s"The number is $x"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment