Skip to content

Commit

Permalink
feat(builtin)!: rename map_option to filter_map
Browse files Browse the repository at this point in the history
  • Loading branch information
rami3l authored and bobzhang committed Dec 25, 2024
1 parent 5daca41 commit 550f2a1
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 10 deletions.
17 changes: 16 additions & 1 deletion array/array.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand All @@ -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? {
Expand Down
3 changes: 2 additions & 1 deletion array/array.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions array/array_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -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]")
}

Expand Down
3 changes: 2 additions & 1 deletion builtin/builtin.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down
11 changes: 10 additions & 1 deletion builtin/iter.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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.
///
Expand Down
8 changes: 4 additions & 4 deletions builtin/iter_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -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="[]")
}

Expand Down

0 comments on commit 550f2a1

Please sign in to comment.