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.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 |
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
//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') |
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
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) | |
//} |
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
trait Pet { | |
def name : String | |
} | |
trait Renamable[A] { | |
def rename( a: A, newName: String): A | |
} | |
case class Fish(name: String, age: Int) | |
object Fish { |
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
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) |
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
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) |
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
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]] |
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
object TeathCleans { | |
trait TeathClean { | |
def cleanTeath | |
} | |
trait SonicTeathClean extends TeathClean { | |
override def cleanTeath = { | |
println("supersonic cleaning") | |
} | |
} | |
trait NormalBrushTeathClean extends TeathClean { |