diff options
Diffstat (limited to 'Scala/objsets/src/main')
-rw-r--r-- | Scala/objsets/src/main/scala/objsets/TweetSet.scala | 39 |
1 files changed, 33 insertions, 6 deletions
diff --git a/Scala/objsets/src/main/scala/objsets/TweetSet.scala b/Scala/objsets/src/main/scala/objsets/TweetSet.scala index 9200c50..f558e48 100644 --- a/Scala/objsets/src/main/scala/objsets/TweetSet.scala +++ b/Scala/objsets/src/main/scala/objsets/TweetSet.scala @@ -42,7 +42,7 @@ abstract class TweetSet { * Question: Can we implment this method here, or should it remain abstract * and be implemented in the subclasses? */ - def filter(p: Tweet => Boolean): TweetSet = ??? + def filter(p: Tweet => Boolean): TweetSet = filterAcc(p, new Empty) /** * This is a helper method for `filter` that propagetes the accumulated tweets. @@ -55,7 +55,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. @@ -66,7 +66,12 @@ 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 + + /** + * This is a helper method for `mostRetweeted` search the set transporting the challenger + */ + def mostRetweetedElect(tweet: Tweet): Tweet /** * Returns a list containing all tweets of this set, sorted by retweet count @@ -77,7 +82,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 descendingByRetweet: TweetList /** @@ -110,7 +115,15 @@ abstract class TweetSet { class Empty extends TweetSet { - def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet = ??? + def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet = acc + + def union(that: TweetSet): TweetSet = that + + def mostRetweeted: Tweet = throw new NoSuchElementException + + def mostRetweetedElect(challenger: Tweet): Tweet = challenger + + def descendingByRetweet: TweetList = Nil /** @@ -128,7 +141,21 @@ class Empty extends TweetSet { class NonEmpty(elem: Tweet, left: TweetSet, right: TweetSet) extends TweetSet { - def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet = ??? + def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet = + if (p(elem)) right filterAcc(p, (left filterAcc(p, acc) incl elem)) + else right filterAcc(p, left filterAcc(p, acc)) + + def union(that: TweetSet): TweetSet = (right union (left union that)) incl elem + + def mostRetweeted: Tweet = mostRetweetedElect(elem) + + def mostRetweetedElect(challenger: Tweet): Tweet = + right mostRetweetedElect (left mostRetweetedElect (if (elem.retweets > challenger.retweets) elem else challenger)) + + def descendingByRetweet: TweetList = { + val most = mostRetweetedElect(elem) + new Cons(most, remove(most).descendingByRetweet) + } /** |