Skip to content

Instantly share code, notes, and snippets.

View manjuraj's full-sized avatar

Manju Rajashekhar manjuraj

View GitHub Profile
@manjuraj
manjuraj / infix.scala
Last active September 3, 2018 21:41
infix syntax for generic type
//
// References
// - http://jim-mcbeath.blogspot.com/2008/11/scala-type-infix-operators.html
//
//
// Notes:
// - a generic type op[T1, T2] can be written T1 op T2
//
@manjuraj
manjuraj / gist:b56e3377d562fdf0d283
Last active September 15, 2017 19:43
generalized type constraints: =:=, <:< and <%<
//
// References
// - http://stackoverflow.com/questions/3427345/what-do-and-mean-in-scala-2-8-and-where-are-they-documented
// - http://stackoverflow.com/questions/2603003/operator-in-scala
// - https://gist.github.com/retronym/229163
// - http://debasishg.blogspot.com/2010/08/using-generalized-type-constraints-how.html#sthash.GKfUGq9p.dpuf
// - http://www.scala-lang.org/old/node/4041.html
// - https://groups.google.com/forum/#!searchin/scala-user/generic$20type$20constraint/scala-user/mgx9-TUapQo/tiN6x818dKsJ
//
@manjuraj
manjuraj / ordering
Created February 12, 2015 21:33
ordering
// References
// Ordering case classes: http://meta.plasm.us/posts/2013/10/13/ordering-case-classes/
// Tuple Ordering available up to 9 arity - https://github.com/scala/scala/blob/v2.10.2/src/library/scala/math/Ordering.scala#L358
//
// Semigroup with a associative binary method `append`, subject
// to semigroup laws
//
// A Semigroup in type F must satisfy two laws:
// - closure: ∀ a, b in F, append(a, b) is also in F. This is
// enforced by the type system
// - associativity: ∀ a, b, c in F, the following equation holds
// => append(append(a, b), c) = append(a, append(b , c))
//
@manjuraj
manjuraj / typesafe-builder.scala
Last active October 24, 2024 17:55
typesafe builders in scala
//
// References:
// - http://www.tikalk.com/java/type-safe-builder-scala-using-type-constraints/
// - http://www.blumenfeld-maso.com/2011/05/statically-controlling-calls-to-methods-in-scala/
// - http://dcsobral.blogspot.com/2009/09/type-safe-builder-pattern.html
// - http://blog.rafaelferreira.net/2008/07/type-safe-builder-pattern-in-scala.html
// - http://jim-mcbeath.blogspot.com/2009/09/type-safe-builder-in-scala-part-4.html
// - http://villane.wordpress.com/2010/03/05/taking-advantage-of-scala-2-8-replacing-the-builder/
// - http://debasishg.blogspot.com/2010/08/using-generalized-type-constraints-how.html#sthash.GKfUGq9p.dpuf
//
@manjuraj
manjuraj / typeclass.scala
Last active August 29, 2015 14:06
Typeclass example
//
// Show typeclass parameterized on a concrete type
//
trait Show[A] {
def show(a: A): String
}
object Show {
implicit object StringShow extends Show[String] {
def show(s: String) = s"*$s*"
@manjuraj
manjuraj / gist:596f38e337d64e40d247
Last active August 29, 2015 14:05
Typeclasses in the wild
// https://github.com/twitter/util/blob/master/util-app/src/main/scala/com/twitter/app/Flag.scala
// Type class / Typeclass / Type traits
// References
// - http://www.haskell.org/haskellwiki/Typeclassopedia
// - http://www.haskell.org/wikiupload/8/85/TMR-Issue13.pdf
// - https://github.com/channingwalton/typeclassopedia
// - http://ropas.snu.ac.kr/~bruno/papers/TypeClasses.pdf
// - http://debasishg.blogspot.com/2010/06/scala-implicits-type-classes-here-i.html
@manjuraj
manjuraj / gist:cd043b80d07eab089dda
Created August 21, 2014 00:52
Scala Option fold
// http://stackoverflow.com/questions/5328007/why-doesnt-option-have-a-fold-method
// See scalaz option cata()
//
// fold[B](ifEmpty: => B)(f: A => B): B
http://www.blumenfeld-maso.com/2010/01/scala-word-of-the-day-catamorphism/
import com.fasterxml.jackson.core.{JsonGenerator, JsonToken, JsonParser, JsonFactory}
import com.fasterxml.jackson.databind._
val mapper = new ObjectMapper
val jsonFactory = new JsonFactory(mapper)
val content =
"""
{
"name" : "Watership Down",
"location" : {
@manjuraj
manjuraj / gist:cc2a180ee9637ecc95b1
Created May 31, 2014 01:17
renaming java collection imports
import java.util.{Map ⇒ JMap}
import java.util.{List ⇒ JList}