Code Samples
authorFahad Ausaf <fahad.ausaf@kcl.ac.uk>
Sun, 09 Nov 2014 19:25:10 +0000
changeset 44 a751aa1ee4f7
parent 43 10e7a90d8e7a
child 45 7545b1bc1514
Code Samples
Fahad/CodeSamples/CaseClasses.scala
Fahad/CodeSamples/Classes.scala
Fahad/CodeSamples/CompoundTypes.scala
Fahad/CodeSamples/ExtractorObjects.scala
Fahad/CodeSamples/GenericClasses.scala
Fahad/CodeSamples/ImplicitParameters.scala
Fahad/CodeSamples/Main.scala
Fahad/CodeSamples/PredefineClassOf.scala
Fahad/CodeSamples/SequenceComprehensions.scala
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Fahad/CodeSamples/CaseClasses.scala	Sun Nov 09 19:25:10 2014 +0000
@@ -0,0 +1,36 @@
+package Main
+
+abstract class Term
+case class Var(name: String) extends Term
+case class Fun(arg: String, body: Term) extends Term
+case class Appli(f: Term, v: Term) extends Term
+
+object TermTest extends App {
+  def printTerm(term: Term) {
+    term match {
+      case Var(n) =>
+        print("" + n)
+      case Fun(x, b) =>
+        print("^" + x + ".")
+        printTerm(b)
+      case Appli(f, v) =>
+        Console.print("(")
+        printTerm(f)
+        print(" ")
+        printTerm(v)
+        print(")")
+    }
+  }
+  def isIdentityFun(term: Term): Boolean = term match {
+    case Fun(x, Var(y)) if x == y => true
+    case _ => false
+  }
+  val id = Fun("x", Var("x"))
+  println("id: " + id)
+  val t = Fun("x", Fun("y", Appli(Var("x"), Var("y"))))
+  println("t: " + t)
+  printTerm(t)
+  println
+  println(isIdentityFun(id))
+  println(isIdentityFun(t))
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Fahad/CodeSamples/Classes.scala	Sun Nov 09 19:25:10 2014 +0000
@@ -0,0 +1,20 @@
+package Main
+
+class Point(xc: Int, yc: Int) {
+  var x: Int = xc
+  var y: Int = yc
+  def move(dx: Int, dy: Int) {
+    x = x + dx
+    y = y + dy
+  }
+  override def toString(): String = "(" + x + ", " + y + ")";
+}
+
+object Classes{
+  def main(args: Array[String]){
+    val pt = new Point(1,2)
+    println(pt)
+    pt.move(10,10)
+    println(pt)
+  }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Fahad/CodeSamples/CompoundTypes.scala	Sun Nov 09 19:25:10 2014 +0000
@@ -0,0 +1,18 @@
+package Main
+
+object CompoundClasses {
+  trait Cloneable extends java.lang.Cloneable {
+    override def clone(): Cloneable = { super.clone(); this }
+  }
+
+  trait Resetable {
+    def reset: Unit
+  }
+
+  def cloneAndReset(obj: Cloneable with Resetable): Cloneable = {
+    val cloned = obj.clone()
+    obj.reset
+    cloned
+  }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Fahad/CodeSamples/ExtractorObjects.scala	Sun Nov 09 19:25:10 2014 +0000
@@ -0,0 +1,11 @@
+package Main
+
+object Twice {
+  def apply(x: Int): Int = x * 2
+  def unapply(z: Int): Option[Int] = if (z % 2 == 0) Some(z / 2) else None
+}
+
+object TwiceTest extends App {
+  val x = Twice(10)
+  x match { case Twice(n) => Console.println(n) } //prints 21
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Fahad/CodeSamples/GenericClasses.scala	Sun Nov 09 19:25:10 2014 +0000
@@ -0,0 +1,17 @@
+package Main
+
+class Stack[T] {
+  var elems: List[T] = Nil
+  def push(x: T) { elems = x :: elems }
+  def top: T = elems.head
+  def pop() { elems = elems.tail }
+}
+
+object GenericsTest extends App{
+  val stack = new Stack[Int]
+  stack.push(1)
+  stack.push('a')
+  println(stack.top)
+  stack.pop()
+  println(stack.top)
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Fahad/CodeSamples/ImplicitParameters.scala	Sun Nov 09 19:25:10 2014 +0000
@@ -0,0 +1,32 @@
+package Main
+
+abstract class SemiGroup[A] {
+  def add(x: A, y: A): A
+}
+
+abstract class Monoid[A] extends SemiGroup[A] {
+  def unit: A
+}
+
+object ImplicitTest extends App {
+  implicit object StringMonoid extends Monoid[String] {
+    def add(x: String, y: String): String = x concat y
+    def unit: String = ""
+  }
+
+  implicit object IntMonoid extends Monoid[Int] {
+    def add(x: Int, y: Int): Int = (x + y)
+    def unit: Int = 0
+  }
+
+  def sum[A](xs: List[A])(implicit m: Monoid[A]): A =
+    if (xs.isEmpty) m.unit
+    else {
+      println("head: " + xs.head);
+      println("tail: " + xs.tail);
+      m.add(xs.head, sum(xs.tail))
+    }
+
+  println(sum(List(1, 2, 3)))
+  println(sum(List("a", "b", "c")))
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Fahad/CodeSamples/Main.scala	Sun Nov 09 19:25:10 2014 +0000
@@ -0,0 +1,45 @@
+package Main
+
+abstract class Buffer{
+  type T
+  val element: T
+}
+
+abstract class SeqBuffer extends Buffer{
+  type U
+  type T <: Seq[U]
+  def length = element.length
+}
+
+abstract class IntSeqBuffer extends SeqBuffer{
+  type U = Int
+}
+
+object AbstractTypeTest1 extends App{
+  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)
+}
+
+abstract class Buffer2[+T] {
+  val element: T
+}
+
+abstract class SeqBuffer2[U, +T <: Seq[U]] extends Buffer2[T] {
+  def length = element.length
+}
+
+object AbstractTypeTest2 extends App {
+  def newIntSeqBuf(e1: Int, e2: Int): SeqBuffer2[Int, Seq[Int]] =
+    new SeqBuffer2[Int, List[Int]] {
+      val element = List(e1, e2)
+    }
+  val buf = newIntSeqBuf(7, 8)
+  println("length = " + buf.length)
+  println("content = " + buf.element)
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Fahad/CodeSamples/PredefineClassOf.scala	Sun Nov 09 19:25:10 2014 +0000
@@ -0,0 +1,21 @@
+package Main
+
+object ClassReprTest {
+  abstract class Bar {
+    type T <: AnyRef
+    def bar(x: T) {
+      println("5: " + x.getClass())
+    }
+  }
+  def main(args: Array[String]) {
+    println("1: " + args.getClass())
+    println("2: " + classOf[Array[String]])
+    
+    new Bar {
+      type T = Array[String]
+      val x: T = args
+      println("3: " + x.getClass())
+      println("4: " + classOf[T])
+    }.bar(args)
+  }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Fahad/CodeSamples/SequenceComprehensions.scala	Sun Nov 09 19:25:10 2014 +0000
@@ -0,0 +1,29 @@
+
+package Main
+
+object ComprehensionsTest extends App {
+  def even(from: Int, to: Int): List[Int] =
+    for (i <- List.range(from, to) if i % 2 == 0) yield i
+
+  Console.println(even(0, 20))
+}
+
+object ComprehensionsTest2 extends App {
+  def foo(n: Int, v: Int) =
+    for (
+      i <- 0 until n;
+      j <- i + 1 until n if i + j == v
+    ) yield Pair(i, j);
+
+  foo(20, 32) foreach {
+    case (i, j) =>
+      println("(" + i + "," + j + ")")
+  }
+}
+
+object ComprehensionTest3 extends App {
+  for (
+    i <- Iterator.range(0, 20);
+    j <- Iterator.range(i + 1, 20) if i + j == 32
+  ) println("(" + i + ", " + j + ")")
+}
\ No newline at end of file