Skip to content

Instantly share code, notes, and snippets.

@khajavi
Created June 10, 2015 15:40
Show Gist options
  • Save khajavi/688b538c834b83b5be8b to your computer and use it in GitHub Desktop.
Save khajavi/688b538c834b83b5be8b to your computer and use it in GitHub Desktop.
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.13.0")
name := "Hello"
version := "0.1"
scalaVersion := "2.11.6"
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.")
}
}
}
////////////////////////////
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