Override "operators" and println method in Scala - scala

I need to create methods for basic operations for different types so the output of the expression: println(1 + 2*I + I*3 + 2) is 3+5i. I am new to Scala and here is what I have so far:
class IClass() {
var value = 0
def *(number: Int): String = {
//value += number
value + "i"
}
}
object ComplexNumbers {
var TotalValue: Int = 0
var TotalString: String = ""
// ...
def Complex(num1: Int, num2: Int): String ={
num1 + "+" + num2 + "i"
}
implicit class IntMultiply(private val a: Int) extends AnyVal {
def + (b: String)= {
if(b.contains("i")){
TotalValue += a
TotalString.concat(b)
}
}
def * (b: IClass) = {
//b.value += a
a + "i"
}
}
implicit class StringAdd(private val a: String) extends AnyVal {
def + (b: String): String = {
if(b.contains("i")){
}
a + "i"
}
}
def main(args: Array[String]) {
println(Complex(1,2)) // 1+2i
val I = new IClass()
println(1 + 2*I + I*3 + 2) // 3+5i
// val c = (2+3*I + 1 + 4*I) * I
// println(-c) // 7-3i
}
}
I think I am going in a wrong direction with this because by implementing these operation methods on types I get an error in the println: Type Mismach because of the Any return type where I only update fields without returning anything. Any idea how to implement this?

You should think of the complex numbers as a class with certain behaviors, and define it first, rather than focusing on the one concrete side effect you are after at the moment. It seems counter intuitive, but implementing a more abstract/general problem often makes the job easier than trying to narrow it down to just the task at hand.
case class ComplexInt(real: Int, im: Int) {
def + (other: ComplexInt) = ComplexInt(real + other.real, im + other.im)
def * (other: ComplexInt) = ComplexInt(
real * other.real - im * other.im,
real * other.im + im * other.real
)
def unary_- = ComplexInt(-real, -im)
def -(other: ComplexInt) = this + -other
override def toString() = (if(real == 0 && im != 0) "" else real.toString) + (im match {
case 0 => ""
case 1 if real == 0 => "i"
case 1 => " + i"
case n if n < 0 || real == 0 => s"${n}i"
case n => s"+${n}i"
})
}
object ComplexInt {
val I = ComplexInt(0, 1)
implicit def fromInt(n: Int) = ComplexInt(n, 0)
}
Now, you just need to import ComplexInt.I,
and then things like println(1 + 2*I + I*3 + 2) will print 3+5i etc.
You can even do (1 + 2*I)*(2 + 3*I) (evaluates to -4+7i).

Related

Non-numerical type returned by curried function

New to Scala and functional programming in general. Have the following code snippet which means to calculate the sum over a closed interval of Ints. Have implemented both tail-recursive method (sum1) and non-tail-recursive method (sum2).
package test
import scala.annotation.tailrec
object Currying extends App{
def sum1(f:Int=>Int)(a:Int, b:Int): (Int, Int) => Int = {
require(a <= b, "Received a=" + a + " and b=" + b)
#tailrec
def sum(a:Int, acc:Int) : Int = {
if(a > b) acc
else sum(a+1, acc + f(a))
}
sum
}
println("sum1(x=>x*x)(1, 4)=" + sum1(x=>x*x)(1, 4))
println("sum1(x=>x*x*x)(1, 4)=" + sum1(x=>x*x*x)(1, 4))
def sum2(f:Int=>Int)(a:Int, b:Int): Int => Int = {
def sum(a:Int) : Int = {
if(a > b) 0
else f(a) + sum(a+1)
}
sum
}
println("sum2(x=>x*x)(1, 4)=" + sum2(x=>x*x)(1, 4))
println("sum2(x=>x*x*x)(1, 4)=" + sum2(x=>x*x*x)(1, 4))
}
The print-outs, however, do not return actual Ints, but what appears to be a function itself:
sum(x=>x*x)(1, 4)=week2.Currying$$$Lambda$12/1468177767#3d24753a
sum(x=>x*x*x)(1, 4)=week2.Currying$$$Lambda$12/1468177767#67b64c45
sum2(x=>x*x)(1, 4)=week2.Currying$$$Lambda$15/1626877848#2f7a2457
sum2(x=>x*x*x)(1, 4)=week2.Currying$$$Lambda$15/1626877848#6108b2d7
Process finished with exit code 0
Clearly I'm doing something dumb in the currying, but I can't for the life of me figure it out.

