When function `f` returns true then an option containing the element
is returned. If `f` matches no elements then none is returned.
*/
-fn find<T: copy>(v: [T], f: fn(T) -> bool) -> option::t<T> {
+fn find<T: copy>(v: [T], f: fn(T) -> bool) -> option<T> {
- for elt: T in v { if f(elt) { ret some(elt); } }
- ret none;
+ find_from(v, 0u, len(v), f)
}
/*
- Function: position
+ Function: find_from
+
+ Search for the first element that matches a given predicate within a range
+
+ Apply function `f` to each element of `v` within the range [`start`, `end`).
+ When function `f` returns true then an option containing the element
+ is returned. If `f` matches no elements then none is returned.
+ */
+ fn find_from<T: copy>(v: [T], start: uint, end: uint, f: fn(T) -> bool) ->
- option::t<T> {
++ option<T> {
+ option::map(position_from(v, start, end, f)) { |i| v[i] }
+ }
+
+ /*
+ Function: rfind
+
+ Search for the last element that matches a given predicate
+
+ Apply function `f` to each element of `v` in reverse order. When function `f`
+ returns true then an option containing the element is returned. If `f`
+ matches no elements then none is returned.
+ */
-fn rfind<T: copy>(v: [T], f: fn(T) -> bool) -> option::t<T> {
++fn rfind<T: copy>(v: [T], f: fn(T) -> bool) -> option<T> {
+ rfind_from(v, 0u, len(v), f)
+ }
+
+ /*
+ Function: rfind_from
+
+ Search for the last element that matches a given predicate within a range
+
+ Apply function `f` to each element of `v` in reverse order within the range
+ [`start`, `end`). When function `f` returns true then an option containing
+ the element is returned. If `f` matches no elements then none is returned.
+ */
+ fn rfind_from<T: copy>(v: [T], start: uint, end: uint, f: fn(T) -> bool) ->
- option::t<T> {
++ option<T> {
+ option::map(rposition_from(v, start, end, f)) { |i| v[i] }
+ }
+
+ /*
+ Function: position_elt
Find the first index containing a matching value
option::some(uint) - The first index containing a matching value
option::none - No elements matched
*/
- fn position<T>(x: T, v: [T]) -> option<uint> {
- let i: uint = 0u;
- while i < len(v) { if x == v[i] { ret some::<uint>(i); } i += 1u; }
-fn position_elt<T>(v: [T], x: T) -> option::t<uint> {
++fn position_elt<T>(v: [T], x: T) -> option<uint> {
+ position(v) { |y| x == y }
+ }
+
+ /*
+ Function: position
+
+ Find the first index matching some predicate
+
+ Apply function `f` to each element of `v`. When function `f` returns true
+ then an option containing the index is returned. If `f` matches no elements
+ then none is returned.
+ */
-fn position<T>(v: [T], f: fn(T) -> bool) -> option::t<uint> {
++fn position<T>(v: [T], f: fn(T) -> bool) -> option<uint> {
+ position_from(v, 0u, len(v), f)
+ }
+
+ /*
+ Function: position_from
+
+ Find the first index matching some predicate within a range
+
+ Apply function `f` to each element of `v` between the range [`start`, `end`).
+ When function `f` returns true then an option containing the index is
+ returned. If `f` matches no elements then none is returned.
+ */
+ fn position_from<T>(v: [T], start: uint, end: uint, f: fn(T) -> bool) ->
- option::t<uint> {
++ option<uint> {
+ assert start <= end;
+ assert end <= len(v);
+ let i = start;
+ while i < end { if f(v[i]) { ret some::<uint>(i); } i += 1u; }
ret none;
}
/*
- Function: position_pred
+ Function: rposition_elt
+
+ Find the last index containing a matching value
+
+ Returns:
+
+ option::some(uint) - The last index containing a matching value
+ option::none - No elements matched
+ */
-fn rposition_elt<T>(v: [T], x: T) -> option::t<uint> {
++fn rposition_elt<T>(v: [T], x: T) -> option<uint> {
+ rposition(v) { |y| x == y }
+ }
+
+ /*
+ Function: rposition
+
+ Find the last index matching some predicate
- Find the first index for which the value matches some predicate
+ Apply function `f` to each element of `v` in reverse order. When function
+ `f` returns true then an option containing the index is returned. If `f`
+ matches no elements then none is returned.
*/
- fn position_pred<T>(v: [T], f: fn(T) -> bool) -> option<uint> {
- let i: uint = 0u;
- while i < len(v) { if f(v[i]) { ret some::<uint>(i); } i += 1u; }
-fn rposition<T>(v: [T], f: fn(T) -> bool) -> option::t<uint> {
++fn rposition<T>(v: [T], f: fn(T) -> bool) -> option<uint> {
+ rposition_from(v, 0u, len(v), f)
+ }
+
+ /*
+ Function: rposition_from
+
+ Find the last index matching some predicate within a range
+
+ Apply function `f` to each element of `v` in reverse order between the range
+ [`start`, `end`). When function `f` returns true then an option containing
+ the index is returned. If `f` matches no elements then none is returned.
+ */
+ fn rposition_from<T>(v: [T], start: uint, end: uint, f: fn(T) -> bool) ->
- option::t<uint> {
++ option<uint> {
+ assert start <= end;
+ assert end <= len(v);
+ let i = end;
+ while i > start {
+ if f(v[i - 1u]) { ret some::<uint>(i - 1u); }
+ i -= 1u;
+ }
ret none;
}