# HG changeset patch # User fahadausaf # Date 1414341748 0 # Node ID fa92e8f089a2d25ab65dea854228b26358fea5b9 # Parent 238c98db60579d7f2af867026c050c9de11652f8 no message diff -r 238c98db6057 -r fa92e8f089a2 Fahad/Scala/Chapter5.sc --- a/Fahad/Scala/Chapter5.sc Sun Oct 26 11:17:40 2014 +0000 +++ b/Fahad/Scala/Chapter5.sc Sun Oct 26 16:42:28 2014 +0000 @@ -28,28 +28,79 @@ sumPowersOfTwo(2, 4) */ + /* def sum(f: Int => Int, a: Int, b: Int): Int = - if (a > b) 0 else f(a) + sum(f, a + 1, b) //> sum: (f: Int => Int, a: Int, b: Int)Int + if (a > b) 0 else f(a) + sum(f, a + 1, b) //def sumInts(a: Int, b: Int): Int = sum(id, a, b) //def sumInts(a: Int, b: Int): Int = sum((x: Int) => x, a, b) def sumInts(a: Int, b: Int): Int = sum(x => x, a, b) - //> sumInts: (a: Int, b: Int)Int //def sumSquares(a: Int, b: Int): Int = sum(square, a, b) //def sumSquares(a: Int, b: Int): Int = sum((x: Int) => x * x, a, b) def sumSquares(a: Int, b: Int): Int = sum(x => x * x, a, b) - //> sumSquares: (a: Int, b: Int)Int def sumPowersOfTwo(a: Int, b: Int): Int = sum(powerOfTwo, a, b) - //> sumPowersOfTwo: (a: Int, b: Int)Int - def id(x: Int): Int = x //> id: (x: Int)Int - def square(x: Int): Int = x * x //> square: (x: Int)Int + def id(x: Int): Int = x + def square(x: Int): Int = x * x + */ def powerOfTwo(x: Int): Int = if (x == 0) 1 else 2 * powerOfTwo(x - 1) //> powerOfTwo: (x: Int)Int - + + // 5.2: Currying - - - + + def sum(f: Int => Int): (Int, Int) => Int = { + def sumF(a: Int, b: Int): Int = + if (a > b) 0 else f(a) + sumF(a + 1, b) + sumF + } //> sum: (f: Int => Int)(Int, Int) => Int + + def sumInts = sum(x => x) //> sumInts: => (Int, Int) => Int + def sumSquares = sum(x => x * x) //> sumSquares: => (Int, Int) => Int + def sumPowersOfTwo = sum(powerOfTwo) //> sumPowersOfTwo: => (Int, Int) => Int + sumSquares(1,10) + sumPowersOfTwo(10,20) //> res0: Int = 2096513 + sum(x => x * x)(2,4) //> res1: Int = 29 + + // 5.3: Finding Fixed Points of Functions + + val tolerance = 0.001 //> tolerance : Double = 0.001 + def isCloseEnough(x: Double, y: Double) = Math.abs((x-y) / x) < tolerance + //> isCloseEnough: (x: Double, y: Double)Boolean + def fixedPoint(f: Double => Double)(firstGuess: Double) = { + def iterate(guess: Double): Double = { + val next = f(guess) + if (isCloseEnough(guess, next)) next + else iterate(next) + } + iterate(firstGuess) + } //> fixedPoint: (f: Double => Double)(firstGuess: Double)Double + + + + + + + + + + + + + + + + + + + + + + + + + + + + } \ No newline at end of file diff -r 238c98db6057 -r fa92e8f089a2 Fahad/Scala/Chapter6.sc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Fahad/Scala/Chapter6.sc Sun Oct 26 16:42:28 2014 +0000 @@ -0,0 +1,68 @@ +package greeter + +object Chapter6 { + println("Classes and Objects") //> Classes and Objects + + class Rational(n: Int, d: Int) { + private def gcd(x: Int, y: Int): Int = { + if (x == 0) y + else if (x < 0) gcd(-x, y) + else if (y < 0) -gcd(x, -y) + else gcd(y % x, x) + } + private val g = gcd(n, d) + + val numer: Int = n / g + val denom: Int = d / g + def +(that: Rational) = new Rational(numer * that.denom + that.numer * denom, denom * that.denom) + def -(that: Rational) = new Rational(numer * that.denom - that.numer * denom, denom * that.denom) + def *(that: Rational) = new Rational(numer * that.numer, denom * that.denom) + def /(that: Rational) = new Rational(numer * that.denom, denom * that.numer) + + //Inheritance and Overriding + override def toString = "" + numer + "/" + denom + //Parameterless Methods + def square = new Rational(numer * numer, denom * denom) + } + //Inheritance and Overriding + var i = 1 //> i : Int = 1 + var x = new Rational(0, 1) //> x : greeter.Chapter6.Rational = 0/1 + while (i <= 10) { + x += new Rational(1, i) + i += 1 + } + println("" + x.numer + "/" + x.denom) //> 7381/2520 + //Parameterless Methods + val r = new Rational(3, 4) //> r : greeter.Chapter6.Rational = 3/4 + println(r.square) //> 9/16 + + //Abstract Classes + abstract class IntSet { + def incl(x: Int): IntSet + def contains(x: Int): Boolean + } + + //Triats + trait IntSett { + def incl(x: Int): IntSet + def contains(x: Int): Boolean + } + + //Implementing abstract class + class EmptySet extends IntSet { + def contains(x: Int): Boolean = false + def incl(x: Int): IntSet = new NonEmptySet(x, new EmptySet, new EmptySet) + } + + class NonEmptySet(elem: Int, left: IntSet, right: IntSet) extends IntSet { + def contains(x: Int): Boolean = + if (x < elem) left contains x + else if (x > elem) right contains x + else true + def incl(x: Int): IntSet = + if (x < elem) new NonEmptySet(elem, left incl x, right) + else if (x > elem) new NonEmptySet(elem, left, right incl x) + else this + } + +} \ No newline at end of file