From ec863d35ccca82ace00d762dbca1568ccac1992a Mon Sep 17 00:00:00 2001 From: zihang Date: Thu, 26 Dec 2024 10:21:15 +0800 Subject: [PATCH 1/2] fix(iter): tap --- builtin/iter.mbt | 11 ++++++----- builtin/iter_test.mbt | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/builtin/iter.mbt b/builtin/iter.mbt index d550f1d60..445461a6a 100644 --- a/builtin/iter.mbt +++ b/builtin/iter.mbt @@ -474,12 +474,13 @@ pub fn flat_map[T, R](self : Iter[T], f : (T) -> Iter[R]) -> Iter[R] { /// # Returns /// /// The same iterator. -/// -/// @alert deprecated "use cascade operator [..] instead" -/// @coverage.skip pub fn tap[T](self : Iter[T], f : (T) -> Unit) -> Iter[T] { - self.each(f) - self + self.map(fn { + a => { + f(a) + a + } + }) } ///| diff --git a/builtin/iter_test.mbt b/builtin/iter_test.mbt index 881864386..a51e8c9ba 100644 --- a/builtin/iter_test.mbt +++ b/builtin/iter_test.mbt @@ -269,8 +269,8 @@ test "find_first2" { test "tap" { let iter = test_from_array(['1', '2', '3', '4', '5']) let exb = StringBuilder::new(size_hint=0) - iter..each(fn { x => exb.write_char(x) }).each(fn { x => exb.write_char(x) }) - inspect!(exb, content="1234512345") + iter.tap(fn { x => exb.write_char(x) }).each(fn { x => exb.write_char(x) }) + inspect!(exb, content="1122334455") } test "prepend" { From 072df18b6e78dc2b1e824049d7fe5ed05d6a6ccd Mon Sep 17 00:00:00 2001 From: zihang Date: Thu, 26 Dec 2024 10:23:01 +0800 Subject: [PATCH 2/2] chore: update info --- builtin/builtin.mbti | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin/builtin.mbti b/builtin/builtin.mbti index f5d330733..3fa59beba 100644 --- a/builtin/builtin.mbti +++ b/builtin/builtin.mbti @@ -238,7 +238,7 @@ impl Iter { singleton[T](T) -> Self[T] take[T](Self[T], Int) -> Self[T] take_while[T](Self[T], (T) -> Bool) -> Self[T] - tap[T](Self[T], (T) -> Unit) -> Self[T] //deprecated + tap[T](Self[T], (T) -> Unit) -> Self[T] to_array[T](Self[T]) -> Array[T] } impl[T : Show] Show for Iter[T]