Scala范围表示object.propery不';不存在,但eclipse中的左面板显示了它

Scala范围表示object.propery不';不存在,但eclipse中的左面板显示了它,eclipse,scala,scope,Eclipse,Scala,Scope,我正在尝试构建一个简单的程序,但看起来好像我遇到了一个我根本不理解的小问题: 我有以下代码: package objsets import common._ import TweetReader._ /** * A class to represent tweets. */ class Tweet(val user: String, val text: String, val retweets: Int) { override def toString: String =

我正在尝试构建一个简单的程序,但看起来好像我遇到了一个我根本不理解的小问题:

我有以下代码:

    package objsets

import common._
import TweetReader._

/**
 * A class to represent tweets.
 */
class Tweet(val user: String, val text: String, val retweets: Int) {
  override def toString: String =
    "User: " + user + "\n" +
    "Text: " + text + " [" + retweets + "]"
}

/**
 * This represents a set of objects of type `Tweet` in the form of a binary search
 * tree. Every branch in the tree has two children (two `TweetSet`s). There is an
 * invariant which always holds: for every branch `b`, all elements in the left
 * subtree are smaller than the tweet at `b`. The eleemnts in the right subtree are
 * larger.
 *
 * Note that the above structure requires us to be able to compare two tweets (we
 * need to be able to say which of two tweets is larger, or if they are equal). In
 * this implementation, the equality / order of tweets is based on the tweet's text
 * (see `def incl`). Hence, a `TweetSet` could not contain two tweets with the same
 * text from different users.
 *
 *
 * The advantage of representing sets as binary search trees is that the elements
 * of the set can be found quickly. If you want to learn more you can take a look
 * at the Wikipedia page [1], but this is not necessary in order to solve this
 * assignment.
 * 
 * [1] http://en.wikipedia.org/wiki/Binary_search_tree
 */
abstract class TweetSet {

  def isEmpty: Boolean
  /**
   * This method takes a predicate and returns a subset of all the elements
   * in the original set for which the predicate is true.
   *
   * Question: Can we implment this method here, or should it remain abstract
   * and be implemented in the subclasses?
   */
  def filter(p: Tweet => Boolean): TweetSet

  /**
   * 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;

  /**
   * Returns the tweet from this set which has the greatest retweet count.
   *
   * Calling `mostRetweeted` on an empty set should throw an exception of
   * type `java.util.NoSuchElementException`.
   *
   * Question: Should we implment this method here, or should it remain abstract
   * and be implemented in the subclasses?
   */
  def mostRetweeted: Tweet = ???

  /**
   * Returns a list containing all tweets of this set, sorted by retweet count
   * in descending order. In other words, the head of the resulting list should
   * have the highest retweet count.
   *
   * Hint: the method `remove` on TweetSet will be very useful.
   * Question: Should we implment this method here, or should it remain abstract
   * and be implemented in the subclasses?
   */
  def descendingByRetweet: TweetList


  /**
   * The following methods are already implemented
   */

  /**
   * Returns a new `TweetSet` which contains all elements of this set, and the
   * the new element `tweet` in case it does not already exist in this set.
   *
   * If `this.contains(tweet)`, the current set is returned.
   */
  def incl(tweet: Tweet): TweetSet

  /**
   * Returns a new `TweetSet` which excludes `tweet`.
   */
  def remove(tweet: Tweet): TweetSet

  /**
   * Tests if `tweet` exists in this `TweetSet`.
   */
  def contains(tweet: Tweet): Boolean

  /**
   * This method takes a function and applies it to every element in the set.
   */
  def foreach(f: Tweet => Unit): Unit
}

class Empty extends TweetSet {

  def descendingByRetweet() = Nil
  def union(that: TweetSet): TweetSet = that  
  def isEmpty = true

  def filter(p: Tweet=> Boolean): TweetSet = new Empty()

  def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet = new Empty()


  /**
   * The following methods are already implemented
   */

  def contains(tweet: Tweet): Boolean = false

  def incl(tweet: Tweet): TweetSet = new NonEmpty(tweet, new Empty, new Empty)

  def remove(tweet: Tweet): TweetSet = this

  def foreach(f: Tweet => Unit): Unit = ()
}

class NonEmpty(elem: Tweet, left: TweetSet, right: TweetSet) extends TweetSet {

  def descendingByRetweet = {


    def iterateAndAddList(list: TweetList,first: Boolean): TweetList = {

      val current: Tweet = greatestCurrent()
      if(current != Nil && first != true){
      list.head = current
      list.isEmpty = false
      list.tail = Nil
      remove(current)
      iterateAndAddList(list,false)
      }else list

    }

    def greatestCurrent():Tweet = {
      new Tweet("a","b",21)
    }

    iterateAndAddList(Nil,true)

  }

  def union(that: TweetSet): TweetSet = (left.union(right)).union(that).incl(elem)
  val isEmpty = false

