From b244be6e7dc68ecd92c9c330ec33e494d6a323d5 Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Sun, 10 Dec 2023 21:45:56 +0100 Subject: [PATCH] Rename source.map => transformValuesWith and add warning --- shared/src/main/scala/async/Async.scala | 9 ++++++--- shared/src/test/scala/ChannelBehavior.scala | 2 +- shared/src/test/scala/SourceBehavior.scala | 8 ++++---- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/shared/src/main/scala/async/Async.scala b/shared/src/main/scala/async/Async.scala index a3c0c720..8b86eea5 100644 --- a/shared/src/main/scala/async/Async.scala +++ b/shared/src/main/scala/async/Async.scala @@ -153,8 +153,11 @@ object Async: end values extension [T](src: Source[T]) - /** Pass on data transformed by `f` */ - def map[U](f: T => U) = + /** Create a new source that requires the original source to run the given transformation function on every value + * received. Note that [[f]] is **always** run on the computation that produces the values from the original + * source, so this is very likely to run **sequentially** and be a performance bottleneck. + */ + def transformValuesWith[U](f: T => U) = new Source[U]: selfSrc => def transform(k: Listener[U]) = @@ -267,5 +270,5 @@ object Async: * continuation. */ def either[T1, T2](src1: Source[T1], src2: Source[T2]): Source[Either[T1, T2]] = - race(src1.map(Left(_)), src2.map(Right(_))) + race(src1.transformValuesWith(Left(_)), src2.transformValuesWith(Right(_))) end Async diff --git a/shared/src/test/scala/ChannelBehavior.scala b/shared/src/test/scala/ChannelBehavior.scala index f4e13b84..e2e565c1 100644 --- a/shared/src/test/scala/ChannelBehavior.scala +++ b/shared/src/test/scala/ChannelBehavior.scala @@ -254,7 +254,7 @@ class ChannelBehavior extends munit.FunSuite { } val race = Async.race( (0 until 100).map(i => - Async.race((10 * i until 10 * i + 10).map(idx => channels(idx).readSource.map(_.right.get))*) + Async.race((10 * i until 10 * i + 10).map(idx => channels(idx).readSource.transformValuesWith(_.right.get))*) )* ) var sum = 0 diff --git a/shared/src/test/scala/SourceBehavior.scala b/shared/src/test/scala/SourceBehavior.scala index a361fb25..58f60a4f 100644 --- a/shared/src/test/scala/SourceBehavior.scala +++ b/shared/src/test/scala/SourceBehavior.scala @@ -127,12 +127,12 @@ class SourceBehavior extends munit.FunSuite { assertEquals(bRan, true) } - test("map") { + test("transform values with") { Async.blocking: val f: Future[Int] = Future { 10 } - assertEquals(f.map({ case Success(i) => i + 1 }).awaitResult, 11) + assertEquals(f.transformValuesWith({ case Success(i) => i + 1 }).awaitResult, 11) val g: Future[Int] = Future.now(Failure(AssertionError(1123))) - assertEquals(g.map({ case Failure(_) => 17 }).awaitResult, 17) + assertEquals(g.transformValuesWith({ case Failure(_) => 17 }).awaitResult, 17) } test("all listeners in chain fire") { @@ -143,7 +143,7 @@ class SourceBehavior extends munit.FunSuite { sleep(50) 10 } - val g = f.map(identity) + val g = f.transformValuesWith(identity) f.onComplete(Listener.acceptingListener { (_, _) => aRan.complete(Success(())) }) g.onComplete(Listener.acceptingListener { (_, _) => bRan.complete(Success(())) }) assertEquals(aRan.future.poll(), None)