Place an implicit conversion in the companion object and import it into the client scope, so it is available for both an integer argument and an integer receiver:
import scala.language.implicitConversions
class Rational(n0: BigInt, d0: BigInt) {
def this(n: Int, d: Int) = this(BigInt(n), BigInt(d))
require(d0 != 0)
private val g = n0.abs.gcd(d0.abs)
val num: BigInt = (if (d0 < 0) -n0 else n0) / g
val den: BigInt = d0.abs / g
def *(r: Rational): Rational =
new Rational(num*r.num, den*r.den)
override def toString: String =
if (den == 1) num.toString else num.toString + "/" + den.toString
}
object Rational {
implicit def fromInt(n: Int): Rational = new Rational(n,1)
}
import Rational._
val r1 = new Rational(3,7)
val r2 = new Rational(-11,-5)
println(r1*r2)
println(r1*3)
println(7*r1)Normalization gives . The output lines are
33/35, 9/7, 3. For r1*3, the argument is converted; for 7*r1, conversion supplies the receiver's rational multiplication method. BigInt keeps the exact rational arithmetic independent of fixed-width product overflow.An infinite iterator can keep the current index and triangular value:
val triangular = new Iterator[BigInt] {
private var n = BigInt(0)
private var total = BigInt(0)
def hasNext: Boolean = true
def next(): BigInt = {
val result = total
n += 1
total += n
result
}
}
println(triangular.take(8).toList)Before each call,
total is the triangular number . Returning it and then using preserves this invariant. The prefix is . Arbitrary-precision integers prevent wraparound as the indefinitely available iterator advances.Immutable Scala
List[+A] is covariant: if Juice is a subtype of Drink, a list of juices can be used as a list of drinks. Scala Array[A] is invariant because it permits both reading and writing elements.If an array of juices could be treated as an array of drinks, writing a beer through that widened reference would violate the original juice-array contract. This illustrates covariant type parameter versus generic type invariance; immutability permits the first relationship for lists.
class VendingMachine[+A](val currentItem: Option[A], items: List[A]) {
def this(items: List[A]) = this(None,items)
def dispenseNext(): VendingMachine[A] = items match {
case Nil =>
if (currentItem.isDefined) new VendingMachine[A](None,Nil)
else this
case t :: ts => new VendingMachine[A](Some(t),ts)
}
def addAll[B >: A](newItems: List[B]): VendingMachine[B] =
new VendingMachine[B](items ++ newItems)
}The lower type bound permits the old pending elements of type
A to be used as type B, so concatenation is type-correct. The resulting machine is immutable and can return the wider type without changing the existing machine. With the given client expressions, VM1 has type VendingMachine[Juice] and VM2 can have type VendingMachine[Drink]. The supplied addAll body constructs a new pending-item machine; its constructor sets currentItem to None. Articles by others on the same topic
There are currently no matching articles.