Is it valid for decreasing on an empty set of sets?

Doesn't that work?

> 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) [...] 
+6
source share
2 answers

Zoom out (left and right) cannot be applied to an empty collection.

Conceptually:

 myCollection.reduce(f) 

look like:

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

Thus, a collection must have at least one element.

+18
source

This should do what you want:

 setOfSets.foldLeft(Set[String]())(_ union _) 

Although I did not understand the requirement not to indicate the order.

+10
source

Source: https://habr.com/ru/post/894569/


All Articles