You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Going through the docs, I realized that Function1, Function2 are
only specialized on return type Boolean, not for arguments. For
higher-order methods such as fold, a specialization on Boolean
would be quite handy for performance.
The text was updated successfully, but these errors were encountered:
Wouldn't think it is necessary to specialize for Boolean. Boolean only has two values true and false. When it is "boxed", it is actually referencing two pre-defined java.lang.Boolean objects, which does not impact performance at all. Can definitely close this one.
val q: Boolean => Boolean = b => !b
def h(x: Boolean) = q(x)
Am I supposed to expect boxing? The same with Int param does not box.
Here, h boxes before calling the lambda, which unboxes before calling my static !_.
You can mix in a specialized trait:
trait P { def apply(b: Boolean): Boolean }
val p: (Boolean => Boolean) with P = new Function1[Boolean, Boolean] with P {
override def apply(b: Boolean) = !b
}
Boxed apply unboxes and calls apply(Z) as you would hope. Of course h would use unboxed P#apply.
I recently
def f(xs: List[Int]): Option[Int] =
xs.collectFirst { case i: Int if i > 42 => i > 50 }
.map {
if (_) 1
else -1
}
I see that this form is actually worse, as you don't get to handle the boxed value; it always unboxes, so it reboxes for the eq!
def ffs(xs: List[Int]): Option[Int] =
xs.collectFirst { case i: Int if i > 42 => i > 50 }
.map(b => if (b eq java.lang.Boolean.TRUE) 1 else -1)
Presumably, only by taking Any (and casting to AnyRef) can you wind up doing the obvious:
def testit[A >: Boolean <: Any](v: A): Int = if (v.asInstanceOf[AnyRef] eq java.lang.Boolean.TRUE) 1 else -1
Going through the docs, I realized that Function1, Function2 are
only specialized on return type Boolean, not for arguments. For
higher-order methods such as fold, a specialization on Boolean
would be quite handy for performance.
The text was updated successfully, but these errors were encountered: