-
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.
Merge pull request #15 from softwaremill/source-stateful-map
Add Source.mapStateful and Source.mapStatefulConcat combinators
- Loading branch information
Showing
3 changed files
with
247 additions
and
0 deletions.
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
79 changes: 79 additions & 0 deletions
79
core/src/test/scala/ox/channels/SourceOpsMapStatefulConcatTest.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,79 @@ | ||
package ox.channels | ||
|
||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import ox.* | ||
|
||
class SourceOpsMapStatefulConcatTest extends AnyFlatSpec with Matchers { | ||
|
||
behavior of "Source.mapStatefulConcat" | ||
|
||
it should "deduplicate" in scoped { | ||
// given | ||
val c = Source.fromValues(1, 2, 2, 3, 2, 4, 3, 1, 5) | ||
|
||
// when | ||
val s = c.mapStatefulConcat(() => Set.empty[Int])((s, e) => (s + e, Option.unless(s.contains(e))(e))) | ||
|
||
// then | ||
s.toList shouldBe List(1, 2, 3, 4, 5) | ||
} | ||
|
||
it should "count consecutive" in scoped { | ||
// given | ||
val c = Source.fromValues("apple", "apple", "apple", "banana", "orange", "orange", "apple") | ||
|
||
// when | ||
val s = c.mapStatefulConcat(() => (Option.empty[String], 0))( | ||
{ case ((previous, count), e) => | ||
previous match | ||
case None => ((Some(e), 1), None) | ||
case Some(`e`) => ((previous, count + 1), None) | ||
case Some(_) => ((Some(e), 1), previous.map((_, count))) | ||
}, | ||
{ case (previous, count) => previous.map((_, count)) } | ||
) | ||
|
||
// then | ||
s.toList shouldBe List( | ||
("apple", 3), | ||
("banana", 1), | ||
("orange", 2), | ||
("apple", 1) | ||
) | ||
} | ||
|
||
it should "propagate errors in the mapping function" in scoped { | ||
// given | ||
val c = Source.fromValues("a", "b", "c") | ||
|
||
// when | ||
val s = c.mapStatefulConcat(() => 0) { (index, element) => | ||
if (index < 2) (index + 1, Some(element)) | ||
else throw new RuntimeException("boom") | ||
} | ||
|
||
// then | ||
s.receive() shouldBe "a" | ||
s.receive() shouldBe "b" | ||
s.receive() should matchPattern { | ||
case ChannelClosed.Error(Some(reason)) if reason.getMessage == "boom" => | ||
} | ||
} | ||
|
||
it should "propagate errors in the completion callback" in scoped { | ||
// given | ||
val c = Source.fromValues("a", "b", "c") | ||
|
||
// when | ||
val s = c.mapStatefulConcat(() => 0)((index, element) => (index + 1, Some(element)), _ => throw new RuntimeException("boom")) | ||
|
||
// then | ||
s.receive() shouldBe "a" | ||
s.receive() shouldBe "b" | ||
s.receive() shouldBe "c" | ||
s.receive() should matchPattern { | ||
case ChannelClosed.Error(Some(reason)) if reason.getMessage == "boom" => | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
core/src/test/scala/ox/channels/SourceOpsMapStatefulTest.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,60 @@ | ||
package ox.channels | ||
|
||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import ox.* | ||
|
||
class SourceOpsMapStatefulTest extends AnyFlatSpec with Matchers { | ||
|
||
behavior of "Source.mapStateful" | ||
|
||
it should "zip with index" in scoped { | ||
val c = Source.fromValues("a", "b", "c") | ||
|
||
val s = c.mapStateful(() => 0)((index, element) => (index + 1, (element, index))) | ||
|
||
s.toList shouldBe List(("a", 0), ("b", 1), ("c", 2)) | ||
} | ||
|
||
it should "calculate a running total" in scoped { | ||
val c = Source.fromValues(1, 2, 3, 4, 5) | ||
|
||
val s = c.mapStateful(() => 0)((sum, element) => (sum + element, sum), Some.apply) | ||
|
||
s.toList shouldBe List(0, 1, 3, 6, 10, 15) | ||
} | ||
|
||
it should "propagate errors in the mapping function" in scoped { | ||
// given | ||
val c = Source.fromValues("a", "b", "c") | ||
|
||
// when | ||
val s = c.mapStateful(() => 0) { (index, element) => | ||
if (index < 2) (index + 1, element) | ||
else throw new RuntimeException("boom") | ||
} | ||
|
||
// then | ||
s.receive() shouldBe "a" | ||
s.receive() shouldBe "b" | ||
s.receive() should matchPattern { | ||
case ChannelClosed.Error(Some(reason)) if reason.getMessage == "boom" => | ||
} | ||
} | ||
|
||
it should "propagate errors in the completion callback" in scoped { | ||
// given | ||
val c = Source.fromValues("a", "b", "c") | ||
|
||
// when | ||
val s = c.mapStateful(() => 0)((index, element) => (index + 1, element), _ => throw new RuntimeException("boom")) | ||
|
||
// then | ||
s.receive() shouldBe "a" | ||
s.receive() shouldBe "b" | ||
s.receive() shouldBe "c" | ||
s.receive() should matchPattern { | ||
case ChannelClosed.Error(Some(reason)) if reason.getMessage == "boom" => | ||
} | ||
} | ||
} |