在Scala Seq中获得具有字段最大值的对象

时间:2018-09-10 16:41:41

标签: scala playframework scala-collections

Cycle是一个对象,它具有一个称为cycleNumber的字段,我要获取其最大CycleNumber的Cycle对象。为此,我正在尝试:

componentDidUpdate(prevProps) { //prevProps is the previous props of the component before being updated
   //so, if this.props != prevProps it means that component props have been updated

    if(this.props.isEditingbook && !prevProps.isEditingbook) {
      let book = this.props.index.bookDictionary[this.props.currentbook.id] 
      book.name = this.state.model.name.value
    }
    this.setState({ ...this.state, ...prevProps})
  }

但是,我遇到了匹配错误,我相信这是对最新命令的尊重,因为相应的表中有2行用于Cycle对象。

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

如果要从Seq获取最大值,可以使用sortBy,maxBy,reduce或fold

    legendItemClick: function(event) {
      var chart = this.series.chart

      if (this.visible) {
        // this.series.visible = false;
        ySum = ySum - event.target.options.y;
      } else {
        ySum = ySum + event.target.options.y;
      }
      chart.setTitle({
        text: ySum
      }, false, false);

      return true
    }

答案 1 :(得分:1)

class MyPickerView: UIPickerView {

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        self.changeGradientLayer()
    }

    func changeGradientLayer() {
        guard let sublayers = self.subviews.first?.layer.sublayers else { return }

        for case let sublayer as CAGradientLayer in sublayers {
            sublayer.colors = [UIColor.black.withAlphaComponent(0).cgColor,
                               UIColor.black.withAlphaComponent(0.71).cgColor,
                               UIColor.black.withAlphaComponent(1.0).cgColor,
                               UIColor.black.withAlphaComponent(1.0).cgColor,
                               UIColor.black.withAlphaComponent(0.71).cgColor,
                               UIColor.black.withAlphaComponent(0).cgColor]
        }
    }

}

一个简单的实现:

  1. 将Seq [Cycle]转换为Seq [(Cycle,Int)]每个元组的._1是循环本身,而._2是其循环编号(如果没有则为0)
  2. 获取具有最大cycleNumber的元组
  3. 返回该元组的第一个元素(._1)或循环自身