-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce
intersperse
function
The intersperse function behaves as `List.mkString` e.g.: * called with an `inject` element only it produces Source.fromValues("f", "b").intersperse(", ") // ("f", ", ", "b") * called with `start`, `inject` and `end` it produces Source.fromValues("f", "b").intersperse("[", ", ", "]") // ("[", "f", ", ", "b", "]")
- Loading branch information
1 parent
161557a
commit 04e57f4
Showing
2 changed files
with
159 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
core/src/test/scala/ox/channels/SourceOpsIntersperseTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package ox.channels | ||
|
||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import ox.* | ||
|
||
class SourceOpsIntersperseTest extends AnyFlatSpec with Matchers { | ||
behavior of "Source.intersperse" | ||
|
||
it should "intersperse with inject only over an empty source" in { | ||
scoped { | ||
val c = Channel[String]() | ||
fork { | ||
c.done() | ||
} | ||
|
||
val s = c.intersperse(", ") | ||
|
||
s.receive() shouldBe ChannelClosed.Done | ||
} | ||
} | ||
|
||
it should "intersperse with inject only over a source with one element" in { | ||
scoped { | ||
val c = Channel[String]() | ||
fork { | ||
c.send("foo") | ||
c.done() | ||
} | ||
|
||
val s = c.intersperse(", ") | ||
|
||
s.receive() shouldBe "foo" | ||
s.receive() shouldBe ChannelClosed.Done | ||
} | ||
} | ||
|
||
it should "intersperse with inject only over a source with multiple elements" in { | ||
scoped { | ||
val c = Channel[String]() | ||
fork { | ||
c.send("foo") | ||
c.send("bar") | ||
c.done() | ||
} | ||
|
||
val s = c.intersperse(", ") | ||
|
||
s.receive() shouldBe "foo" | ||
s.receive() shouldBe ", " | ||
s.receive() shouldBe "bar" | ||
s.receive() shouldBe ChannelClosed.Done | ||
} | ||
} | ||
|
||
it should "intersperse with start, inject and end over an empty source" in { | ||
scoped { | ||
val c = Channel[String]() | ||
fork { | ||
c.done() | ||
} | ||
|
||
val s = c.intersperse("[", ", ", "]") | ||
|
||
s.receive() shouldBe "[" | ||
s.receive() shouldBe "]" | ||
s.receive() shouldBe ChannelClosed.Done | ||
} | ||
} | ||
|
||
it should "intersperse with start, inject and end over a source with one element" in { | ||
scoped { | ||
val c = Channel[String]() | ||
fork { | ||
c.send("foo") | ||
c.done() | ||
} | ||
|
||
val s = c.intersperse("[", ", ", "]") | ||
|
||
s.receive() shouldBe "[" | ||
s.receive() shouldBe "foo" | ||
s.receive() shouldBe "]" | ||
s.receive() shouldBe ChannelClosed.Done | ||
} | ||
} | ||
|
||
it should "intersperse with start, inject and end over a source with multiple elements" in { | ||
scoped { | ||
val c = Channel[String]() | ||
fork { | ||
c.send("foo") | ||
c.send("bar") | ||
c.done() | ||
} | ||
|
||
val s = c.intersperse("[", ", ", "]") | ||
|
||
s.receive() shouldBe "[" | ||
s.receive() shouldBe "foo" | ||
s.receive() shouldBe ", " | ||
s.receive() shouldBe "bar" | ||
s.receive() shouldBe "]" | ||
s.receive() shouldBe ChannelClosed.Done | ||
} | ||
} | ||
} |