Monadic approach to estimating PI in scala

I'm trying to understand how to leverage monads in scala to solve simple problems as way of building up my familiarity. One simple problem is estimating PI using a functional random number generator. I'm including the code below for a simple stream based approach.
I'm looking for help in translating this to a monadic approach. For example, is there an idiomatic way convert this code to using the state (and other monads) in a stack safe way?
trait RNG {
def nextInt: (Int, RNG)
def nextDouble: (Double, RNG)
}
case class Point(x: Double, y: Double) {
val isInCircle = (x * x + y * y) < 1.0
}
object RNG {
def nonNegativeInt(rng: RNG): (Int, RNG) = {
val (ni, rng2) = rng.nextInt
if (ni > 0) (ni, rng2)
else if (ni == Int.MinValue) (0, rng2)
else (ni + Int.MaxValue, rng2)
}
def double(rng: RNG): (Double, RNG) = {
val (ni, rng2) = nonNegativeInt(rng)
(ni.toDouble / Int.MaxValue, rng2)
}
case class Simple(seed: Long) extends RNG {
def nextInt: (Int, RNG) = {
val newSeed = (seed * 0x5DEECE66DL + 0xBL) & 0xFFFFFFFFFFFFL
val nextRNG = Simple(newSeed)
val n = (newSeed >>> 16).toInt
(n, nextRNG)
}
def nextDouble: (Double, RNG) = {
val (n, nextRNG) = nextInt
double(nextRNG)
}
}
}
object PI {
import RNG._
def doubleStream(rng: Simple):Stream[Double] = rng.nextDouble match {
case (d:Double, next:Simple) => d #:: doubleStream(next)
}
def estimate(rng: Simple, iter: Int): Double = {
val doubles = doubleStream(rng).take(iter)
val inside = (doubles zip doubles.drop(3))
.map { case (a, b) => Point(a, b) }
.filter(p => p.isInCircle)
.size * 1.0
(inside / iter) * 4.0
}
}
// > PI.estimate(RNG.Simple(10), 100000)
// res1: Double = 3.14944
I suspect I'm looking for something like replicateM from the Applicative monad in cats but I'm not sure how to line up the types or how to do it in a way that doesn't accumulate intermediate results in memory. Or, is there a way to do it with a for comprehension that can iteratively build up Points?
Id you want to iterate using monad in a stack safe way, then there is a tailRecM method implemented in Monad type class:
// assuming random generated [-1.0,1.0]
def calculatePi[F[_]](iterations: Int)
(random: => F[Double])
(implicit F: Monad[F]): F[Double] = {
case class Iterations(total: Int, inCircle: Int)
def step(data: Iterations): F[Either[Iterations, Double]] = for {
x <- random
y <- random
isInCircle = (x * x + y * y) < 1.0
newTotal = data.total + 1
newInCircle = data.inCircle + (if (isInCircle) 1 else 0)
} yield {
if (newTotal >= iterations) Right(newInCircle.toDouble / newTotal.toDouble * 4.0)
else Left(Iterations(newTotal, newInCircle))
}
// iterates until Right value is returned
F.tailRecM(Iterations(0, 0))(step)
}
calculatePi(10000)(Future { Random.nextDouble }).onComplete(println)
It uses by-name param because you could try to pass there something like Future (even though the Future is not lawful), which are eager, so you would end up with evaluating the same thing time and time again. With by name param at least you have the chance of passing there a recipe for side-effecting random. Of course, if we use Option, List as a monad holding our "random" number, we should also expect funny results.
The correct solution would be using something that ensures that this F[A] is lazily evaluated, and any side effect inside is evaluated each time you need a value from inside. For that you basically have to use some of Effects type classes, like e.g. Sync from Cats Effects.
def calculatePi[F[_]](iterations: Int)
(random: F[Double])
(implicit F: Sync[F]): F[Double] = {
...
}
calculatePi(10000)(Coeval( Random.nextDouble )).value
calculatePi(10000)(Task( Random.nextDouble )).runAsync
Alternatively, if you don't care about purity that much, you could pass side effecting function or object instead of F[Int] for generating random numbers.
// simplified, hardcoded F=Coeval
def calculatePi(iterations: Int)
(random: () => Double): Double = {
case class Iterations(total: Int, inCircle: Int)
def step(data: Iterations) = Coeval {
val x = random()
val y = random()
val isInCircle = (x * x + y * y) < 1.0
val newTotal = data.total + 1
val newInCircle = data.inCircle + (if (isInCircle) 1 else 0)
if (newTotal >= iterations) Right(newInCircle.toDouble / newTotal.toDouble * 4.0)
else Left(Iterations(newTotal, newInCircle))
}
Monad[Coeval].tailRecM(Iterations(0, 0))(step).value
}
Here is another approach that my friend Charles Miller came up with. It's a bit more direct since it uses RNG directly but it follows the same approach provided by #Mateusz Kubuszok above that leverages Monad.
The key difference is that it leverages the State monad so we can thread the RNG state through the computation and generate the random numbers using the "pure" random number generator.
import cats._
import cats.data._
import cats.implicits._
object PICharles {
type RNG[A] = State[Long, A]
object RNG {
def nextLong: RNG[Long] =
State.modify[Long](
seed ⇒ (seed * 0x5DEECE66DL + 0xBL) & 0xFFFFFFFFFFFFL
) >> State.get
def nextInt: RNG[Int] = nextLong.map(l ⇒ (l >>> 16).toInt)
def nextNatural: RNG[Int] = nextInt.map { i ⇒
if (i > 0) i
else if (i == Int.MinValue) 0
else i + Int.MaxValue
}
def nextDouble: RNG[Double] = nextNatural.map(_.toDouble / Int.MaxValue)
def runRng[A](seed: Long)(rng: RNG[A]): A = rng.runA(seed).value
def unsafeRunRng[A]: RNG[A] ⇒ A = runRng(System.currentTimeMillis)
}
object PI {
case class Step(count: Int, inCircle: Int)
def calculatePi(iterations: Int): RNG[Double] = {
def step(s: Step): RNG[Either[Step, Double]] =
for {
x ← RNG.nextDouble
y ← RNG.nextDouble
isInCircle = (x * x + y * y) < 1.0
newInCircle = s.inCircle + (if (isInCircle) 1 else 0)
} yield {
if (s.count >= iterations)
Right(s.inCircle.toDouble / s.count.toDouble * 4.0)
else
Left(Step(s.count + 1, newInCircle))
}
Monad[RNG].tailRecM(Step(0, 0))(step(_))
}
def unsafeCalculatePi(iterations: Int) =
RNG.unsafeRunRng(calculatePi(iterations))
}
}
Thanks Charles & Mateusz for your help!

