-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy path2-doubly-mutable.scala
More file actions
71 lines (55 loc) · 1.71 KB
/
2-doubly-mutable.scala
File metadata and controls
71 lines (55 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* In case of double linked list we have to ignore some principles of functional programming
* and we should make some fields of Node class mutable
* In other case we will have to recreate all list after any change
*/
sealed trait DoubleLinked[+A] {
def ::[B >: A](value: B): DoubleLinked[B] = {
DoubleLinked.append(value, this)
}
def foreach[B >: A](f: B => Unit): Unit = {
DoubleLinked.foreach(this)(f)
}
def map[B](f: A => B): DoubleLinked[B] = {
DoubleLinked.map(this, DNil)(f)
}
}
case object DNil extends DoubleLinked[Nothing]
class DNode[A](var prev: DoubleLinked[A], val value: A, var next: DoubleLinked[A]) extends DoubleLinked[A]
object DoubleLinked {
def empty[A]: DoubleLinked[A] = DNil
def apply[A](values: A*): DoubleLinked[A] = values.foldLeft(DoubleLinked.empty[A])((list, item) => item :: list)
def append[A](value: A, list: DoubleLinked[A]): DoubleLinked[A] = {
val node = new DNode(DNil, value, list)
list match {
case DNil => DNil
case l: DNode[A] =>
l.prev = node
}
node
}
def foreach[A](list: DoubleLinked[A])(f: A => Unit): Unit = {
list match {
case DNil =>
case l: DNode[A] =>
f(l.value)
l.next.foreach(f)
}
}
def map[A, B](list: DoubleLinked[A], prev: DoubleLinked[B])(f: A => B): DoubleLinked[B] = {
list match {
case DNil => DNil
case l: DNode[A] =>
new DNode(prev, f(l.value), map(l.next, prev)(f))
}
}
}
object DoublyExample {
def main(args: Array[String]): Unit = {
val list2: DoubleLinked[Int] = 1 :: 2 :: 3 :: DNil
list2.foreach(item => print(s"$item\t"))
println()
list2.map(_ + 1).foreach(item => print(s"$item\t"))
println()
}
}