From f66fdac8b4aaf358f50bcdabc709399143c1cfe9 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Mon, 3 Mar 2025 17:04:55 +0100 Subject: [PATCH] added lesson 2 --- src/Lesson2/Curryfication.sc | 6 ++++++ src/Lesson2/SumInts.sc | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 src/Lesson2/Curryfication.sc create mode 100644 src/Lesson2/SumInts.sc diff --git a/src/Lesson2/Curryfication.sc b/src/Lesson2/Curryfication.sc new file mode 100644 index 0000000..7304110 --- /dev/null +++ b/src/Lesson2/Curryfication.sc @@ -0,0 +1,6 @@ +def mul(x: Int, y: Int): Int = x * y + +def mulCurry(x: Int) = (y :Int) => x * y + +def mulCurryScala(x: Int)(y: Int): Int = x * y + diff --git a/src/Lesson2/SumInts.sc b/src/Lesson2/SumInts.sc new file mode 100644 index 0000000..d431042 --- /dev/null +++ b/src/Lesson2/SumInts.sc @@ -0,0 +1,20 @@ +def sumInts(a: Int, b: Int): Double = + if (a > b) 0 else a + sumInts(a + 1, b) + +def sumCubes(a: Int, b: Int): Double = + if (a > b) 0 else a * a * a + sumCubes(a + 1, b) + +def sumRec(a: Int, b: Int): Double = + if (a > b) 0 else 1.0 / a + sumRec(a + 1, b) + +def mapReduce(f: Int => Double, a: Int, b: Int): Double = + if (a > b) 0 else f(a) + mapReduce(f, a + 1, b) + +sumInts(3, 5) +sumCubes(1, 3) +sumRec(1, 3) + +mapReduce(i => i, 3, 5) +mapReduce(i => i*i*i, 1, 3) +mapReduce(i => 1.0/i, 1, 3) +