This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val words = List("a", "b", "c", "d", "a", "c", "b", "a", "b", "d") | |
val res0 = words.map(w => (w, 1)).foldRight(("", 1)) { | |
case ((x1, y1), (x2, y2)) => (x1 + x2, y1 + y2) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public ObjectType getObjectType(Object object) { | |
if (object instanceof Map) return MAP; | |
else if (object instanceof Collection) return COLLECTION; | |
else if (object instanceof Integer) return INT; | |
else if (object instanceof Byte) return BYTE; | |
else if (object instanceof Boolean) return BOOL; | |
else if (object instanceof int[]) return INT_ARRAY; | |
else if (object instanceof char[]) return CHAR_ARRAY; | |
else if (object instanceof byte[]) return BYTE_ARRAY; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int[] ints = {1, 2, 3, 4, 5, 6}; | |
Object[] objects = (Object[]) ints; // error! cannot cast int[] to Object[] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class List[+T] { | |
def flatMap[U](f: T => List[U]): List[U] = this match { | |
case x :: xs => f(x) ++ xs.flatMap(f) // ++ is a concat operator | |
case Nil => Nil | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val opt: Option[Int] = Some(1) | |
val r3 = opt flatMap { | |
case (x: Int) => Some(x+1) | |
case _ => None | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Test { | |
def main(args: Array[String]) { | |
val result = `iterate'`(3, `^2`, 5) | |
println(s"$result") | |
} | |
def `iterate'`(n: Int, f: Int => Int, x: Int): Int = n match { | |
case 0 => x | |
case _ => `iterate'`(n - 1, f, f(x)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def `while`(condition: => Boolean)(command: => Unit): Unit = if (condition) { | |
command | |
`while`(condition)(command) | |
} | |
var i = 10 | |
`while`{ i = i - 1; i > 0 } { println(i) } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var i = 10 | |
class Condition(closure: (=> Unit) => Condition)(cmd: => Unit) { | |
def until(condition: => Boolean) = { | |
if (condition) { | |
cmd | |
closure(cmd) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val f: PartialFunction[Int, Boolean] = { | |
case 1 => true | |
case 2 => true | |
case 3 => true | |
} | |
println { | |
f.applyOrElse(7, (_: Int) => false) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val f: PartialFunction[Int, Boolean] = { | |
case 1 => true | |
case 2 => true | |
case 3 => true | |
} | |
val g: PartialFunction[Boolean, Int] = { | |
case true => 1 | |
case false => 2 | |
} |