From 022c591d1ff008041f712e65149670f568404c1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Sun, 14 Apr 2013 18:12:34 +0200 Subject: Scala : objsets: implements TweetSet --- .../objsets/src/main/scala/objsets/TweetSet.scala | 39 ++++++++++++++++++---- 1 file 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) + } /** -- cgit v1.1-2-g2b99