Skip to content

Commit 60184d1

Browse files
wtingcatamorphism
authored andcommitted
Update documentation with examples for various int, vec methods.
add int::range(), remainder() examples add vec::foldl(), foldr() examples tweak
1 parent b49c47a commit 60184d1

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

src/libcore/int-template.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,27 @@ pub pure fn sub(x: T, y: T) -> T { x - y }
4545
pub pure fn mul(x: T, y: T) -> T { x * y }
4646
#[inline(always)]
4747
pub pure fn div(x: T, y: T) -> T { x / y }
48+
49+
/**
50+
* Returns the remainder of y / x.
51+
*
52+
* # Examples
53+
* ~~~
54+
* assert int::rem(5 / 2) == 1;
55+
* ~~~
56+
*
57+
* When faced with negative numbers, the result copies the sign of the
58+
* dividend.
59+
*
60+
* ~~~
61+
* assert int::rem(2 / -3) == 2;
62+
* ~~~
63+
*
64+
* ~~~
65+
* assert int::rem(-2 / 3) == -2;
66+
* ~~~
67+
*
68+
*/
4869
#[inline(always)]
4970
pub pure fn rem(x: T, y: T) -> T { x % y }
5071

@@ -70,8 +91,24 @@ pub pure fn is_nonpositive(x: T) -> bool { x <= 0 as T }
7091
#[inline(always)]
7192
pub pure fn is_nonnegative(x: T) -> bool { x >= 0 as T }
7293

94+
/**
95+
* Iterate over the range [`lo`..`hi`)
96+
*
97+
* # Arguments
98+
*
99+
* * `lo` - lower bound, inclusive
100+
* * `hi` - higher bound, exclusive
101+
*
102+
* # Examples
103+
* ~~~
104+
* let mut sum = 0;
105+
* for int::range(1, 5) |i| {
106+
* sum += i;
107+
* }
108+
* assert sum == 10;
109+
* ~~~
110+
*/
73111
#[inline(always)]
74-
/// Iterate over the range [`lo`..`hi`)
75112
pub pure fn range(lo: T, hi: T, it: fn(T) -> bool) {
76113
let mut i = lo;
77114
while i < hi {

src/libcore/vec.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,23 @@ pub pure fn connect<T: Copy>(v: &[~[T]], sep: &T) -> ~[T] {
922922
r
923923
}
924924

925-
/// Reduce a vector from left to right
925+
/**
926+
* Reduces a vector from left to right.
927+
*
928+
* # Arguments
929+
* * `z` - initial accumulator value
930+
* * `v` - vector to iterate over
931+
* * `p` - a closure to do operate on vector elements
932+
*
933+
* # Examples
934+
*
935+
* Sum all values in the vector [1, 2, 3]:
936+
*
937+
* ~~~
938+
* vec::foldl(0, [1, 2, 3], |a, b| a + *b);
939+
* ~~~
940+
*
941+
*/
926942
pub pure fn foldl<T, U>(z: T, v: &[U], p: fn(t: T, u: &U) -> T) -> T {
927943
let mut accum = z;
928944
let mut i = 0;
@@ -936,7 +952,25 @@ pub pure fn foldl<T, U>(z: T, v: &[U], p: fn(t: T, u: &U) -> T) -> T {
936952
return accum;
937953
}
938954

939-
/// Reduce a vector from right to left
955+
/**
956+
* Reduces a vector from right to left. Note that the argument order is
957+
* reversed compared to `foldl` to reflect the order they are provided to
958+
* the closure.
959+
*
960+
* # Arguments
961+
* * `v` - vector to iterate over
962+
* * `z` - initial accumulator value
963+
* * `p` - a closure to do operate on vector elements
964+
*
965+
* # Examples
966+
*
967+
* Sum all values in the vector [1, 2, 3]:
968+
*
969+
* ~~~
970+
* vec::foldr([1, 2, 3], 0, |a, b| a + *b);
971+
* ~~~
972+
*
973+
*/
940974
pub pure fn foldr<T, U: Copy>(v: &[T], z: U, p: fn(t: &T, u: U) -> U) -> U {
941975
let mut accum = z;
942976
for rev_each(v) |elt| {

0 commit comments

Comments
 (0)