From 55a05ea736087fc6f9282466e0789b745679dd37 Mon Sep 17 00:00:00 2001 From: beauvill Date: Tue, 8 Oct 2019 11:31:01 +0200 Subject: [PATCH] Finished homework 3 --- src/main/scala/objsets/TweetSet.scala | 39 ++++++++++++++++++++------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/src/main/scala/objsets/TweetSet.scala b/src/main/scala/objsets/TweetSet.scala index ac88c95..a4c1b4f 100644 --- a/src/main/scala/objsets/TweetSet.scala +++ b/src/main/scala/objsets/TweetSet.scala @@ -41,12 +41,13 @@ abstract class TweetSet extends TweetSetInterface { * 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. */ - def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet + def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet + /** * Returns a new `TweetSet` that is the union of `TweetSet`s `this` and `that`. @@ -54,7 +55,7 @@ abstract class TweetSet extends TweetSetInterface { * 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 = that.filterAcc(elem => true, this) /** * Returns the tweet from this set which has the greatest retweet count. @@ -65,7 +66,7 @@ abstract class TweetSet extends TweetSetInterface { * 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 @@ -76,7 +77,7 @@ abstract class TweetSet extends TweetSetInterface { * Question: Should we implment this method here, or should it remain abstract * and be implemented in the subclasses? */ - def descendingByRetweet: TweetList = ??? + def descendingByRetweet: TweetList /** * The following methods are already implemented @@ -107,7 +108,11 @@ abstract class TweetSet extends TweetSetInterface { } class Empty extends TweetSet { - def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet = ??? + def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet = acc + + def mostRetweeted: Tweet = throw new java.util.NoSuchElementException + + def descendingByRetweet: TweetList = Nil /** * The following methods are already implemented @@ -124,8 +129,22 @@ 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)) then + left.filterAcc(p, right.filterAcc(p, acc.incl(elem))) + else + left.filterAcc(p, right.filterAcc(p, acc)) + def mostRetweeted: Tweet = { + var mostRet: Tweet = elem + (left union right).foreach(t => if (t.retweets > mostRet.retweets) then mostRet = t) + mostRet + } + + def descendingByRetweet: TweetList = { + val mostRet: Tweet = mostRetweeted + new Cons(mostRet,remove(mostRet).descendingByRetweet) + } /** * The following methods are already implemented @@ -185,14 +204,14 @@ object GoogleVsApple { val google = List("android", "Android", "galaxy", "Galaxy", "nexus", "Nexus") val apple = List("ios", "iOS", "iphone", "iPhone", "ipad", "iPad") - lazy val googleTweets: TweetSet = ??? - lazy val appleTweets: TweetSet = ??? + lazy val googleTweets: TweetSet = TweetReader.allTweets.filter(tweet => google.exists(elem => tweet.text.contains(elem))) + lazy val appleTweets: TweetSet = TweetReader.allTweets.filter(tweet => apple.exists(elem => tweet.text.contains(elem))) /** * A list of all tweets mentioning a keyword from either apple or google, * sorted by the number of retweets. */ - lazy val trending: TweetList = ??? + lazy val trending: TweetList = googleTweets.union(appleTweets).descendingByRetweet } object Main extends App {