Skip to content

Commit e518158

Browse files
peter-jerry-yebobzhang
authored andcommitted
chore(tuple): deprecate map_xxx for tuple
1 parent 8efd1db commit e518158

File tree

3 files changed

+10
-16
lines changed

3 files changed

+10
-16
lines changed

tuple/README.mbt.md

+4-16
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,14 @@ test {
3131

3232
## Transformation
3333

34-
You can transform the tuple using the `map_fst` and `map_snd` method, which will apply the function to the first and second element of the tuple respectively.
34+
You can transform the tuple using the matrix functions combined with `then`.
3535

3636
```moonbit
3737
test {
3838
let tuple = (1, 2)
39-
let _tuple2 = @tuple.map_fst(fn(x) { x + 1 }, tuple) // tuple2 = (2, 2)
40-
let _tuple3 = @tuple.map_snd(fn(x) { x + 1 }, tuple) // tuple3 = (1, 3)
41-
}
42-
```
43-
44-
Or you can use the `map_both` method to apply the function to both elements of the tuple.
45-
46-
```moonbit
47-
test {
48-
let tuple = (1, 2)
49-
let _mapped = @tuple.map_both(
50-
fn(x : Int) -> Int { x + 1 },
51-
fn(x : Int) -> Int { x - 1 },
52-
tuple
53-
) // mapped = (2, 1)
39+
let _tuple2 = (fn { (x, y) => (x + 1, y) })(tuple) // tuple2 = (2, 2)
40+
let _tuple3 = tuple |> then(fn { (x, y) => (x, y + 1) }) // tuple3 = (1, 3)
41+
let _mapped = tuple |> then(fn(pair) { (pair.0 + 1, pair.1 - 1) }) // _mapped = 2, 1
5442
}
5543
```
5644

tuple/tuple.mbt

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pub fn snd[T, U](tuple : (T, U)) -> U {
5959
/// let mapped = @tuple.map_fst(fn(x : Int) -> Int { x + 1 }, tuple)
6060
/// assert_eq!(mapped, (2, 2))
6161
/// ```
62+
#deprecated("use `tuple |> then(fn { (a, b) => (f(a), b) })` instead")
6263
pub fn map_fst[T, U, V](f : (T) -> U, tuple : (T, V)) -> (U, V) {
6364
(f(tuple.0), tuple.1)
6465
}
@@ -72,6 +73,7 @@ pub fn map_fst[T, U, V](f : (T) -> U, tuple : (T, V)) -> (U, V) {
7273
/// let mapped = @tuple.map_snd(fn(x : Int) -> Int { x + 1 }, tuple)
7374
/// assert_eq!(mapped, (1, 3))
7475
/// ```
76+
#deprecated("use `tuple |> then(fn { (a, b) => (a, f(b)) })` instead")
7577
pub fn map_snd[T, U, V](f : (T) -> U, tuple : (V, T)) -> (V, U) {
7678
(tuple.0, f(tuple.1))
7779
}
@@ -85,6 +87,7 @@ pub fn map_snd[T, U, V](f : (T) -> U, tuple : (V, T)) -> (V, U) {
8587
/// let mapped = @tuple.map_both(fn(x : Int) -> Int { x + 1 }, fn(x : Int) -> Int { x - 1 }, tuple)
8688
/// assert_eq!(mapped, (2, 1))
8789
/// ```
90+
#deprecated("use `tuple |> then(fn { (a, b) => (f(a), g(b)) })` instead")
8891
pub fn map_both[T, U, V, W](
8992
f : (T) -> U,
9093
g : (V) -> W,

tuple/tuple.mbti

+3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ fn curry[T, U, V]((T, U) -> V) -> (T) -> (U) -> V
99

1010
fn fst[T, U]((T, U)) -> T
1111

12+
#deprecated
1213
fn map_both[T, U, V, W]((T) -> U, (V) -> W, (T, V)) -> (U, W)
1314

15+
#deprecated
1416
fn map_fst[T, U, V]((T) -> U, (T, V)) -> (U, V)
1517

18+
#deprecated
1619
fn map_snd[T, U, V]((T) -> U, (V, T)) -> (V, U)
1720

1821
fn pair[T, U](T, U) -> (T, U)

0 commit comments

Comments
 (0)