summaryrefslogtreecommitdiffstats
path: root/Scala
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2013-04-03 22:18:48 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2016-11-10 18:03:22 +0100
commit476583c90a47cad62f992ab0b845ebf2897cb47d (patch)
treec3742b376e852cc34b3bcfe052fd7aa1acbc09f5 /Scala
parent545c03ac4981737594d7638b7a925d1881a74cdc (diff)
downloadcoursera-476583c90a47cad62f992ab0b845ebf2897cb47d.zip
coursera-476583c90a47cad62f992ab0b845ebf2897cb47d.tar.gz
Scala : cleanup, Scala : add sumCurry
Diffstat (limited to 'Scala')
-rw-r--r--Scala/sandbox/curry.scala30
-rw-r--r--Scala/sandbox/main.scala1
-rw-r--r--Scala/sandbox/recursion.scala11
-rw-r--r--Scala/sandbox/sqrt.scala4
-rw-r--r--Scala/sandbox/sum.scala14
5 files changed, 40 insertions, 20 deletions
diff --git a/Scala/sandbox/curry.scala b/Scala/sandbox/curry.scala
new file mode 100644
index 0000000..910de62
--- /dev/null
+++ b/Scala/sandbox/curry.scala
@@ -0,0 +1,30 @@
+
+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 sumInts = sumCurry(x => x)
+ def sumCubes = sumCurry(x => x * x)
+
+ def run = {
+ println("Curry")
+ println(sum(x => x * x, 3, 5))
+ println(sumInts(3, 5))
+ println(sumCubes(3, 5))
+ }
+
+}
diff --git a/Scala/sandbox/main.scala b/Scala/sandbox/main.scala
index e770ece..7a0b791 100644
--- a/Scala/sandbox/main.scala
+++ b/Scala/sandbox/main.scala
@@ -3,5 +3,6 @@ object Main extends App {
Sqrt.run
Recursion.run
+ Curry.run
}
diff --git a/Scala/sandbox/recursion.scala b/Scala/sandbox/recursion.scala
index e7c29a5..5488aea 100644
--- a/Scala/sandbox/recursion.scala
+++ b/Scala/sandbox/recursion.scala
@@ -8,15 +8,11 @@ object Recursion {
def gcd(a: Int, b: Int): Int =
if (b == 0) a else gcd(b, a % b)
- println(gcd(21, 56))
-
// 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)
- println(fact(4))
-
// tail recursive version of factorial
def factr(n: Int): Int = {
def factIter(acc: Int, n: Int): Int =
@@ -24,6 +20,11 @@ object Recursion {
factIter(1, n)
}
- def run = println(factr(4))
+ 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
index 2544425..c76ee00 100644
--- a/Scala/sandbox/sqrt.scala
+++ b/Scala/sandbox/sqrt.scala
@@ -18,11 +18,13 @@ object Sqrt {
sqrtIter(1.0)
}
- def run =
+ 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/sum.scala b/Scala/sandbox/sum.scala
deleted file mode 100644
index a09f26b..0000000
--- a/Scala/sandbox/sum.scala
+++ /dev/null
@@ -1,14 +0,0 @@
-
-object Sum {
-
- 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)
- }
-
- def run =
- println(sum(x => x * x, 3, 5))
-
-}