Creating Singleton Object from a class in Scala

When I was playing with the Scala, I couldn't figure out something. Maybe I am doing completely wrong.
I was trying with Rational Example and Complex Example but I couldn't find a way to use operations like R*3/5 and 1/2*R
here is the complex numbers example I am working on
class Complex(val real : Int, val img : Int){
def this(real: Int) = this(real, 0)
def *(that : Complex) = {
val realPart = this.real * that.real + -(this.img * that.img)
val imgPart = this.real * that.img + this.img * that.real
new Complex(realPart, imgPart)
}
override def toString = this.real + "+" + this.img + "i"
}
object Complex {
def apply(real : Int, img : Int) = new Complex(real, img)
def apply(real : Int) = new Complex(real)
}
object ComplexNumbers {
def main(args: Array[String]) {
import ComplexConversions._
println(Complex(1,2)) // 1+2i
println(I*2) //0+2i
println(2*I) //0+2i
}
}
Well I have tried to create an object I
object I{
def apply() = new Complex(0,1)
def *(that : Complex) = {
val realPart = 0 * that.real + -(1 * that.img)
val imgPart = 0 * that.img + 1 * that.real
new Complex(realPart, imgPart)
}
}
but it did work for the I*2. but I have problems for 2*I. How can I reach the result that I want?
When you call "I * 2", scala looks for a method named "*" on the class of I, and finds it.
When you call "2 * I", scala looks for a method named "*" on the class of 2 (which is Int), and cannot find one.
Even though Int is defined externally, you can add this method to it in Scala via the "implicit conversion" mechanism. This is covered briefly in the "implicits" example and in more detail elsewhere, e.g. here
Try adding some code like the following to your "Complex" object:
object Complex {
implicit class IntOps(x: Int) {
def *(y: Complex) = y * x
}
}
You'll also need to declare I as a val, rather than an Object for this to work:
val I = Complex(0, 1)
(or add an implicit method like class Complex { def *(i: I) = ... }, but that's much uglier)
(I assume by Complex Example, you mean this?)
Working code:
class Complex(val real : Int, val img : Int){
def this(real: Int) = this(real, 0)
def *(that : Complex) = {
val realPart = this.real * that.real + -(this.img * that.img)
val imgPart = this.real * that.img + this.img * that.real
new Complex(realPart, imgPart)
}
override def toString = this.real + "+" + this.img + "i"
}
object Complex {
def apply(real : Int, img : Int) = new Complex(real, img)
def apply(real : Int) = new Complex(real)
val I = Complex(0, 1)
implicit def toComplex(x: Int): Complex = new Complex(x)
}
object ComplexNumbers {
def main(args: Array[String]) {
import Complex._
println(Complex(1,2)) // 1+2i
println(I*2) //0+2i
println(2*I) //0+2i
}
}
If you want to be able to use 2*I, you will need to add a new * override for the Int class (since * is really a method of the class Int, meaning 2*I is really 2.*(I)).
You can accomplish this with an implicit class:
scala> case class myInt(i: Int){
| def mult(that: Int): myInt = myInt(that * i)
| }
defined class myInt
scala> implicit class intOverride(i: Int){
| def *(that: myInt): myInt = that.mult(i)
| }
defined class intOverride
scala> val a = myInt(2)
a: myInt = myInt(2)
scala> 2 * a
res1: myInt = myInt(4)

toString function in Scala

I'm new to Scala, I was reading about scala from the following source: http://docs.scala-lang.org/tutorials/tour/classes
It had the following code:
class Point(var x: Int, var y: Int) {
def move(dx: Int, dy: Int): Unit = {
x = x + dx
y = y + dy
}
override def toString: String =
"(" + x + ", " + y + ")"
}
object Classes {
def main(args: Array[String]) {
val pt = new Point(1, 2)
println(pt)
pt.move(10, 10)
println(pt)
}
}
The output is:
(1, 2)
(11, 12)
I wanted to ask how did the println(pt) function printed the result (1,2)? Should we not call pt.toString() to print the result as shown?
There's an overload of println that accepts a value of type Any (in Predef.scala):
def println(x: Any) = Console.println(x)
Deep inside, it calls x.toString() to get the string to print.

Get value directly or via method in Scala?

One is able to define methods without paranthesis if they have no arguments. A special use-case is to use this to get values. Should I do this, or rather directly get the value?
So
class Complex(real: Double, imaginary: Double) {
def re = real
def im = imaginary
override def toString() =
"" + re + (if (im < 0) "" else "+") + im + "i"
}
or
class Complex(real: Double, imaginary: Double) {
val re = real
val im = imaginary
override def toString() =
"" + re + (if (im < 0) "" else "+") + im + "i"
}
You can put val in front of the constructor arguments to make it a bit shorter, which is effectively the same as your second piece of code:
class Complex(val re: Double, val im: Double) {
override def toString() = "" + re + (if (im < 0) "" else "+") + im + "i"
}
Note that def defines a method, while val defines a final member variable. Since the return value of these two methods is fixed, there's not really a reason to make them methods. So, in this case, use val instead of def.
Even better: make it a case class:
case class Complex(re: Double, im: Double) {
override def toString() = "%f%+fi".format(re,im)
}
This gives you re and im as members, plus some additional perks, such as copy:
val a = Complex(1,2)
val realA = a.copy(im = 0)
and unapply:
def isImaginary(a: Complex) = a match {
case Complex(0, _) => true
case _ => false
}
def abs(a: Complex) = a match {
case Complex(re, im) => re*re + im*im
}
and also equals, hashCode, etc.