-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2929 from Dretch/tuplemethods
replace core::tuple functions with methods
- Loading branch information
Showing
8 changed files
with
34 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,33 @@ | ||
//! Operations on tuples | ||
/// Return the first element of a pair | ||
pure fn first<T:copy, U:copy>(pair: (T, U)) -> T { | ||
let (t, _) = pair; | ||
ret t; | ||
} | ||
|
||
/// Return the second element of a pair | ||
pure fn second<T:copy, U:copy>(pair: (T, U)) -> U { | ||
let (_, u) = pair; | ||
ret u; | ||
} | ||
impl extensions <T:copy, U:copy> for (T, U) { | ||
|
||
/// Return the first element of self | ||
pure fn first() -> T { | ||
let (t, _) = self; | ||
ret t; | ||
} | ||
|
||
/// Return the second element of self | ||
pure fn second() -> U { | ||
let (_, u) = self; | ||
ret u; | ||
} | ||
|
||
/// Return the results of swapping the two elements of self | ||
pure fn swap() -> (U, T) { | ||
let (t, u) = self; | ||
ret (u, t); | ||
} | ||
|
||
/// Return the results of swapping the two elements of a pair | ||
pure fn swap<T:copy, U:copy>(pair: (T, U)) -> (U, T) { | ||
let (t, u) = pair; | ||
ret (u, t); | ||
} | ||
|
||
|
||
#[test] | ||
fn test_tuple() { | ||
assert first((948, 4039.48)) == 948; | ||
assert second((34.5, ~"foo")) == ~"foo"; | ||
assert swap(('a', 2)) == (2, 'a'); | ||
assert (948, 4039.48).first() == 948; | ||
assert (34.5, ~"foo").second() == ~"foo"; | ||
assert ('a', 2).swap() == (2, 'a'); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters