This file contains hidden or 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
// 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 |
This file contains hidden or 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
// 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") |
This file contains hidden or 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
// 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") |
This file contains hidden or 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
// 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") |
This file contains hidden or 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
// 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 { |
This file contains hidden or 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
// 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 |
This file contains hidden or 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 java.io._ | |
// Pickling binary | |
import scala.pickling._ | |
import binary._ | |
import scala.pickling.Defaults._ | |
// msgpack | |
import org.msgpack.annotation.Message | |
import org.msgpack.ScalaMessagePack |
This file contains hidden or 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 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) |
This file contains hidden or 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 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 |