Last active
August 29, 2015 14:22
-
-
Save khajavi/b8844efcc37b70cd0d74 to your computer and use it in GitHub Desktop.
My Scala Practices
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
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.13.0") |
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
name := "Hello" | |
version := "0.1" | |
scalaVersion := "2.11.6" | |
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
// gist-id: b8844efcc37b70cd0d74 | |
// references: | |
// 1. http://www.scala-lang.org/docu/files/ScalaTutorial.pdf | |
trait Equal { | |
def isEqual(x: Any): Boolean | |
} | |
object Hi { | |
var factor = 3 | |
val multiplier = (i:Int) => i * factor | |
def main(args: Array[String]) = { | |
println("Hi!") | |
val p1 = new Point(2,4) | |
val p2 = new Point(3,5) | |
val p3 = new Point(2,4) | |
println(p1.isEqual(p3)) | |
println("----------------") | |
val l1 = new Location(1,2,3) | |
println(l1) | |
println(multiplier(3)) | |
println("-------Collection--------") | |
val x = List(1,2,3) | |
println (x) | |
val y = Set(4,5,6) | |
println (y) | |
val z = Map("one" -> 1, "two" -> 2, "three" -> 3) | |
println (z) | |
val m = (10, "Scala") | |
println (m) | |
val n:Option[Int] = Some(5) | |
println (n) | |
println("-------Mixin---------") | |
class Iter extends StringIterator("Test") with RichIterator | |
val iter = new Iter | |
iter foreach println | |
println("------abstract-types---------") | |
abstract class IntSeqBuffer extends SeqBuffer { | |
type U = Int | |
} | |
abstract class SeqBuffer extends Buffer { | |
type U | |
type T <: Seq[U] | |
def length = element.length | |
} | |
trait Buffer { | |
type T | |
val element: T | |
} | |
def newIntSeqBuf(elem1: Int, elem2: Int): IntSeqBuffer = | |
new IntSeqBuffer { | |
type T = List[U] | |
val element = List(elem1, elem2) | |
} | |
val buf = newIntSeqBuf(7, 8) | |
println("length = " + buf.length) | |
println("content = " + buf.element) | |
println("----------------------------") | |
trait A { | |
def a = 1 | |
} | |
trait X extends A { | |
override def a = { | |
println("X") | |
super.a | |
} | |
} | |
trait Y extends A { | |
override def a = { | |
println("Y") | |
super.a | |
} | |
} | |
val xy = new AnyRef with X with Y | |
println(xy.a) | |
val yx = new AnyRef with Y with X | |
println(yx.a) | |
println("-----------------------------") | |
abstract class Term | |
case class Var(name: String) extends Term | |
val test1 = Var("Test") | |
val test2 = Var("Test") | |
println(test1.name) | |
println( test1 == test2 ) | |
println("-----------------------------") | |
// http://stackoverflow.com/a/11203867/225052 | |
sealed trait Answer | |
case object Yes extends Answer | |
case object No extends Answer | |
val a1: Answer = Yes | |
a1 match { | |
case No => println("No") | |
case Yes => println("Yes") | |
} | |
println("-------------case classes (Algebric data type)----------") | |
sealed abstract class Tree | |
case class Node(left: Tree, right: Tree) extends Tree | |
case class Leaf[A](value: A) extends Tree | |
case object EmtptyLeaf extends Tree | |
val treeA = Node(EmtptyLeaf, Node(Node(EmtptyLeaf, EmtptyLeaf), Leaf(3))) | |
treeA match { | |
case Node(EmtptyLeaf, Node(_, Leaf(_))) => println("This is the right tree") | |
case _ => println("Not right tree.") | |
} | |
println("-----------Functions-are-Objects-----------") | |
// var counter:Int = 0 | |
def worker(callback: () => Unit) { | |
var counter:Int = 0; | |
while (counter < 5) { | |
callback(); | |
Thread sleep 100 | |
counter += 1 | |
} | |
} | |
def timeFlies() { | |
println("time filies like an arrow...") | |
} | |
worker(timeFlies) | |
worker(() => println("anonymouse function")) | |
println("-----------Class-Private-Members-----------") | |
class Complex(real: Double, imaginary: Double) { | |
def re = real | |
def im = imaginary | |
} | |
var comp1 = new Complex(1.5, 2.3) | |
// println(comp.real) real and imaginary are private member | |
println("real= " + comp1.re) | |
println("imaginary= " + comp1.im) | |
println("-----------Class-Private-Members-----------") | |
class Complex2(var real: Double, val imaginary: Double) { | |
def re = real | |
def im = imaginary | |
} | |
var comp2 = new Complex2(1.5, 2.3) | |
println(comp2.real + " " + comp2.imaginary) | |
println("-----------Overriding-----------") | |
class Complex3(var real: Double, val imaginary: Double) { | |
def re = real | |
def im = imaginary | |
override def toString = "" + re + (if (im < 0) "" else "+") + im + "i" | |
} | |
var comp3 = new Complex3(1.5, 2.3) | |
println(comp3) | |
} | |
} | |
//////////////////////////// | |
abstract class AbsIterator { | |
type T | |
def hasNext: Boolean | |
def next: T | |
} | |
trait RichIterator extends AbsIterator { | |
type T | |
def hasNext: Boolean | |
def next: T | |
def foreach(f: T => Unit) = while (hasNext) f(next) | |
} | |
class StringIterator(s: String) extends AbsIterator { | |
type T = Char | |
private var i = 0 | |
def hasNext = i < s.length() | |
def next = { val ch = s charAt i; i += 1; ch } | |
} | |
class Point(val xc: Int,val yc: Int) extends Equal { | |
var x: Int = xc | |
var y: Int = yc | |
def isEqual(obj: Any) = | |
obj.isInstanceOf[Point] && | |
obj.asInstanceOf[Point].x == x | |
} | |
class Location(override val xc:Int, override val yc: Int, | |
val zc: Int) extends Point(xc, yc) { | |
var z: Int = zc | |
override def toString = xc + " " + yc + " " + zc | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment