Skip to content

Commit 5d54def

Browse files
committed
core: Add extension methods for vec
1 parent f65ea0c commit 5d54def

File tree

3 files changed

+181
-13
lines changed

3 files changed

+181
-13
lines changed

Diff for: src/libcore/core.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
import option::{some, none};
66
import option = option::option;
77
import path = path::path;
8-
import vec::vec_len;
98
import str::extensions;
9+
import vec::extensions;
1010
import option::extensions;
11-
export path, option, some, none, vec_len, unreachable;
11+
12+
export path, option, some, none, unreachable;
1213
export extensions;
1314

1415
// Export the log levels as global constants. Higher levels mean

Diff for: src/libcore/vec.rs

+172-5
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ export permute;
7171
export windowed;
7272
export as_buf;
7373
export as_mut_buf;
74-
export vec_len;
7574
export unsafe;
7675
export u8;
76+
export extensions;
7777

7878
#[abi = "cdecl"]
7979
native mod rustrt {
@@ -954,13 +954,180 @@ fn as_mut_buf<E,T>(v: [mut E], f: fn(*mut E) -> T) -> T unsafe {
954954
let buf = unsafe::to_ptr(v) as *mut E; f(buf)
955955
}
956956

957-
#[doc = "An extension implementation providing a `len` method"]
958-
impl vec_len<T> for [const T] {
959-
#[doc = "Return the length of the vector"]
960-
#[inline(always)]
957+
#[doc = "Extension methods for vectors"]
958+
impl extensions<T> for [const T] {
959+
#[doc = "
960+
Return true if a vector contains an element with the given value
961+
"]
962+
#[inline]
963+
fn contains(x: T) -> bool { contains(self, x) }
964+
#[doc = "Returns the number of elements that are equal to a given value"]
965+
#[inline]
966+
fn count(x: T) -> uint { count(self, x) }
967+
#[doc = "Reduce a vector from left to right"]
968+
#[inline]
969+
fn foldl<U: copy>(z: U, p: fn(U, T) -> U) -> U { foldl(z, self, p) }
970+
#[doc = "Reduce a vector from right to left"]
971+
#[inline]
972+
fn foldr<U: copy>(z: U, p: fn(T, U) -> U) -> U { foldr(self, z, p) }
973+
#[doc = "Returns true if a vector contains no elements"]
974+
#[inline]
975+
fn is_empty() -> bool { is_empty(self) }
976+
#[doc = "Returns true if a vector contains some elements"]
977+
#[inline]
978+
fn is_not_empty() -> bool { is_not_empty(self) }
979+
#[doc = "
980+
Iterates over a vector
981+
982+
Iterates over vector `v` and, for each element, calls function `f` with
983+
the element's value.
984+
"]
985+
#[inline]
986+
fn iter(f: fn(T)) { iter(self, f) }
987+
#[doc = "
988+
Iterates over a vector's elements and indexes
989+
990+
Iterates over vector `v` and, for each element, calls function `f` with
991+
the element's value and index.
992+
"]
993+
#[inline]
994+
fn iteri(f: fn(uint, T)) { iteri(self, f) }
995+
#[doc = "Returns the length of a vector"]
996+
#[inline]
961997
fn len() -> uint { len(self) }
998+
#[doc = "
999+
Find the first index matching some predicate
1000+
1001+
Apply function `f` to each element of `v`. When function `f` returns true
1002+
then an option containing the index is returned. If `f` matches no
1003+
elements then none is returned.
1004+
"]
1005+
#[inline]
1006+
fn position(f: fn(T) -> bool) -> option<uint> { position(self, f) }
1007+
#[doc = "Find the first index containing a matching value"]
1008+
#[inline]
1009+
fn position_elem(x: T) -> option<uint> { position_elem(self, x) }
1010+
#[doc = "
1011+
Iterates over a vector in reverse
1012+
1013+
Iterates over vector `v` and, for each element, calls function `f` with
1014+
the element's value.
1015+
"]
1016+
#[inline]
1017+
fn riter(f: fn(T)) { riter(self, f) }
1018+
#[doc ="
1019+
Iterates over a vector's elements and indexes in reverse
1020+
1021+
Iterates over vector `v` and, for each element, calls function `f` with
1022+
the element's value and index.
1023+
"]
1024+
#[inline]
1025+
fn riteri(f: fn(uint, T)) { riteri(self, f) }
1026+
#[doc = "
1027+
Find the last index matching some predicate
1028+
1029+
Apply function `f` to each element of `v` in reverse order. When function
1030+
`f` returns true then an option containing the index is returned. If `f`
1031+
matches no elements then none is returned.
1032+
"]
1033+
#[inline]
1034+
fn rposition(f: fn(T) -> bool) -> option<uint> { rposition(self, f) }
1035+
#[doc = "Find the last index containing a matching value"]
1036+
#[inline]
1037+
fn rposition_elem(x: T) -> option<uint> { rposition_elem(self, x) }
1038+
}
1039+
1040+
#[doc = "Extension methods for vectors"]
1041+
impl extensions<T: copy> for [const T] {
1042+
#[doc = "
1043+
Search for the first element that matches a given predicate
1044+
1045+
Apply function `f` to each element of `v`, starting from the first.
1046+
When function `f` returns true then an option containing the element
1047+
is returned. If `f` matches no elements then none is returned.
1048+
"]
1049+
#[inline]
1050+
fn find(f: fn(T) -> bool) -> option<T> { find(self, f) }
1051+
#[doc = "Returns the first element of a vector"]
1052+
#[inline]
1053+
fn head() -> T { head(self) }
1054+
#[doc = "Returns all but the last elemnt of a vector"]
1055+
#[inline]
1056+
fn init() -> [T] { init(self) }
1057+
#[doc = "
1058+
Returns the last element of a `v`, failing if the vector is empty.
1059+
"]
1060+
#[inline]
1061+
fn last() -> T { last(self) }
1062+
#[doc = "
1063+
Search for the last element that matches a given predicate
1064+
1065+
Apply function `f` to each element of `v` in reverse order. When function
1066+
`f` returns true then an option containing the element is returned. If `f`
1067+
matches no elements then none is returned.
1068+
"]
1069+
#[inline]
1070+
fn rfind(f: fn(T) -> bool) -> option<T> { rfind(self, f) }
1071+
#[doc = "Returns a copy of the elements from [`start`..`end`) from `v`."]
1072+
#[inline]
1073+
fn slice(start: uint, end: uint) -> [T] { slice(self, start, end) }
1074+
#[doc = "Returns all but the first element of a vector"]
1075+
#[inline]
1076+
fn tail() -> [T] { tail(self) }
1077+
}
1078+
1079+
#[doc = "Extension methods for vectors"]
1080+
impl extensions<T> for [T] {
1081+
#[doc = "
1082+
Return true if a predicate matches all elements
1083+
1084+
If the vector contains no elements then true is returned.
1085+
"]
1086+
#[inline]
1087+
fn all(f: fn(T) -> bool) -> bool { all(self, f) }
1088+
#[doc = "
1089+
Return true if a predicate matches any elements
1090+
1091+
If the vector contains no elements then false is returned.
1092+
"]
1093+
#[inline]
1094+
fn any(f: fn(T) -> bool) -> bool { any(self, f) }
1095+
#[doc = "
1096+
Apply a function to each element of a vector and return the results
1097+
1098+
If function `f` returns `none` then that element is excluded from
1099+
the resulting vector.
1100+
"]
1101+
#[inline]
1102+
fn filter_map<U: copy>(f: fn(T) -> option<U>) -> [U] {
1103+
filter_map(self, f)
1104+
}
1105+
#[doc = "
1106+
Apply a function eo each element of a vector and return a concatenation
1107+
of each result vector
1108+
"]
1109+
#[inline]
1110+
fn flat_map<U>(f: fn(T) -> [U]) -> [U] { flat_map(self, f) }
1111+
#[doc = "
1112+
Apply a function to each element of a vector and return the results
1113+
"]
1114+
#[inline]
1115+
fn map<U>(f: fn(T) -> U) -> [U] { map(self, f) }
9621116
}
9631117

1118+
#[doc = "Extension methods for vectors"]
1119+
impl extensions<T: copy> for [T] {
1120+
#[doc = "
1121+
Construct a new vector from the elements of a vector for which some
1122+
predicate holds.
1123+
1124+
Apply function `f` to each element of `v` and return a vector containing
1125+
only those elements for which `f` returned true.
1126+
"]
1127+
#[inline]
1128+
fn filter(f: fn(T) -> bool) -> [T] { filter(self, f) }
1129+
}
1130+
9641131
#[doc = "Unsafe operations"]
9651132
mod unsafe {
9661133
// FIXME: This should have crate visibility

Diff for: src/test/run-pass/static-impl.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ impl util for uint {
1818
}
1919

2020
impl util<T> for [T] {
21-
fn length() -> uint { vec::len(self) }
22-
fn iter(f: fn(T)) { for x in self { f(x); } }
23-
fn map<U>(f: fn(T) -> U) -> [U] {
21+
fn length_() -> uint { vec::len(self) }
22+
fn iter_(f: fn(T)) { for x in self { f(x); } }
23+
fn map_<U>(f: fn(T) -> U) -> [U] {
2424
let mut r = [];
2525
for elt in self { r += [f(elt)]; }
2626
r
@@ -33,9 +33,9 @@ fn main() {
3333
assert 10u.plus() == 30;
3434
assert "hi".plus() == 200;
3535

36-
assert [1].length().str() == "1";
37-
assert [3, 4].map({|a| a + 4})[0] == 7;
38-
assert [3, 4].map::<uint>({|a| a as uint + 4u})[0] == 7u;
36+
assert [1].length_().str() == "1";
37+
assert [3, 4].map_({|a| a + 4})[0] == 7;
38+
assert [3, 4].map_::<uint>({|a| a as uint + 4u})[0] == 7u;
3939
let mut x = 0u;
4040
10u.times {|_n| x += 2u;}
4141
assert x == 20u;

0 commit comments

Comments
 (0)