Skip to content

Instantly share code, notes, and snippets.

@gto76
Last active November 10, 2015 16:57
Show Gist options
  • Save gto76/c8bc20a9e7d973c53402 to your computer and use it in GitHub Desktop.
Save gto76/c8bc20a9e7d973c53402 to your computer and use it in GitHub Desktop.
Scala examples

Scala Examples

Sum two inputs:

println(io.Source.stdin.getLines().take(2).map(_.toInt).sum)

Repeat each element of list num times:

def f(num:Int,arr:List[Int]):List[Int] = arr.map(x => List.fill(num)(x)).flatten

Obtain only elements less then delim:

def f(delim:Int,arr:List[Int]):List[Int] = arr.filter(_ < delim)

Drop elements with odd index:

def f(arr:List[Int]):List[Int] = arr.zipWithIndex.filter(_._2 % 2 == 1).map(e => e._1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment