Min/max with Option[T] for possibly empty Seq?

seq.reduceOption(_ min _) does what you want? Edit: Here’s an example incorporating your _.something: case class Foo(a: Int, b: Int) val seq = Seq(Foo(1,1),Foo(2,0),Foo(0,3)) val ord = Ordering.by((_: Foo).b) seq.reduceOption(ord.min) //Option[Foo] = Some(Foo(2,0)) or, as generic method: def minOptionBy[A, B: Ordering](seq: Seq[A])(f: A => B) = seq reduceOption Ordering.by(f).min which you could invoke with minOptionBy(seq)(_.something)

withFilter instead of filter

From the Scala docs: Note: the difference between c filter p and c withFilter p is that the former creates a new collection, whereas the latter only restricts the domain of subsequent map, flatMap, foreach, and withFilter operations. So filter will take the original collection and produce a new collection, but withFilter will non-strictly (i.e. … Read more

Converting a Java collection into a Scala collection

For future reference: With Scala 2.8, it could be done like this: import scala.collection.JavaConversions._ val list = new java.util.ArrayList[String]() list.add(“test”) val set = list.toSet set is a scala.collection.immutable.Set[String] after this. Also see Ben James’ answer for a more explicit way (using JavaConverters), which seems to be recommended now.

Why is Scala’s immutable Set not covariant in its type?

Set is invariant in its type parameter because of the concept behind sets as functions. The following signatures should clarify things slightly: trait Set[A] extends (A=>Boolean) { def apply(e: A): Boolean } If Set were covariant in A, the apply method would be unable to take a parameter of type A due to the contravariance … Read more

techhipbettruvabetnorabahisbahis forumu