Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.enst.plnc2014.td1
- object TD1 {
- def isOdd(x: Int): Boolean = !isEven(x)
- def isEven(x: Int): Boolean = ((x % 2) == 0)
- }
- class ExtSeq[T](seq : Seq[T]){
- def any(f: (T => Boolean)): Boolean = seq.foldLeft(false)((a, b) => a || f(b))
- def all(f: (T => Boolean)): Boolean = seq.foldLeft(true)((a, b) => a && f(b))
- }
- object ExtSeq {
- implicit def toExtSeq[T](seq : Seq[T]) : ExtSeq[T] = new ExtSeq(seq)
- }
- case class Complex (a: Double, b : Double) {
- override val toString =
- if (b == 0.0) s"$a"
- else if (a == 0.0) s"${b}i"
- else if (b > 0.0) s"${a}+${b}i"
- else s"${a}${b}i"
- def reciprocal : Complex = new Complex(a, -b)
- def +(c : Complex) : Complex = new Complex(c.a + a, c.b + b)
- def -(c : Complex) : Complex = new Complex(a - c.a, b - c.b)
- def *(c : Complex) : Complex = new Complex(c.a * a - c.b * b, c.b * a + c.a + b)
- def *(c : Complex) : Complex = new Complex((a * c.a + b * c.b) / (c.a*c.a + c.b*c.b), (b*c.a - a+c.b) / (c.a*c.a + c.b*c.b))
- val abs = sqrt(a * a + b * b)
- }
- object Main extends App {
- import TD1._
- import scala.language.implicitConversions
- val c = (new Complex(3, 4)) + (new Complex(1, 2))
- println(c.toString)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement