Skip to content

Instantly share code, notes, and snippets.

@wmfairuz
Created May 8, 2014 03:25
Show Gist options
  • Save wmfairuz/0980f37f25e21e133982 to your computer and use it in GitHub Desktop.
Save wmfairuz/0980f37f25e21e133982 to your computer and use it in GitHub Desktop.
scala-cheatsheet
val a:Int = 0 // immutable
var b:Int = 0 // mutable
def foo(a: Int): Int = ??? // function definition
// anonymous function. (x: Int) is parameter. x * x is body
(x: Int) => x * x
// above can be rewriten with (with a name f)
def f(x:Int): Int = x * x
// function that accept another function in argument
def f(g: Int => Int, a: Int): Int = f(a) + a
// function that returns another function
def f(a: Int): Int => Int = {
def g(b: Int): Int = { ??? }
g
}
// above can be rewriten with...
def f(a: Int)(b: Int): Int = { ??? }
// Type of f ?
def f(a: Int => Int)(b: Int, c: Int): Int = ...
(Int => Int) => (Int, Int) => Int
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment