Skip to content

Commit

Permalink
Got a ten now. I think i'm getting the hang of thngs now :)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrueastman committed Nov 12, 2016
1 parent f2dc5e8 commit 7085ffb
Showing 1 changed file with 35 additions and 35 deletions.
70 changes: 35 additions & 35 deletions objsets/src/main/scala/objsets/TweetSet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ class Tweet(val user: String, val text: String, val retweets: Int) {
*/
abstract class TweetSet {

def isEmpty: Boolean

/**
* This method takes a predicate and returns a subset of all the elements
* in the original set for which the predicate is true.
*
* Question: Can we implment this method here, or should it remain abstract
* and be implemented in the subclasses?
*/
def filter(p: Tweet => Boolean): TweetSet = filterAcc(p, new Empty)
def filter(p: Tweet => Boolean): TweetSet = filterAcc(p,new Empty)

/**
* This is a helper method for `filter` that propagetes the accumulated tweets.
Expand All @@ -54,7 +56,7 @@ abstract class TweetSet {
* Question: Should we implment this method here, or should it remain abstract
* and be implemented in the subclasses?
*/
def union(that: TweetSet): TweetSet
def union(that: TweetSet): TweetSet

/**
* Returns the tweet from this set which has the greatest retweet count.
Expand All @@ -65,7 +67,7 @@ abstract class TweetSet {
* Question: Should we implment this method here, or should it remain abstract
* and be implemented in the subclasses?
*/
def mostRetweeted: Tweet
def mostRetweeted: Tweet

/**
* Returns a list containing all tweets of this set, sorted by retweet count
Expand All @@ -76,9 +78,7 @@ abstract class TweetSet {
* Question: Should we implment this method here, or should it remain abstract
* and be implemented in the subclasses?
*/
def descendingByRetweet: TweetList

def empty:Boolean
def descendingByRetweet: TweetList

/**
* The following methods are already implemented
Expand Down Expand Up @@ -109,15 +109,12 @@ abstract class TweetSet {
}

class Empty extends TweetSet {

def descendingByRetweet: TweetList= Nil

def empty= true

def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet = acc //same as returning empty

def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet = acc
def isEmpty: Boolean = true

def union(that: TweetSet): TweetSet = that
def mostRetweeted = throw new java.util.NoSuchElementException
def mostRetweeted: Tweet = throw new java.util.NoSuchElementException("No Tweets here")
def descendingByRetweet: TweetList = Nil

/**
* The following methods are already implemented
Expand All @@ -133,31 +130,33 @@ class Empty extends TweetSet {
}

class NonEmpty(elem: Tweet, left: TweetSet, right: TweetSet) extends TweetSet {

def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet = {
if(p(elem)) left.filterAcc(p,right.filterAcc(p,acc.incl(elem)))
else left.filterAcc(p,right.filterAcc(p,acc))
}

def descendingByRetweet: TweetList= new Cons(mostRetweeted, remove(mostRetweeted).descendingByRetweet)
def empty=false
def mostRetweeted = {
lazy val leftMost = left.mostRetweeted
lazy val rightMost = right.mostRetweeted
def union(that: TweetSet): TweetSet = {
filterAcc(tw => true,that)
}

if( !left.empty && leftMost.retweets > elem.retweets )
if( !right.empty && rightMost.retweets > leftMost.retweets )
rightMost
else
leftMost
else if( !right.empty && rightMost.retweets > elem.retweets )
rightMost
else
elem
def isEmpty: Boolean = false
def mostRetweeted: Tweet = {
lazy val leftMost= left.mostRetweeted
lazy val rightMost= right.mostRetweeted
if(!left.isEmpty && leftMost.retweets > elem.retweets){
if(!right.isEmpty && rightMost.retweets > leftMost.retweets)
rightMost
else
leftMost
}
else if(!right.isEmpty && rightMost.retweets > elem.retweets)
rightMost
else
elem
}
def descendingByRetweet: TweetList = new Cons(mostRetweeted,remove(mostRetweeted).descendingByRetweet)

def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet = {
if(p(elem)) left.filterAcc(p, right.filterAcc(p, acc.incl(elem)))
else left.filterAcc(p, right.filterAcc(p, acc))
}

def union(that: TweetSet): TweetSet= ((left union right) union that) incl elem

/**
* The following methods are already implemented
*/
Expand Down Expand Up @@ -213,6 +212,7 @@ object GoogleVsApple {

lazy val googleTweets: TweetSet = ???
lazy val appleTweets: TweetSet = ???

/**
* A list of all tweets mentioning a keyword from either apple or google,
* sorted by the number of retweets.
Expand Down

0 comments on commit 7085ffb

Please sign in to comment.