Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 4 additions & 16 deletions tuple/README.mbt.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,14 @@ test {

## Transformation

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.
You can transform the tuple using the matrix functions combined with `then`.

```moonbit
test {
let tuple = (1, 2)
let _tuple2 = @tuple.map_fst(fn(x) { x + 1 }, tuple) // tuple2 = (2, 2)
let _tuple3 = @tuple.map_snd(fn(x) { x + 1 }, tuple) // tuple3 = (1, 3)
}
```

Or you can use the `map_both` method to apply the function to both elements of the tuple.

```moonbit
test {
let tuple = (1, 2)
let _mapped = @tuple.map_both(
fn(x : Int) -> Int { x + 1 },
fn(x : Int) -> Int { x - 1 },
tuple
) // mapped = (2, 1)
let _tuple2 = (fn { (x, y) => (x + 1, y) })(tuple) // tuple2 = (2, 2)
let _tuple3 = tuple |> then(fn { (x, y) => (x, y + 1) }) // tuple3 = (1, 3)
let _mapped = tuple |> then(fn(pair) { (pair.0 + 1, pair.1 - 1) }) // _mapped = 2, 1
}
```

Expand Down
3 changes: 3 additions & 0 deletions tuple/tuple.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub fn snd[T, U](tuple : (T, U)) -> U {
/// let mapped = @tuple.map_fst(fn(x : Int) -> Int { x + 1 }, tuple)
/// assert_eq!(mapped, (2, 2))
/// ```
#deprecated("use `tuple |> then(fn { (a, b) => (f(a), b) })` instead")
pub fn map_fst[T, U, V](f : (T) -> U, tuple : (T, V)) -> (U, V) {
(f(tuple.0), tuple.1)
}
Expand All @@ -72,6 +73,7 @@ pub fn map_fst[T, U, V](f : (T) -> U, tuple : (T, V)) -> (U, V) {
/// let mapped = @tuple.map_snd(fn(x : Int) -> Int { x + 1 }, tuple)
/// assert_eq!(mapped, (1, 3))
/// ```
#deprecated("use `tuple |> then(fn { (a, b) => (a, f(b)) })` instead")
pub fn map_snd[T, U, V](f : (T) -> U, tuple : (V, T)) -> (V, U) {
(tuple.0, f(tuple.1))
}
Expand All @@ -85,6 +87,7 @@ pub fn map_snd[T, U, V](f : (T) -> U, tuple : (V, T)) -> (V, U) {
/// let mapped = @tuple.map_both(fn(x : Int) -> Int { x + 1 }, fn(x : Int) -> Int { x - 1 }, tuple)
/// assert_eq!(mapped, (2, 1))
/// ```
#deprecated("use `tuple |> then(fn { (a, b) => (f(a), g(b)) })` instead")
pub fn map_both[T, U, V, W](
f : (T) -> U,
g : (V) -> W,
Expand Down
3 changes: 3 additions & 0 deletions tuple/tuple.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ fn curry[T, U, V]((T, U) -> V) -> (T) -> (U) -> V

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

#deprecated
fn map_both[T, U, V, W]((T) -> U, (V) -> W, (T, V)) -> (U, W)

#deprecated
fn map_fst[T, U, V]((T) -> U, (T, V)) -> (U, V)

#deprecated
fn map_snd[T, U, V]((T) -> U, (V, T)) -> (V, U)

fn pair[T, U](T, U) -> (T, U)
Expand Down