Skip to content

Instantly share code, notes, and snippets.

import scala.collection.mutable
import scala.io.Source
import scala.util.Try
/*
Sample csv:
user_id,age,favorite_color
0,5,cyan
1,5,gray
2,22,green
//http://aperiodic.net/phil/scala/s-99/, p26
// In how many ways can a committee of 3 be chosen from a group of 12 people?
//We all know that there are C(12,3) = 220 possibilities (C(N,K) denotes the well-known binomial coefficient).
//For pure mathematicians, this result may be great. But we want to really generate all the possibilities.
import scala.annotation.tailrec
val interested = Seq('a','b', 'c', 'd','e', 'f', 'g','h', 'i')
@jiamingd
jiamingd / ContextImplicitShow
Last active July 24, 2017 05:36
Drill on how type class with implicit bring in polymorphism
trait Show[A] {
def doShow(input : A) : String
}
// Value class apporach need more tune up
// Value class: Goal is to avoid a runtime overhead
//implicit class LetShowable[A](val a: A) extends AnyVal{
// def letShow(implicit sh: Show[A]) = sh.doShow(a)
//}
@jiamingd
jiamingd / ImplicitByImplicitClass.sc
Last active August 25, 2017 19:10
f-bound vs type inference drill
trait Pet {
def name : String
}
trait Renamable[A] {
def rename( a: A, newName: String): A
}
case class Fish(name: String, age: Int)
object Fish {
@jiamingd
jiamingd / gist:2a033b412f0a8ab1535aeb573a582591
Created July 14, 2017 05:01
Fun playing with case class constructor overload
case class Car(color: String, size : Int, door : Int){
def this(door : Int) = this("silver", 6, door)
}
object Car{
def apply(door : Int ) = new Car(door)
}
val coupe = Car(2)
val car = Car("red", 8, 4)
@jiamingd
jiamingd / FunctorMonad.scala
Last active July 24, 2017 21:42
Light weight of Functor & Monad in scala work
case class MyFunctor[K](contextValue: K) {
def map[T](f: K => T) : MyFunctor[T]= {
MyFunctor( f(this.contextValue) )
}
}
case class MyMonad[K](contextValue : K) {
def flatMap[T](f: K => MyMonad[T]) : MyMonad[T] = {
println(s"flatMap called: $contextValue ")
f(this.contextValue)
@jiamingd
jiamingd / Heterogenous List
Created June 12, 2017 17:16
Typed return value can make the duplicate impl, the boilerplates gone, and more type safe. This is some drill and improvement on sample of "Scala in Depth"
object HLists {
trait HList {
type ConResult[A] <: HList
def ::[A](a: A): ConResult[A]
}
case class HCol[H, T](h: H, t: T) extends HList {
override type ConResult[A] = HCol[A, HCol[H, T]]
@jiamingd
jiamingd / How Felix clean him before go to bed
Last active August 24, 2017 15:44
Demo how to cake multiple traits
object TeathCleans {
trait TeathClean {
def cleanTeath
}
trait SonicTeathClean extends TeathClean {
override def cleanTeath = {
println("supersonic cleaning")
}
}
trait NormalBrushTeathClean extends TeathClean {