Skip to content

Commit 1de8a17

Browse files
committed
#7 Add toList(), make foreach() and fornew() correspond to the standard library convention
1 parent 5d76423 commit 1de8a17

File tree

6 files changed

+47
-9
lines changed

6 files changed

+47
-9
lines changed

src/main/scala/com/github/yruslan/channel/Channel.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ abstract class Channel[T] extends ReadChannel[T] with WriteChannel[T] {
7777
}
7878
}
7979

80-
final override def fornew(f: T => Unit): Unit = {
80+
final override def fornew[U](f: T => U): Unit = {
8181
val valueOpt = tryRecv()
8282
valueOpt.foreach(v => f(v))
8383
}
8484

85-
final override def foreach(f: T => Unit): Unit = {
85+
final override def foreach[U](f: T => U): Unit = {
8686
while (true) {
8787
lock.lock()
8888
readers += 1

src/main/scala/com/github/yruslan/channel/ChannelDecoratorFilter.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class ChannelDecoratorFilter[T](inputChannel: ReadChannel[T], pred: T => Boolean
8181

8282
override def recver(action: T => Unit): Selector = inputChannel.recver(t => if (pred(t)) action(t))
8383

84-
override def fornew(action: T => Unit): Unit = inputChannel.fornew(t => if (pred(t)) action(t))
84+
override def fornew[U](action: T => U): Unit = inputChannel.fornew(t => if (pred(t)) action(t))
8585

86-
override def foreach(action: T => Unit): Unit = inputChannel.foreach(t => if (pred(t)) action(t))
86+
override def foreach[U](action: T => U): Unit = inputChannel.foreach(t => if (pred(t)) action(t))
8787
}

src/main/scala/com/github/yruslan/channel/ChannelDecoratorMap.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class ChannelDecoratorMap[T, U](inputChannel: ReadChannel[T], f: T => U) extends
3838

3939
override def recver(action: U => Unit): Selector = inputChannel.recver(t => action(f(t)))
4040

41-
override def fornew(action: U => Unit): Unit = inputChannel.fornew(t => action(f(t)))
41+
override def fornew[K](action: U => K): Unit = inputChannel.fornew(t => action(f(t)))
4242

43-
override def foreach(action: U => Unit): Unit = inputChannel.foreach(t => action(f(t)))
43+
override def foreach[K](action: U => K): Unit = inputChannel.foreach(t => action(f(t)))
4444
}

src/main/scala/com/github/yruslan/channel/ReadChannel.scala

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ package com.github.yruslan.channel
2828

2929
import com.github.yruslan.channel.impl.Selector
3030

31+
import scala.collection.mutable.ListBuffer
3132
import scala.concurrent.duration.Duration
3233

3334
trait ReadChannel[T] extends ChannelLike {
@@ -37,9 +38,15 @@ trait ReadChannel[T] extends ChannelLike {
3738

3839
def recver(action: T => Unit): Selector
3940

40-
def fornew(f: T => Unit): Unit
41-
def foreach(f: T => Unit): Unit
41+
def fornew[U](f: T => U): Unit
42+
def foreach[U](f: T => U): Unit
4243

4344
def map[U](f: T => U): ReadChannel[U] = new ChannelDecoratorMap[T, U](this, f)
4445
def filter(f: T => Boolean): ReadChannel[T] = new ChannelDecoratorFilter[T](this, f)
46+
47+
def toList: List[T] = {
48+
val lst = new ListBuffer[T]
49+
foreach(v => lst += v)
50+
lst.toList
51+
}
4552
}

src/main/scala/com/github/yruslan/channel/impl/SimpleLinkedList.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class SimpleLinkedList[T] {
108108
count = 0
109109
}
110110

111-
def foreach(f: T => Unit): Unit = this.synchronized {
111+
def foreach[U](f: T => U): Unit = this.synchronized {
112112
var p = first
113113
while (p != null) {
114114
f(p.el)

src/test/scala/com/github/yruslan/channel/ChannelSuite.scala

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,38 @@ class ChannelSuite extends AnyWordSpec with BeforeAndAfterAll {
866866
}
867867
}
868868
}
869+
}
870+
871+
"toList" should {
872+
"covert a channel to a list" in {
873+
val ch1 = Channel.make[Int](3)
874+
875+
ch1.send(1)
876+
ch1.send(2)
877+
ch1.send(3)
878+
ch1.close()
879+
880+
val lst = ch1.toList
881+
882+
assert(lst == List(1, 2, 3))
883+
}
869884

885+
"convert a mapped filtered channel to a list" in {
886+
val ch1 = Channel.make[Int](3)
887+
888+
val ch2 = ch1
889+
.map(v => v * 2)
890+
.filter(v => v != 4)
891+
892+
ch1.send(1)
893+
ch1.send(2)
894+
ch1.send(3)
895+
ch1.close()
896+
897+
val lst = ch2.toList
898+
899+
assert(lst == List(2, 6))
900+
}
870901
}
871902

872903
"master/worker model" should {

0 commit comments

Comments
 (0)