Finished homework 3

This commit is contained in:
beauvill 2019-10-08 11:31:01 +02:00
parent 4f98102c94
commit 55a05ea736

View File

@ -41,20 +41,21 @@ 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
/**
* Returns a new `TweetSet` that is the union of `TweetSet`s `this` and `that`.
*
* 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 {