  def filter(p: Tweet => Boolean): TweetSet = filterAcc(p,new Empty)

  def filterAcc(p: Tweet => Boolean, acc: TweetSet): TweetSet = {

    if(left.isEmpty && right.isEmpty) acc
    else if(p(elem)){ left.filterAcc(p,acc.incl(elem)).union(right.filterAcc(p,acc.incl(elem)))}
    else left.filterAcc(p,acc).union(right.filterAcc(p,acc))


  }


  /**
   * The following methods are already implemented
   */

  def contains(x: Tweet): Boolean =
    if (x.text < elem.text) left.contains(x)
    else if (elem.text < x.text) right.contains(x)
    else true

  def incl(x: Tweet): TweetSet = {
    if (x.text < elem.text) new NonEmpty(elem, left.incl(x), right)
    else if (elem.text < x.text) new NonEmpty(elem, left, right.incl(x))
    else this
  }

  def remove(tw: Tweet): TweetSet =
    if (tw.text < elem.text) new NonEmpty(elem, left.remove(tw), right)
    else if (elem.text < tw.text) new NonEmpty(elem, left, right.remove(tw))
    else left.union(right)

  def foreach(f: Tweet => Unit): Unit = {
    f(elem)
    left.foreach(f)
    right.foreach(f)
  }
}









//--------------------------------------------------------------------------------------------
















trait TweetList {
  def head: Tweet
  def tail: TweetList
  def isEmpty: Boolean
  def foreach(f: Tweet => Unit): Unit =
    if (!isEmpty) {
      f(head)
      tail.foreach(f)
    }
}

object Nil extends TweetList {
  def head = throw new java.util.NoSuchElementException("head of EmptyList")
  def tail = throw new java.util.NoSuchElementException("tail of EmptyList")
  def isEmpty = true
}

class Cons(val head: Tweet, val tail: TweetList) extends TweetList {
  def isEmpty = false
}


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 = ???

  /**
   * A list of all tweets mentioning a keyword from either apple or google,
   * sorted by the number of retweets.
   */
  lazy val trending: TweetList = ???
}

object Main extends App {
  // Print the trending tweets
  GoogleVsApple.trending foreach println
}

出于某种原因似乎被隐藏了?这个属性不应该是可访问的吗?

不确定“隐藏”是什么意思,但其中一个问题可能是这里

def descendingByRetweet = {

  def iterateAndAddList(list: TweetList,first: Boolean): TweetList = {
    val current: Tweet = greatestCurrent()
    if(current != Nil && first != true){
      list.head = current
      list.isEmpty = false
      list.tail = Nil
      remove(current)
      iterateAndAddList(list,false)
    }
    else list
  }

  def greatestCurrent():Tweet = {
    new Tweet("a","b",21)
  }

  iterateAndAddList(Nil,true)

}
您正在重新分配给
def
TweetList
是不可变的,您不能为它们分配属性,相反,您可以创建一个新元素,然后将其传递给函数并保持递归:

def descendingByRetweet = {

  def iterateAndAddList(list: TweetList,first: Boolean): TweetList = {
    val current: Tweet = greatestCurrent()
    if(current != Nil && first != true) iterateAndAddList(new Cons(current, Nil), false)
    else list
  }

  def greatestCurrent():Tweet = {
    new Tweet("a","b",21)
  }

  iterateAndAddList(Nil,true)

}

这是在Scala中的不可变集合上递归的方法,您始终需要创建一个新对象。

我没有看到任何
按顺序下降
您的意思是
按网络下降
?这是一种特性。但我正在讨论Cons和Nil,它们实现了TweetList,因此试图实现多态性。无论如何,即使它们是空的,为什么编译器会返回错误?没有调用任何方法。完全没有。编译器运行失败,并引发异常,表示TweetList.head不可能。但它应该是,因为它有这些元素作为参数?任何扩展它的类都应该。我遗漏了什么?我尝试了你的代码,但它没有编译,
list。head
是可能的,但你正在
list.head=current
中重新分配给val。然后我如何修改Cons和Nil(都是TweetList)的head和tail并继承head和tail属性?我对答案进行了编辑,我不明白
remove(current)
应该做什么,因为它返回一个
TweetList
,并且您没有将其结果分配给任何变量,所以我认为这应该是
Cons
的尾部,无论如何,您可以传递一个
Nil
而不是
remove(current)的结果
Cons
对象,如果它适合您的需要。
def descendingByRetweet = {

  def iterateAndAddList(list: TweetList,first: Boolean): TweetList = {
    val current: Tweet = greatestCurrent()
    if(current != Nil && first != true) iterateAndAddList(new Cons(current, Nil), false)
    else list
  }

  def greatestCurrent():Tweet = {
    new Tweet("a","b",21)
  }

  iterateAndAddList(Nil,true)

}