读书人

Martin Odersky Scala编程公开课 第三

发布时间: 2013-10-10 14:14:51 作者: rapoo

Martin Odersky Scala编程公开课 第三周作业

Functional Programming Principles in Scala
by Martin Odersky这次的作业叫做Object-Oriented Sets。要完成一个完整的类,实现取最大值、排序等方法。由于是函数式编程,这些的实现方法和以往我知道的完全不一样。
总结TweetSet有两个子类,Empty和NonEmpty,使用BFT实现。有一点比较惊奇的是父类的方法可以创建一个子类的对象,以往并没有认识到这一点。
在方法的实现上,filter和mostRetweeted都需要使用辅助方法(acc)来做递归。类似于contains的实现方法,很巧妙的对类进行了遍历。有的方法需要在两个子类里面以不同的方法实现。对于把一个类分成父类和两个子类这一点,我的感觉是为了最大程度的把子类的公共部分抽象出来,杀鸡用了牛刀。我的实现里面,mostRetweeted调用了mostAcc,同时传递了一个新建的Tweet。为了更优美应该分别在两个子类里面实现。程序优化很少,感觉运行效率很低,很奇怪有些公司用scala来运营网站等。
我的程序
Download the objsets.zip handout archive file.In this assignment you will work with an object-oriented representations based on binary trees.Object-Oriented SetsFor this part, you will earn credit by completing the TweetSet.scala file. This file defines an abstract class TweetSet with two concrete subclasses, Empty which represents an empty set, and NonEmpty(elem: Tweet, left: TweetSet, right: TweetSet), which represents a non-empty set as a binary tree rooted at elem. The tweets are indexed by their text bodies: the bodies of all tweets on the left are lexicographically smaller than elem and all bodies of elements on the right are lexicographically greater.Note also that these classes are immutable: the set-theoretic operations do not modify this but should return a new set.Before tackling this assignment, we suggest you first study the already implemented methods contains and incl for inspiration.1 FilteringImplement filtering on tweet sets. Complete the stubs for the methods filter and filterAcc. filter takes as argument a function, the predicate, which takes a tweet and returns a boolean. filter then returns the subset of all the tweets in the original set for which the predicate is true. For example, the following call:tweets.filter(tweet => tweet.retweets > 10)applied to a set tweets of two tweets, say, where the first tweet was not retweeted and the second tweet was retweeted 20 times should return a set containing only the second tweet.Hint: start by defining the helper method filterAcc which takes an accumulator set as a second argument. This accumulator contains the ongoing result of the filtering./** This method takes a predicate and returns a subset of all the elements *  in the original set for which the predicate is true. */def filter(p: Tweet => Boolean): TweetSetdef filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSetThe definition of filter in terms of filterAcc should then be straightforward.2 Taking UnionsImplement union on tweet sets. Complete the stub for the method union. The method union takes another set that, and computes a new set which is the union of this and that, i.e. a set that contains exactly the elements that are either in this or in that, or in both.def union(that: TweetSet): TweetSetNote that in this exercise it is your task to find out in which class(es) to define the union method (should it be abstract in class TweetSet?).3 Sorting Tweets by Their InfluenceThe more often a tweet is “re-tweeted” (that is, repeated by a different user with or without additions), the more influential it is.The goal of this part of the exercise is to add a method descendingByRetweet to TweetSet which should produce a linear sequence of tweets (as an instance of class TweetList), ordered by their number of retweets:def descendingByRetweet: TweetListThis method reflects a common pattern when transforming data structures. While traversing one data structure (in this case, a TweetSet), we’re building a second data structure (here, an instance of class TweetList). The idea is to start with the empty list Nil (containing no tweets), and to find the tweet with the most retweets in the input TweetSet. This tweet is removed from the TweetSet (that is, we obtain a new TweetSet that has all the tweets of the original set except for the tweet that was “removed”; this immutable set operation, remove, is already implemented for you), and added to the result list by creating a new Cons. After that, the process repeats itself, but now we are searching through a TweetSet with one less tweet.Hint: start by implementing the method mostRetweeted which returns the most popular tweet of a TweetSet.4 Tying everything togetherIn the last step of this assignment your task is to detect influential tweets in a set of recent tweets. We are providing you with a TweetSet containing several hundred tweets from popular tech news sites in the past few days, located in the TweetReader object (file “TweetReader.scala”). TweetReader.allTweets returns an instance of TweetSet containing a set of all available tweets.Furthermore, you are given two lists of keywords. The first list corresponds to keywords associated with Google and Android smartphones, while the second list corresponds to keywords associated with Apple and iOS devices. Your objective is to detect which platform has generated more interest or activity in the past few days.As a first step, use the functionality you implemented in the first parts of this assignment to create two different TweetSets, googleTweets and appleTweets. The first TweetSet, googleTweets, should contain all tweets that mention (in their “text”) one of the keywords in the google list. The second TweetSet, appleTweets, should contain all tweets that mention one of the keyword in the apple list. Their signature is as follows:lazy val googleTweets: TweetSetlazy val appleTweets: TweetSetHint: use the exists method of List and contains method of class java.lang.String.From the union of those two TweetSets, produce trending, an instance of class TweetList representing a sequence of tweets ordered by their number of retweets:lazy val trending: TweetList



2楼se77en_py1小时前
还有你提到的父类的方法可以创建子类是什么意思?子类不都是继承父类而来的吗?
Re: caozhankui1小时前
回复se77en_pyn啊,我写的不规范,应该是创建子类的一个对象
Re: se77en_py55分钟前
回复caozhankuin还是不明白,什么地方创建子类的对象了?不都是继承了父类的方法吗?
Re: se77en_py45分钟前
回复caozhankuin嗯,我比较好奇的是它的类居然能接受参数,还有就是Trait好灵活
1楼se77en_py1小时前
你写的比我写的简洁多了,惭愧啊...

读书人网 >编程

热点推荐