This file contains 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
// The collect method takes a PartialFunction as argument and maps the | |
// values defined for this partial function over it, skipping those | |
// outside the definition domain. | |
// Given a partial function pf, the following code: | |
// | |
// val pf: PartialFunction[A, B] = | |
// coll.collect(pf) | |
// | |
// is roughly equivalent to |
This file contains 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
// A boolean extractor is an object that returns Boolean from | |
// the unapply method rather than Option[_]. | |
scala> :paste | |
object HasVowels { | |
def unapply(in: String): Boolean = in.exists("aeiou".toSet) | |
} | |
// Exiting paste mode, now interpreting. |
This file contains 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
import scala.util.{Try, Success, Failure} | |
implicit class EitherOps[L <: Throwable, R](val e: Either[L, R]) extends AnyVal { | |
def toTry: Try[R] = e.fold(Failure(_), Success(_)) | |
} | |
implicit class TryOps[T](val t: Try[T]) extends AnyVal { | |
def toEither: Either[Throwable, T] = t match { | |
case Failure(e) => Left(e) | |
case Success(v) => Right(v) |
This file contains 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
scala> :pa | |
// Entering paste mode (ctrl-D to finish) | |
trait Run { | |
def run(): Unit | |
} | |
def doRun(r: Run) = r.run() | |
// Exiting paste mode, now interpreting. |
This file contains 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
// Type information got lost because of implicit conversion - instead of type | |
// Array[T], we have the runtime type WrappedArray[T] | |
scala> def first[T](x: Traversable[T]) = (x.head, x) | |
first: [T](x: Traversable[T])(T, Traversable[T]) | |
scala> first(Array(1,2,3)) | |
res0: (Int, Traversable[Int]) = (1,WrappedArray(1, 2, 3)) | |
// Use implicit type constraints when we want to enrich an existing type while |
This file contains 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
// Purpose: | |
// - Ensures that a resource is deterministically disposed of once it goes out of scope | |
// - Use this pattern when working with resources that should be closed or managed after use | |
// | |
// The benefit of this pattern is that it frees the developer from the responsibility of | |
// explicitly managing resources | |
import scala.io.Source | |
import java.io._ | |
This file contains 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
import scala.collection.JavaConverters._ | |
val p = System.getProperties | |
p.keySet.asScala.foreach { k => println(s"$k : ${p.get(k)}") } |
This file contains 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
// Enumeration | |
object Color extends Enumeration { | |
type Color = Value | |
val Red = Value("red") | |
val Blue = Value("blue") | |
val Green = Value("green") | |
} | |
import Color._ |
This file contains 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
// Scala adds a "format" method to Strings (via StringOps), | |
// which takes Any* instead of Object* | |
"hi: %d".format(5) // same as `String.format("hi: %d", 5)` |
This file contains 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
import scala.util.Try | |
implicit class TryHarder[T](t:Try[T]) { | |
def harder(f: => Unit) = {f; t} | |
} | |
Try(1/0).harder(println("finally")) // prints 'finally' | |
Try(try { ??? } finally { 1 / 0 }).harder(println("finally finally")) // prints 'finally finally' |