-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Source.mapStateful and Source.mapStatefulConcat combinators #15
Merged
+248
−1
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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" => | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think here we already have the desired error handling, so no changes should be necessary :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're still checking the result of
c.send
below inresult.iterator.map(c.send).forall(_.isValue)
, although we assume thatsend
should not fail since we controlc
, so we should probably always returntrue
after sending, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right, though this at worst is dead code - not that there's a possiblity of incorrect behavior. We can change this though: