Skip to content

Instantly share code, notes, and snippets.

View manjuraj's full-sized avatar

Manju Rajashekhar manjuraj

View GitHub Profile
@manjuraj
manjuraj / gist:8c767ac4d6814be2813e
Last active April 16, 2019 14:07
collect vs map, filter vs flatMap
// 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
@manjuraj
manjuraj / gist:6d66a94e3d71fbd0e60e
Last active February 7, 2017 18:38
Boolean unapply extractor
// 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.
@manjuraj
manjuraj / gist:11026922
Last active August 29, 2015 14:00
Try - Either
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)
@manjuraj
manjuraj / gist:9496590
Last active August 29, 2015 13:57
implicit views
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.
@manjuraj
manjuraj / gist:9492759
Last active August 29, 2015 13:57
Clever use of typeclasses to enrich existing type while preserving existing type in type system
// 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
@manjuraj
manjuraj / gist:9296160
Last active February 19, 2019 03:49
Loan pattern
// 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._
@manjuraj
manjuraj / gist:8859551
Created February 7, 2014 09:20
Print system properties
import scala.collection.JavaConverters._
val p = System.getProperties
p.keySet.asScala.foreach { k => println(s"$k : ${p.get(k)}") }
@manjuraj
manjuraj / gist:8549678
Last active January 4, 2016 01:39
Enumerations
// Enumeration
object Color extends Enumeration {
type Color = Value
val Red = Value("red")
val Blue = Value("blue")
val Green = Value("green")
}
import Color._
@manjuraj
manjuraj / gist:8549617
Created January 21, 2014 22:18
format strings
// 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)`
@manjuraj
manjuraj / gist:8548582
Created January 21, 2014 21:18
TryHarder - Try with finally
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'