From 550f2a1ee89f3f41a0252891714a23b270a67e59 Mon Sep 17 00:00:00 2001 From: rami3l Date: Tue, 17 Dec 2024 17:31:13 +0800 Subject: [PATCH] feat(builtin)!: rename `map_option` to `filter_map` --- array/array.mbt | 17 ++++++++++++++++- array/array.mbti | 3 ++- array/array_test.mbt | 4 ++-- builtin/builtin.mbti | 3 ++- builtin/iter.mbt | 11 ++++++++++- builtin/iter_test.mbt | 8 ++++---- 6 files changed, 36 insertions(+), 10 deletions(-) diff --git a/array/array.mbt b/array/array.mbt index 3816e1498..515373096 100644 --- a/array/array.mbt +++ b/array/array.mbt @@ -111,7 +111,7 @@ pub fn Array::shuffle[T](arr : Array[T], rand~ : (Int) -> Int) -> Array[T] { /// /// # Returns /// -pub fn map_option[A, B](self : Array[A], f : (A) -> B?) -> Array[B] { +pub fn filter_map[A, B](self : Array[A], f : (A) -> B?) -> Array[B] { let result = [] self.each(fn { x => @@ -123,6 +123,21 @@ pub fn map_option[A, B](self : Array[A], f : (A) -> B?) -> Array[B] { result } +///| +/// Returns a new array containing the elements of the original array that satisfy the given predicate. +/// +/// # Arguments +/// +/// * `self` - The array to filter. +/// * `f` - The predicate function. +/// +/// # Returns +/// +/// @alert deprecated "Use `Array::filter_map` instead" +pub fn map_option[A, B](self : Array[A], f : (A) -> B?) -> Array[B] { + self.filter_map(f) +} + ///| /// Returns the last element of the array. pub fn last[A](self : Array[A]) -> A? { diff --git a/array/array.mbti b/array/array.mbti index 85a269a32..4cf1eee66 100644 --- a/array/array.mbti +++ b/array/array.mbti @@ -49,10 +49,11 @@ impl FixedArray { impl Array { copy[T](Self[T]) -> Self[T] + filter_map[A, B](Self[A], (A) -> B?) -> Self[B] from_iter[T](Iter[T]) -> Self[T] last[A](Self[A]) -> A? makei[T](Int, (Int) -> T) -> Self[T] - map_option[A, B](Self[A], (A) -> B?) -> Self[B] + map_option[A, B](Self[A], (A) -> B?) -> Self[B] //deprecated push_iter[T](Self[T], Iter[T]) -> Unit shuffle[T](Self[T], rand~ : (Int) -> Int) -> Self[T] shuffle_in_place[T](Self[T], rand~ : (Int) -> Int) -> Unit diff --git a/array/array_test.mbt b/array/array_test.mbt index 87124d4ad..aa6ee18cf 100644 --- a/array/array_test.mbt +++ b/array/array_test.mbt @@ -513,9 +513,9 @@ test "Array::makei with negative length" { assert_eq!(arr, []) } -test "map_option" { +test "filter_map" { let arr = [1, 2, 3, 4, 5] - let mapped = arr.map_option(fn(x) { if x % 2 == 0 { Some(x) } else { None } }) + let mapped = arr.filter_map(fn(x) { if x % 2 == 0 { Some(x) } else { None } }) inspect!(mapped, content="[2, 4]") } diff --git a/builtin/builtin.mbti b/builtin/builtin.mbti index bebef7700..b3914d6b8 100644 --- a/builtin/builtin.mbti +++ b/builtin/builtin.mbti @@ -215,6 +215,7 @@ impl Iter { eachi[T](Self[T], (Int, T) -> Unit) -> Unit empty[T]() -> Self[T] filter[T](Self[T], (T) -> Bool) -> Self[T] + filter_map[A, B](Self[A], (A) -> B?) -> Self[B] find_first[T](Self[T], (T) -> Bool) -> T? flat_map[T, R](Self[T], (T) -> Self[R]) -> Self[R] fold[T, B](Self[T], init~ : B, (B, T) -> B) -> B @@ -224,7 +225,7 @@ impl Iter { just_run[T](Self[T], (T) -> IterResult) -> Unit last[A](Self[A]) -> A? map[T, R](Self[T], (T) -> R) -> Self[R] - map_option[A, B](Self[A], (A) -> B?) -> Self[B] + map_option[A, B](Self[A], (A) -> B?) -> Self[B] //deprecated map_while[A, B](Self[A], (A) -> B?) -> Self[B] new[T](((T) -> IterResult) -> IterResult) -> Self[T] op_add[T](Self[T], Self[T]) -> Self[T] diff --git a/builtin/iter.mbt b/builtin/iter.mbt index e15d38649..d550f1d60 100644 --- a/builtin/iter.mbt +++ b/builtin/iter.mbt @@ -418,7 +418,7 @@ pub fn map[T, R](self : Iter[T], f : (T) -> R) -> Iter[R] { ///| /// Transforms the elements of the iterator using a mapping function that returns an `Option`. /// The elements for which the function returns `None` are filtered out. -pub fn map_option[A, B](self : Iter[A], f : (A) -> B?) -> Iter[B] { +pub fn filter_map[A, B](self : Iter[A], f : (A) -> B?) -> Iter[B] { fn(yield_) { self.run(fn(a) { match f(a) { @@ -429,6 +429,15 @@ pub fn map_option[A, B](self : Iter[A], f : (A) -> B?) -> Iter[B] { } } +///| +/// Transforms the elements of the iterator using a mapping function that returns an `Option`. +/// The elements for which the function returns `None` are filtered out. +/// +/// @alert deprecated "Use `Iter::filter_map` instead" +pub fn map_option[A, B](self : Iter[A], f : (A) -> B?) -> Iter[B] { + self.filter_map(f) +} + ///| /// Transforms each element of the iterator into an iterator and flattens the resulting iterators into a single iterator. /// diff --git a/builtin/iter_test.mbt b/builtin/iter_test.mbt index f6628b979..881864386 100644 --- a/builtin/iter_test.mbt +++ b/builtin/iter_test.mbt @@ -213,16 +213,16 @@ test "map" { inspect!(exb, content="23456") } -test "map_option" { +test "filter_map" { let arr = [1, 2, 3, 4, 5] let r1 = arr .iter() - .map_option(fn(x) { if x < 3 { None } else { Some(x) } }) + .filter_map(fn(x) { if x < 3 { None } else { Some(x) } }) .collect() inspect!(r1, content="[3, 4, 5]") - let r2 : Array[Unit] = arr.iter().map_option(fn(_x) { None }).collect() + let r2 : Array[Unit] = arr.iter().filter_map(fn(_x) { None }).collect() inspect!(r2, content="[]") - let r3 : Array[Unit] = [].iter().map_option(Option::Some).collect() + let r3 : Array[Unit] = [].iter().filter_map(Option::Some).collect() inspect!(r3, content="[]") }