core: rename vec::position* functions

Almost all of the vec functions that predicates don't have a
corresponding function that takes a single element, so this
commit renames the common fn usecase to be the default.
This commit is contained in:
Erick Tryzelaar 2012-01-26 08:39:45 -08:00
parent a831e7ce13
commit 259636a112
7 changed files with 27 additions and 24 deletions

View File

@ -929,8 +929,8 @@ for the parameter list, as in `{|| ...}`.
Partial application is done using the `bind` keyword in Rust.
~~~~
let daynum = bind vec::position(_, ["mo", "tu", "we", "do",
"fr", "sa", "su"]);
let daynum = bind vec::position_elt(["mo", "tu", "we", "do",
"fr", "sa", "su"], _);
~~~~
Binding a function produces a boxed closure (`fn@` type) in which some

View File

@ -679,7 +679,7 @@ fn filter_invalid(src: list<@invalid>, bs: [binding]) -> list<@invalid> {
while cur != list::nil {
alt cur {
list::cons(head, tail) {
let p = vec::position_pred(bs, {|b| b.node_id == head.node_id});
let p = vec::position(bs, {|b| b.node_id == head.node_id});
if !is_none(p) { out = list::cons(head, @out); }
cur = *tail;
}

View File

@ -430,7 +430,7 @@ fn shape_of(ccx: @crate_ctxt, t: ty::t, ty_param_map: [uint]) -> [u8] {
}
ty::ty_param(n, _) {
// Find the type parameter in the parameter list.
alt vec::position(n, ty_param_map) {
alt vec::position_elt(ty_param_map, n) {
some(i) { s += [shape_var, i as u8]; }
none { fail "ty param not found in ty_param_map"; }
}

View File

@ -3375,7 +3375,7 @@ fn trans_rec(bcx: @block_ctxt, fields: [ast::field],
let ty_fields = alt ty::struct(bcx_tcx(bcx), t) { ty::ty_rec(f) { f } };
let temp_cleanups = [];
for fld in fields {
let ix = option::get(vec::position_pred(ty_fields, {|ft|
let ix = option::get(vec::position(ty_fields, {|ft|
str::eq(fld.node.ident, ft.ident)
}));
let dst = GEP_tup_like_1(bcx, t, addr, [0, ix as int]);

View File

@ -1513,7 +1513,7 @@ fn lookup_method(fcx: @fn_ctxt, isc: resolve::iscopes,
ty::ty_iface(i, tps) { (i, tps) }
};
let ifce_methods = ty::iface_methods(tcx, iid);
alt vec::position_pred(*ifce_methods, {|m| m.ident == name}) {
alt vec::position(*ifce_methods, {|m| m.ident == name}) {
some(pos) {
let m = ifce_methods[pos];
ret some({method_ty: ty::mk_fn(tcx, m.fty),

View File

@ -614,7 +614,7 @@ fn find<T: copy>(v: [T], f: fn(T) -> bool) -> option::t<T> {
}
/*
Function: position
Function: position_elt
Find the first index containing a matching value
@ -623,18 +623,16 @@ Returns:
option::some(uint) - The first index containing a matching value
option::none - No elements matched
*/
fn position<T>(x: T, v: [T]) -> option::t<uint> {
let i: uint = 0u;
while i < len(v) { if x == v[i] { ret some::<uint>(i); } i += 1u; }
ret none;
fn position_elt<T>(v: [T], x: T) -> option::t<uint> {
position(v) { |y| x == y }
}
/*
Function: position_pred
Function: position
Find the first index for which the value matches some predicate
*/
fn position_pred<T>(v: [T], f: fn(T) -> bool) -> option::t<uint> {
fn position<T>(v: [T], f: fn(T) -> bool) -> option::t<uint> {
let i: uint = 0u;
while i < len(v) { if f(v[i]) { ret some::<uint>(i); } i += 1u; }
ret none;
@ -1453,21 +1451,26 @@ mod tests {
}
#[test]
fn test_position() {
let v1: [int] = [1, 2, 3, 3, 2, 5];
assert (position(1, v1) == option::some::<uint>(0u));
assert (position(2, v1) == option::some::<uint>(1u));
assert (position(5, v1) == option::some::<uint>(5u));
assert (position(4, v1) == option::none::<uint>);
fn test_position_elt() {
assert position_elt([], 1) == none;
let v1 = [1, 2, 3, 3, 2, 5];
assert position_elt(v1, 1) == some(0u);
assert position_elt(v1, 2) == some(1u);
assert position_elt(v1, 5) == some(5u);
assert position_elt(v1, 4) == none;
}
#[test]
fn test_position_pred() {
fn test_position() {
fn less_than_three(&&i: int) -> bool { ret i < 3; }
fn is_eighteen(&&i: int) -> bool { ret i == 18; }
let v1: [int] = [5, 4, 3, 2, 1];
assert position_pred(v1, less_than_three) == option::some::<uint>(3u);
assert position_pred(v1, is_eighteen) == option::none::<uint>;
assert position([], less_than_three) == none;
let v1 = [5, 4, 3, 2, 1];
assert position(v1, less_than_three) == some(3u);
assert position(v1, is_eighteen) == none;
}
#[test]

View File

@ -149,7 +149,7 @@ fn name_str(nm: name) -> str {
}
fn find_opt(opts: [opt], nm: name) -> option::t<uint> {
vec::position_pred(opts, { |opt| opt.name == nm })
vec::position(opts, { |opt| opt.name == nm })
}
/*