减少一组空集是否有效? [英] Is it valid to reduce on an empty set of sets?

查看:12
本文介绍了减少一组空集是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不应该工作吗?

> val setOfSets = Set[Set[String]]()    
setOfSets: scala.collection.immutable.Set[Set[String]] = Set()

> setOfSets reduce (_ union _)
java.lang.UnsupportedOperationException: empty.reduceLeft
  at scala.collection.TraversableOnce$class.reduceLeft(TraversableOnce.scala:152)
  [...]

推荐答案

Reduce(左右)不能应用于空集合.

Reduce (left and right) cannot be applied on an empty collection.

概念上:

myCollection.reduce(f)

类似于:

myCollection.tail.fold( myCollection.head )( f )

因此集合必须至少有一个元素.

Thus the collection must have at least one element.

这篇关于减少一组空集是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