Advertisement
Guest User

Untitled

a guest
Jun 8th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.21 KB | None | 0 0
  1. package fr.enst.plnc2014.td1
  2.  
  3.  
  4. object TD1 {
  5.  
  6.   def isOdd(x: Int): Boolean = !isEven(x)
  7.   def isEven(x: Int): Boolean = ((x % 2) == 0)
  8.  
  9. }
  10.  
  11.  
  12. class ExtSeq[T](seq : Seq[T]){
  13.  
  14.   def any(f: (T => Boolean)): Boolean = seq.foldLeft(false)((a, b) => a || f(b))
  15.   def all(f: (T => Boolean)): Boolean = seq.foldLeft(true)((a, b) => a && f(b))
  16.  
  17. }
  18.  
  19. object ExtSeq {
  20.   implicit def toExtSeq[T](seq : Seq[T]) : ExtSeq[T] = new ExtSeq(seq)
  21. }
  22.  
  23. case class Complex (a: Double, b : Double) {
  24.   override val toString =
  25.     if (b == 0.0) s"$a"
  26.     else if (a == 0.0) s"${b}i"
  27.     else if (b > 0.0) s"${a}+${b}i"
  28.     else s"${a}${b}i"
  29.   def reciprocal : Complex = new Complex(a, -b)
  30.   def +(c : Complex) : Complex = new Complex(c.a + a, c.b + b)
  31.   def -(c : Complex) : Complex = new Complex(a - c.a, b - c.b)
  32.   def *(c : Complex) : Complex = new Complex(c.a * a - c.b * b, c.b * a + c.a + b)
  33.   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))
  34.   val abs = sqrt(a * a + b * b)
  35. }
  36.  
  37. object Main extends App {
  38.  
  39.   import TD1._
  40.   import scala.language.implicitConversions
  41.   val c = (new Complex(3, 4)) + (new Complex(1, 2))
  42.   println(c.toString)
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement