Skip to content

Instantly share code, notes, and snippets.

@trumanw
trumanw / Extractor.scala
Created May 4, 2015 06:45
Extractors in Scala --- Matching Object With Patterns
// Cited from the essay "Matching Object With Patterns"
// Class hierarchy:
trait Expr
class Num(val value: Int) extends Expr
class Var(val name: String) extends Expr
class Mul(val left: Expr, val right: Expr) extends Expr
// Extractors here:
// apply is the injection
// unapply is the extraction
@trumanw
trumanw / CaseClass.scala
Created May 4, 2015 06:44
Case Class in Scala --- Matching Object With Patterns
// Cited from the essay "Matching Object With Patterns"
// Class hierarchy:
trait Expr
case class Num(val value: Int) extends Expr
case class Var(val name: String) extends Expr
case class Mul(val left: Expr, val right: Expr) extends Expr
object CaseClass extends App {
val numObj = new Num(1)
val varObj = new Var("var")
@trumanw
trumanw / TypeCase.scala
Created May 4, 2015 06:42
Type Case in Scala --- Matching Object With Patterns
// Cited from the essay "Matching Object With Patterns"
// Class hierarchy:
trait Expr
class Num(val value: Int) extends Expr
class Var(val name: String) extends Expr
class Mul(val left: Expr, val right: Expr) extends Expr
object Typecase extends App {
val numObj = new Num(1)
val varObj = new Var("var")
@trumanw
trumanw / TypeTestAndCast.scala
Created May 4, 2015 06:41
Type Test/Type Cast in Scala --- Matching Object With Patterns
// Cited from the essay "Matching Object With Patterns"
// Class hierarchy:
trait Expr
class Num(val value: Int) extends Expr
class Var(val name: String) extends Expr
class Mul(val left: Expr, val right: Expr) extends Expr
object TypeTestCast extends App {
val numObj = new Num(1)
val varObj = new Var("var")
@trumanw
trumanw / Visitors.scala
Created May 4, 2015 06:39
Visitors in Scala --- Matching Object With Patterns
// Cited from the essay "Matching Object With Patterns"
// Class hierarchy:
trait Visitor[T] {
def caseMul(t: Mul): T = otherwise(t)
def caseNum(t: Num): T = otherwise(t)
def caseVar(t: Var): T = otherwise(t)
def otherwise(t: Expr): T = throw new NullPointerException
}
trait Expr {
@trumanw
trumanw / OODecomposition.scala
Created May 4, 2015 06:38
Object Oriented Decomposition in Scala --- Matching Object With Patterns
// Cited from the essay "Matching Object With Patterns"
// Class hierarchy:
trait Expr {
def isVar: Boolean = false
def isNum: Boolean = false
def isMul: Boolean = false
def value: Int = throw new NullPointerException
def name: String = throw new NullPointerException
def left: Expr = throw new NullPointerException
def right: Expr = throw new NullPointerException
@trumanw
trumanw / SerializationDemo.scala
Created April 22, 2015 10:54
Serializable vs Pickle vs MsgPack (file input & output)
import java.io._
// Pickling binary
import scala.pickling._
import binary._
import scala.pickling.Defaults._
// msgpack
import org.msgpack.annotation.Message
import org.msgpack.ScalaMessagePack
@trumanw
trumanw / SerializationDemo.scala
Created April 22, 2015 05:20
Serialization demo in scala
import java.io._
@SerialVersionUID(124L)
class Person(var name: String)
extends Serializable {
override def toString = f"My name is $name%s."
}
@SerialVersionUID(123L)
class Pet(var name: String, var species: String, var owner: Person)
@trumanw
trumanw / HexStringUtil.scala
Created April 20, 2015 07:08
String to hex bytes String in Scala.
object HexStringUtil {
// convert normal string to hex bytes string
def string2hex(str: String): String = {
str.toList.map(_.toInt.toHexString).mkString
}
// convert hex bytes string to normal string
def hex2string(hex: String): String = {
hex.sliding(2, 2).toArray.map(Integer.parseInt(_, 16).toChar).mkString