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
{{ message }}
This repository has been archived by the owner on Nov 29, 2020. It is now read-only.
As discussed with @Odomontois on Telegram, in non-polymorphic context, Option[A] can be replaced with A | Null (primitives would have to be replaced with their Java counterparts in Scala2).
Consider the following function, that computes the maximum value of all odd numbers in a list:
defmaxOdd(l: List[Int]):Option[Int] = {
defgo(l: List[Int], sum: Option[Int]):Option[Int] = l match {
caseNil=> sum
case x :: xs if x %2==0=> go(xs, sum)
case x :: xs => sum match {
caseNone=> go(xs, Some(x))
caseSome(y) => go(xs, Some(x max y))
}
}
go(l, None)
}
We can optimize it as (Integer here plays the role of Int | Null):
defmaxOdd(l: List[Int]):Option[Int] = {
defgo(l: List[Int], sum: Integer):Integer= l match {
caseNil=> sum
case x :: xs if x %2==0=> go(xs, sum)
case x :: xs => sum match {
casenull=> go(xs, x)
case y => go(xs, x max y)
}
}
Option(go(l, null))
}
This sort of rewriting should only be applied locally and to private functions.
The text was updated successfully, but these errors were encountered:
As discussed with @Odomontois on Telegram, in non-polymorphic context,
Option[A]
can be replaced withA | Null
(primitives would have to be replaced with their Java counterparts in Scala2).Consider the following function, that computes the maximum value of all odd numbers in a list:
We can optimize it as (
Integer
here plays the role ofInt | Null
):This sort of rewriting should only be applied locally and to private functions.
The text was updated successfully, but these errors were encountered: