From dadc875c05c830d15ee0aebea70aefaea6d3e55e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Wed, 3 Apr 2013 23:41:42 +0200 Subject: Scala : rename files in sandbox --- Scala/sandbox/0-main.scala | 8 ++++++ Scala/sandbox/1-recursion.scala | 30 ++++++++++++++++++++++ Scala/sandbox/1-sqrt.scala | 30 ++++++++++++++++++++++ Scala/sandbox/2-curry.scala | 56 +++++++++++++++++++++++++++++++++++++++++ Scala/sandbox/curry.scala | 56 ----------------------------------------- Scala/sandbox/main.scala | 8 ------ Scala/sandbox/recursion.scala | 30 ---------------------- Scala/sandbox/sqrt.scala | 30 ---------------------- 8 files changed, 124 insertions(+), 124 deletions(-) create mode 100644 Scala/sandbox/0-main.scala create mode 100644 Scala/sandbox/1-recursion.scala create mode 100644 Scala/sandbox/1-sqrt.scala create mode 100644 Scala/sandbox/2-curry.scala delete mode 100644 Scala/sandbox/curry.scala delete mode 100644 Scala/sandbox/main.scala delete mode 100644 Scala/sandbox/recursion.scala delete mode 100644 Scala/sandbox/sqrt.scala diff --git a/Scala/sandbox/0-main.scala b/Scala/sandbox/0-main.scala new file mode 100644 index 0000000..7a0b791 --- /dev/null +++ b/Scala/sandbox/0-main.scala @@ -0,0 +1,8 @@ + +object Main extends App { + + Sqrt.run + Recursion.run + Curry.run + +} diff --git a/Scala/sandbox/1-recursion.scala b/Scala/sandbox/1-recursion.scala new file mode 100644 index 0000000..5488aea --- /dev/null +++ b/Scala/sandbox/1-recursion.scala @@ -0,0 +1,30 @@ + +object Recursion { + + // Tail recursion - tail calls + // the function calls itself (or another one) as last action + // -> the function's stack frame can be reused! + // -> this is as efficient as a loop + def gcd(a: Int, b: Int): Int = + if (b == 0) a else gcd(b, a % b) + + // there is still the multiplication to be performed + // -> the stack frame can't be reused + def fact(n: Int): Int = + if (n == 0) 1 else n * fact(n - 1) + + // tail recursive version of factorial + def factr(n: Int): Int = { + def factIter(acc: Int, n: Int): Int = + if (n == 0) acc else factIter(acc * n, n - 1) + factIter(1, n) + } + + def run = { + println("Recursion") + println(gcd(21, 56)) + println(fact(4)) + println(factr(4)) + } + +} diff --git a/Scala/sandbox/1-sqrt.scala b/Scala/sandbox/1-sqrt.scala new file mode 100644 index 0000000..c76ee00 --- /dev/null +++ b/Scala/sandbox/1-sqrt.scala @@ -0,0 +1,30 @@ + +object Sqrt { + + def abs(x:Double) = if (x < 0) -x else x + + def sqrt(x: Double) = { + + def sqrtIter(guess: Double): Double = + if (isGoodEnough(guess)) guess + else sqrtIter(improve(guess)) + + def isGoodEnough(guess: Double) = + abs(guess * guess - x ) /x < 0.001 + + def improve(guess: Double) = + (guess + x / guess) / 2 + + sqrtIter(1.0) + } + + def run = { + println("Sqrt") + println(sqrt(2)) + println(sqrt(0.001)) + println(sqrt(0.1e-20)) + println(sqrt(1.0e20)) + println(sqrt(1.0e50)) + } + +} diff --git a/Scala/sandbox/2-curry.scala b/Scala/sandbox/2-curry.scala new file mode 100644 index 0000000..adc7e75 --- /dev/null +++ b/Scala/sandbox/2-curry.scala @@ -0,0 +1,56 @@ + +object Curry { + + // high order function + def sum(f: Int => Int, a: Int, b: Int) = { + def loop(a: Int, acc: Int): Int = + if (a > b) acc + else loop(a + 1, f(a) + acc) + loop(a, 0) + } + + // curry version: this function return an (Int, Int) => Int function + def sumCurry(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 + } + + def cube (x: Int): Int = x * x * x + + def sumInts = sumCurry(x => x) + def sumCubes = sumCurry(x => x * x * x) + + // syntax shortcut + def sumCurryShort(f: Int => Int)(a: Int, b: Int): Int = + if (a > b) 0 else f(a) + sumCurryShort(f)(a + 1, b) + + // add _ if you want to treat it as a partially applied function + def sumCubesShort = sumCurryShort(cube)_ + + // product + def product(f: Int => Int)(a: Int, b: Int): Int = + if (a > b) 1 else f(a) * product(f)(a + 1, b) + + def fact(n: Int) = product(x => x)(1, n) + + // add unit value and combining function => sum and product generalisation + def mapReduce(f: Int => Int, c: (Int, Int) => Int, u: Int )(a: Int, b: Int): Int = + if (a > b) u else c(f(a), mapReduce(f, c, u)(a + 1, b)) + + def run = { + println("Curry") + println(sum(x => x * x, 3, 5)) + println(sumInts(3, 5)) + println(sumCurry(cube)(3, 5)) + println(sumCubes(3, 5)) + println(sumCurryShort(cube)(3, 5)) + println(sumCubesShort(3, 5)) + println(product(x => x * x)(3, 4)) + println(fact(4)) + println(mapReduce(x => x * x, (x, y)=> x + y, 0)(3, 5)) + println(mapReduce(x => x * x, (x, y)=> x * y, 1)(3, 4)) + } + +} diff --git a/Scala/sandbox/curry.scala b/Scala/sandbox/curry.scala deleted file mode 100644 index adc7e75..0000000 --- a/Scala/sandbox/curry.scala +++ /dev/null @@ -1,56 +0,0 @@ - -object Curry { - - // high order function - def sum(f: Int => Int, a: Int, b: Int) = { - def loop(a: Int, acc: Int): Int = - if (a > b) acc - else loop(a + 1, f(a) + acc) - loop(a, 0) - } - - // curry version: this function return an (Int, Int) => Int function - def sumCurry(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 - } - - def cube (x: Int): Int = x * x * x - - def sumInts = sumCurry(x => x) - def sumCubes = sumCurry(x => x * x * x) - - // syntax shortcut - def sumCurryShort(f: Int => Int)(a: Int, b: Int): Int = - if (a > b) 0 else f(a) + sumCurryShort(f)(a + 1, b) - - // add _ if you want to treat it as a partially applied function - def sumCubesShort = sumCurryShort(cube)_ - - // product - def product(f: Int => Int)(a: Int, b: Int): Int = - if (a > b) 1 else f(a) * product(f)(a + 1, b) - - def fact(n: Int) = product(x => x)(1, n) - - // add unit value and combining function => sum and product generalisation - def mapReduce(f: Int => Int, c: (Int, Int) => Int, u: Int )(a: Int, b: Int): Int = - if (a > b) u else c(f(a), mapReduce(f, c, u)(a + 1, b)) - - def run = { - println("Curry") - println(sum(x => x * x, 3, 5)) - println(sumInts(3, 5)) - println(sumCurry(cube)(3, 5)) - println(sumCubes(3, 5)) - println(sumCurryShort(cube)(3, 5)) - println(sumCubesShort(3, 5)) - println(product(x => x * x)(3, 4)) - println(fact(4)) - println(mapReduce(x => x * x, (x, y)=> x + y, 0)(3, 5)) - println(mapReduce(x => x * x, (x, y)=> x * y, 1)(3, 4)) - } - -} diff --git a/Scala/sandbox/main.scala b/Scala/sandbox/main.scala deleted file mode 100644 index 7a0b791..0000000 --- a/Scala/sandbox/main.scala +++ /dev/null @@ -1,8 +0,0 @@ - -object Main extends App { - - Sqrt.run - Recursion.run - Curry.run - -} diff --git a/Scala/sandbox/recursion.scala b/Scala/sandbox/recursion.scala deleted file mode 100644 index 5488aea..0000000 --- a/Scala/sandbox/recursion.scala +++ /dev/null @@ -1,30 +0,0 @@ - -object Recursion { - - // Tail recursion - tail calls - // the function calls itself (or another one) as last action - // -> the function's stack frame can be reused! - // -> this is as efficient as a loop - def gcd(a: Int, b: Int): Int = - if (b == 0) a else gcd(b, a % b) - - // there is still the multiplication to be performed - // -> the stack frame can't be reused - def fact(n: Int): Int = - if (n == 0) 1 else n * fact(n - 1) - - // tail recursive version of factorial - def factr(n: Int): Int = { - def factIter(acc: Int, n: Int): Int = - if (n == 0) acc else factIter(acc * n, n - 1) - factIter(1, n) - } - - def run = { - println("Recursion") - println(gcd(21, 56)) - println(fact(4)) - println(factr(4)) - } - -} diff --git a/Scala/sandbox/sqrt.scala b/Scala/sandbox/sqrt.scala deleted file mode 100644 index c76ee00..0000000 --- a/Scala/sandbox/sqrt.scala +++ /dev/null @@ -1,30 +0,0 @@ - -object Sqrt { - - def abs(x:Double) = if (x < 0) -x else x - - def sqrt(x: Double) = { - - def sqrtIter(guess: Double): Double = - if (isGoodEnough(guess)) guess - else sqrtIter(improve(guess)) - - def isGoodEnough(guess: Double) = - abs(guess * guess - x ) /x < 0.001 - - def improve(guess: Double) = - (guess + x / guess) / 2 - - sqrtIter(1.0) - } - - def run = { - println("Sqrt") - println(sqrt(2)) - println(sqrt(0.001)) - println(sqrt(0.1e-20)) - println(sqrt(1.0e20)) - println(sqrt(1.0e50)) - } - -} -- cgit v1.1-2-g2b99