Skip to content

Commit 98cdce2

Browse files
committed
added some scala examples
1 parent 5698542 commit 98cdce2

File tree

6 files changed

+166
-0
lines changed

6 files changed

+166
-0
lines changed

Scala/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea/
2+
target/
3+
*.iml

Scala/1-callback.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
object Callback {
2+
3+
def func(data: Option[String], callback: (Either[Exception, String]) => Int): Int =
4+
data map {d =>
5+
callback(Right(s"Data: $d"))
6+
} getOrElse {
7+
callback(Left(new Exception("Exception occurred!")))
8+
}
9+
10+
def simpleCallback(input: Either[Exception, String]): Int =
11+
input map { s: String =>
12+
println(s)
13+
1 // ok, status 1
14+
} getOrElse {
15+
println(s"Data is not present!\n${input.left.get.getMessage}")
16+
-1 // NOT ok, status -1
17+
}
18+
19+
def main(args: Array[String]): Unit = {
20+
println(s"None status = ${func(None, simpleCallback)}")
21+
22+
println(s"Some status = ${func(Some("123"), simpleCallback)}")
23+
}
24+
}

Scala/2-closure.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
object Closure {
2+
def func(a: String): String => Unit = {
3+
val b = "Closure variable"
4+
5+
(c: String) =>
6+
println(s"$a\t$b\t$c")
7+
}
8+
9+
def main(args: Array[String]): Unit = {
10+
val f1 = func("Number 1")
11+
f1("Number 2")
12+
13+
val f2 = func("Parameter X")
14+
f2("Parameter Y")
15+
}
16+
}

Scala/3-chain.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
object Chain {
2+
3+
def sum(a: Int) =
4+
(b: Int) =>
5+
(c: Int) =>
6+
a + b + c
7+
8+
def main(args: Array[String]): Unit = {
9+
val f1 = sum(1)(1)
10+
val s1 = f1(1)
11+
val s2 = f1(2)
12+
13+
println(s"$s1\t$s2")
14+
}
15+
}

Scala/4-cache.scala

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
object Cache {
2+
3+
// acts like functor
4+
def cached() = {
5+
println("Generate cache")
6+
var cache: Int Map String = Map.empty[Int, String]
7+
8+
(a: Int) =>
9+
cache.get(a) map { value =>
10+
println("Found in cache")
11+
value
12+
} getOrElse {
13+
println("Not found in cache, calculating and saving")
14+
val res = a -> s"value $a"
15+
cache += res
16+
res
17+
}
18+
}
19+
20+
def main(args: Array[String]): Unit = {
21+
val f1 = cached()
22+
f1(1)
23+
f1(2)
24+
f1(1)
25+
f1(2)
26+
27+
val f2 = cached()
28+
f2(1)
29+
f2(2)
30+
f2(1)
31+
f2(2)
32+
}
33+
}

Scala/5-complex.scala

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Some more complex examples of higher order functions usage
3+
* there are `sum` and `product` functions with regular and tail recursive examples
4+
* and some generalized functions which can be used for creating `sum` and `product`
5+
*/
6+
7+
object ComplexExample {
8+
9+
def sum(f: (Int) => Int)(a: Int, b: Int): Int =
10+
if (a > b) 0
11+
else f(a) + sum(f)(a + 1, b)
12+
13+
14+
def product(f: (Int) => Int)(a: Int, b: Int): Int =
15+
if (a > b) 1
16+
else f(a) * product(f)(a + 1, b)
17+
18+
// tail recursive sum
19+
def sum1(f: (Int) => Int)(a: Int, b: Int): Int = {
20+
def loop(buf: Int)(curr: Int): Int =
21+
if (curr > b) buf
22+
else loop(buf + f(curr))(curr + 1)
23+
24+
loop(0)(a)
25+
}
26+
27+
// tail recursive product
28+
def product1(f: (Int) => Int)(a: Int, b: Int): Int = {
29+
def loop(buf: Int)(curr: Int): Int =
30+
if (curr > b) buf
31+
else loop(buf * f(curr))(curr + 1)
32+
33+
loop(1)(a)
34+
}
35+
36+
def generalized(zero: Int)(combine: (Int, Int) => Int): (Int => Int) => (Int, Int) => Int = {
37+
def innerFunc(f: (Int) => Int)(a: Int, b: Int): Int = {
38+
if (a > b) zero
39+
else combine(f(a), innerFunc(f)(a + 1, b))
40+
}
41+
innerFunc
42+
}
43+
44+
// tail recursive generalized function
45+
def generalized1(zero: Int)(combine: (Int, Int) => Int): (Int => Int) => (Int, Int) => Int = {
46+
(f: (Int) => Int) => (a: Int, b: Int) => {
47+
def loop(buf: Int)(curr: Int): Int =
48+
if (curr > b) buf
49+
else loop(combine(buf, f(curr)))(curr + 1)
50+
51+
loop(zero)(a)
52+
}
53+
}
54+
55+
def main(args: Array[String]): Unit = {
56+
val identity = (x: Int) => x
57+
val square = (x: Int) => x * x
58+
59+
val generalizedSum = generalized(0)(_ + _)
60+
val generalizedSum1 = generalized1(0)(_ + _)
61+
println("sum of squares")
62+
println(s"sum(x => x * x)(1, 5) = ${sum(square)(1, 5)}")
63+
println(s"sum1(x => x * x)(1, 5) = ${sum1(square)(1, 5)}")
64+
println(s"generalizedSum(x => x * x)(1, 5) = ${generalizedSum(square)(1, 5)}")
65+
println(s"generalizedSum1(x => x * x)(1, 5) = ${generalizedSum1(square)(1, 5)}")
66+
67+
val generalizedProduct = generalized(1)(_ * _)
68+
val generalizedProduct1 = generalized1(1)(_ * _)
69+
println("factorial")
70+
println(s"product(x => x)(1, 5) = ${product(identity)(1, 5)}")
71+
println(s"product1(x => x)(1, 5) = ${product1(identity)(1, 5)}")
72+
println(s"generalizedProduct(x => x)(1, 5) = ${generalizedProduct(identity)(1, 5)}")
73+
println(s"generalizedProduct1(x => x)(1, 5) = ${generalizedProduct1(identity)(1, 5)}")
74+
}
75+
}

0 commit comments

Comments
 (0)