From bd22ca41ef60c0c43df851ea4b226dfaaa9950cc Mon Sep 17 00:00:00 2001 From: jayunit100 Date: Wed, 10 Dec 2014 19:12:00 -0500 Subject: [PATCH] generic transaction storage and unit tests. final cleaned up, needs testing --- .../scala/littlepetstore/LittlePetStore.scala | 142 ------------------ src/main/scala/littlepetstore/MathApp.scala | 26 ---- .../scala/littlepetstore/PetStoreActors.scala | 43 ------ .../scala/littlepetstore/PetStoreApp.scala | 140 ----------------- .../littlepetstore/PetStoreDataStrucs.scala | 40 ----- src/main/scala/littlepetstore/Sytax.scala | 55 ------- 6 files changed, 446 deletions(-) delete mode 100644 src/main/scala/littlepetstore/LittlePetStore.scala delete mode 100644 src/main/scala/littlepetstore/MathApp.scala delete mode 100644 src/main/scala/littlepetstore/PetStoreActors.scala delete mode 100644 src/main/scala/littlepetstore/PetStoreApp.scala delete mode 100644 src/main/scala/littlepetstore/PetStoreDataStrucs.scala delete mode 100644 src/main/scala/littlepetstore/Sytax.scala diff --git a/src/main/scala/littlepetstore/LittlePetStore.scala b/src/main/scala/littlepetstore/LittlePetStore.scala deleted file mode 100644 index 5afd37c..0000000 --- a/src/main/scala/littlepetstore/LittlePetStore.scala +++ /dev/null @@ -1,142 +0,0 @@ -package littlepetstore - -import scala.Product -import reflect.runtime.universe._ -import scala.io.BytePickle.PU -import littlepetstore.PetStoreActors.Animal; -import littlepetstore.PetStoreActors.Customer; -import littlepetstore.PetStoreActors.Employee; -import littlepetstore.PetStoreActors.Human; -import littlepetstore.PetStoreActors.PetStoreActor; - -import scala.tools.cmd.ParserUtil - -object LittlePetStore { - - - class PetStoreHumanActorActions[T](self: PetStoreActor[T]) { - def complain() = { - System.out.println("im bored at this stupid petstore.") - } - } - - class PetStoreActorActions[T](self: PetStoreActor[T]) { - def register() = { - println("REGISTERING ACTOR " + this + " WITH SYSTEM."); - } - - def die() = { - System.out.println("Customer disappearing...") - } - - def start() = { - System.out.println("Customer appearing...") - } - - } - - /** - * Demonstrate the way that implicits allow us - * to create an API for Employees that differs from animals. - */ - def demoImplicits() = { - - //implicit mapper from actor -> actions - implicit def a2act[T](a: PetStoreActor[T]) = new PetStoreActorActions(a); - //implicit mapper specifically for humans -> actions. - //this allows us to complain. - implicit def ha2act[T<:Human](a: PetStoreActor[T]) = new PetStoreHumanActorActions(a); - - def actor1 = new PetStoreActor[Employee] {} - actor1.start(); - actor1.die(); - actor1.complain(); - - /** - * Notice that this actor cannot "complain", because it - * does not inherit from Human. - */ - def actor2 = new PetStoreActor[Animal] {} - actor2.start(); - actor2.die(); - //actor2.complain(); - - def actors = List(actor1,actor2); - for(a <- actors){ - println(a) - } - } - - /** - * Now, lets say we want to analyze all existing actors in the petstore - * simulation. For example, we want to confirm that the first two actors are - * employee/animal. - * Example of how to match cases to inspect the generic types in a list. - */ - def demoCases() = { - def actor1 = new PetStoreActor[Employee] {} - def actor2 = new PetStoreActor[Employee] {} - def actor3 = new PetStoreActor[Animal] {} - - var actors = List(actor1, actor2, actor3, actor3); - println(); - actors match { - case (_: Employee) :: (_: Employee) :: tail - => {println("nice 2 employees to start with ")}; - case Nil - => {println("list is nill. not way to match.")} - case _ - => {println("no match")} - } - - - } - - def processActor(a:PetStoreActor[Employee]) = { - System.out.println("processing "+a); - } - - /** - * Example of some Classic functional idioms (fold/destructuring/etc) - */ - def demoTuples() = { - implicit def s2act(name: String) = new PetStoreActor[Employee](){ - override def toString() : String = { - name - } - }; - //We can create a management hierarchy like so, - def boss : PetStoreActor[Employee] = new String("ASDF"); - - System.out.println("org chart : " + boss); - } - - def demoActors() = { - implicit def s2act(name: String) = new PetStoreActor[Employee](){ - override def toString() : String = { - name - } - }; - //We can create a management hierarchy like so, - def boss : PetStoreActor[Employee] = new String("ASDF"); - - System.out.println("org chart : " + boss); - } - - - class PetStore { - - demoTuples(); - demoImplicits(); - demoCases(); - demoActors(); - } - - - - def main(args: Array[String]) { - - new PetStore; - } - -} \ No newline at end of file diff --git a/src/main/scala/littlepetstore/MathApp.scala b/src/main/scala/littlepetstore/MathApp.scala deleted file mode 100644 index e333c8a..0000000 --- a/src/main/scala/littlepetstore/MathApp.scala +++ /dev/null @@ -1,26 +0,0 @@ - -package littlepetstore - -/** - * Created by jay on 8/6/14. - */ -object MathApp extends App { - - def pow(n: Int, k: Int): Int = { - - k match { - case 0 => return 1; - case 1 => return n; - case 2 => return n * n; - case kk if kk % 2 == 0 => return pow(pow(n, k / 2), 2); - case _ => return pow(n, k / 2) * pow(n, (k + 1) / 2) - } - } - - - def x: Int = 2; - def y: Int = 4; - println(pow(x, y)) -} - - diff --git a/src/main/scala/littlepetstore/PetStoreActors.scala b/src/main/scala/littlepetstore/PetStoreActors.scala deleted file mode 100644 index b11740c..0000000 --- a/src/main/scala/littlepetstore/PetStoreActors.scala +++ /dev/null @@ -1,43 +0,0 @@ -package littlepetstore - -import scala.runtime.RichInt - -/** - * Created by jay on 7/29/14. - */ -object IDs{ - var intId : RichInt = 0 - - def newId():RichInt= { - val myNewId = Math.random()*10000 - intId = new RichInt(myNewId.toInt).intValue() - return intId; - } -} -object PetStoreActors { - - trait PetStoreActor[Type] { - //Random integer id. - def id = IDs.newId(); - } - - abstract class Customer extends PetStoreActor[Human] { - def email = "nobody@hello.com"; - } - - trait Employee extends Human{ - def salary = 100.00f; - def hello () : String = { - "ASDF" - } - } - trait Human { - def fName = {"joe"}; - def lname = {"bloggs"} - } - - trait Animal { - def name = "fido" - } - -} diff --git a/src/main/scala/littlepetstore/PetStoreApp.scala b/src/main/scala/littlepetstore/PetStoreApp.scala deleted file mode 100644 index 1571451..0000000 --- a/src/main/scala/littlepetstore/PetStoreApp.scala +++ /dev/null @@ -1,140 +0,0 @@ -package littlepetstore - -import scala.Product -import reflect.runtime.universe._ -import scala.io.BytePickle.PU -import littlepetstore.PetStoreActors.Animal; -import littlepetstore.PetStoreActors.Customer; -import littlepetstore.PetStoreActors.Employee; -import littlepetstore.PetStoreActors.Human; -import littlepetstore.PetStoreActors.PetStoreActor; - -import scala.tools.cmd.ParserUtil - -object PetStoreApp{ - - - class PetStoreHumanActorActions[T](self: PetStoreActor[T]) { - def complain() = { - System.out.println("im bored at this stupid petstore.") - } - } - - class PetStoreActorActions[T](self: PetStoreActor[T]) { - def register() = { - println("REGISTERING ACTOR " + this + " WITH SYSTEM."); - } - - def die() = { - System.out.println("Customer disappearing...") - } - - def start() = { - System.out.println("Customer appearing...") - } - - } - - /** - * Demonstrate the way that implicits allow us - * to create an API for Employees that differs from animals. - */ - def demoImplicits() = { - - //implicit mapper from actor -> actions - implicit def a2act[T](a: PetStoreActor[T]) = new PetStoreActorActions(a); - - //implicit mapper specifically for humans -> actions. - //this allows us to complain. - implicit def ha2act[T<:Human](a: PetStoreActor[T]) = new PetStoreHumanActorActions(a); - - def actor1 = new PetStoreActor[Employee] {} - actor1.start(); - actor1.die(); - actor1.complain(); - - /** - * Notice that this actor cannot "complain", because it - * does not inherit from Human. - */ - def actor2 = new PetStoreActor[Animal] {} - actor2.start(); - actor2.die(); - //actor2.complain(); - - def actors = List(actor1,actor2); - for(a <- actors){ - println(a) - } - } - - /** - * Now, lets say we want to analyze all existing actors in the petstore - * simulation. For example, we want to confirm that the first two actors are - * employee/animal. - * Example of how to match cases to inspect the generic types in a list. - */ - def demoCases() = { - def actor1 = new PetStoreActor[Employee] {} - def actor2 = new PetStoreActor[Employee] {} - def actor3 = new PetStoreActor[Animal] {} - - var actors = List(actor1, actor2, actor3, actor3); - println(); - actors match { - case (_: Employee) :: (_: Employee) :: tail - => {println("nice 2 employees to start with ")}; - case Nil - => {println("list is nill. not way to match.")} - case _ - => {println("no match")} - } - - - } - - def processActor(a:PetStoreActor[Employee]) = { - System.out.println("processing "+a); - } - - /** - * Example of some Classic functional idioms (fold/destructuring/etc) - */ - def demoTuples() = { - implicit def s2act(name: String) = new PetStoreActor[Employee](){ - override def toString() : String = { - name - } - }; - //We can create a management hierarchy like so, - def boss : PetStoreActor[Employee] = new String("ASDF"); - - System.out.println("org chart : " + boss); - } - - def demoActors() = { - implicit def s2act(name: String) = new PetStoreActor[Employee](){ - override def toString() : String = { - name - } - }; - //We can create a management hierarchy like so, - def boss : PetStoreActor[Employee] = new String("ASDF"); - - System.out.println("org chart : " + boss); - } - - - class PetStore { - - demoTuples(); - demoImplicits(); - demoCases(); - demoActors(); - } - - def main(args: Array[String]) { - new PetStore; - } - -} \ No newline at end of file diff --git a/src/main/scala/littlepetstore/PetStoreDataStrucs.scala b/src/main/scala/littlepetstore/PetStoreDataStrucs.scala deleted file mode 100644 index fee1a25..0000000 --- a/src/main/scala/littlepetstore/PetStoreDataStrucs.scala +++ /dev/null @@ -1,40 +0,0 @@ -package littlepetstore - -/** - * Created by jay on 8/14/14. - */ -object PetStoreDataStrucs { - - sealed trait LinkedList[+E] - - case class Node[+E](val head : E , val tail : LinkedList[E] ) - extends LinkedList[E]{ - } - - case object Empty extends LinkedList[Nothing] - - object LinkedList { - - //Factory method, like a default constructor. - //Allows you to do "LinkedList(1,2,3)" - def apply[E]( items : E* ) : LinkedList[E] = { - if (items.isEmpty) { - Empty - } else { - Node( items.head, apply(items.tail : _*) ) - } - } - } - class RUN{ - System.out.println("ASDF") - val n = LinkedList(1) - System.out.println(n) - } - - def main(args: Array[String]) { - - new RUN; - } - - -} diff --git a/src/main/scala/littlepetstore/Sytax.scala b/src/main/scala/littlepetstore/Sytax.scala deleted file mode 100644 index 9089eb6..0000000 --- a/src/main/scala/littlepetstore/Sytax.scala +++ /dev/null @@ -1,55 +0,0 @@ -package littlepetstore - -/** - * Why does FreeUser fail to compile? - */ -object Sytax { - - object Thrice { - def apply(x : Int) : Int = x *3 - def unapply(z : Int) : Option[Int] = if (z%3==0) Some(z/3) else None - } - - val x = Thrice(3); - - trait User { - def name:String; - def id:Int; - } - - //impl 1 - class FreeUser(val name: String, val id:Int) extends User {} - object FreeUser { - def unapply(user: FreeUser):Option[(String,Int)] = Some((user.name,user.id)); - } - //impl 2 - class PremiumUser(val name: String, val id:Int) extends User {} - object PremiumUser { - def unapply(user: PremiumUser):Option[(String,Int)]= Some((user.name,user.id)); - } - - - def hello(a:Int) = { - 1+1 - } - object premiumCandidate { - def unapply(user: FreeUser): Boolean = user.id> 100 - } - - val name = "ASDF" - val user: User = new PremiumUser("jay",1); - - - def main(args: Array[String]) { - - //now, we can use FreeUser / PremiumUser to do pattern matching against - //a newly defined user. The user - val xx = user match { - // case u @ premiumCandidate => "hi, you could be premium!" - case FreeUser(name) => "hi..." + name + " you are mediocre. someday you can be premium."; - case PremiumUser(name) => "Dear Premium User " + name + ". Hello . You are special !"; - } - print(xx); - - } -} \ No newline at end of file