Skip to content

Instantly share code, notes, and snippets.

@jaturken
jaturken / gist:3976117
Created October 29, 2012 19:52
Scala features cheatsheet

Cheat Sheet

This cheat sheet originated from the forum, credits to Laurent Poulain. We copied it and changed or added a few things.

Evaluation Rules

  • Call by value: evaluates the function arguments before calling the function
  • Call by name: evaluates the function first, and then evaluates the arguments if need be
@jaturken
jaturken / gist:3953315
Created October 25, 2012 15:23
Tail recursion benchmarking
def non_tail_recursive_factorial(n)
if n < 2
1
else
n * non_tail_recursive_factorial(n-1)
end
end
def tail_recursive_factorial(n, r = 1)
if n < 2
@jaturken
jaturken / gist:3796439
Created September 27, 2012 21:00
Max in Scala
def max(xs: List[Int]): Int = {
def maxOfTwo(a:Int, b:Int) = if(a>b) a else b
def maxOfHeadAndTail(i: Int, l: List[Int]): Int = if (l.isEmpty) i else maxOfHeadAndTail(maxOfTwo(i, l.head), l.tail)
maxOfHeadAndTail(xs.head, xs.tail)
}