diff options
| author | Roy Frostig <[email protected]> | 2010-06-28 23:18:51 -0700 |
|---|---|---|
| committer | Roy Frostig <[email protected]> | 2010-06-28 23:18:51 -0700 |
| commit | 023e5af6398f8892fee429759096ba8c2480ed7d (patch) | |
| tree | 732480750073263707d6ce2cc43e562a7546dcf1 /src/lib | |
| parent | Add a NO_VALGRIND override mechanism to makefile, if you want day-to-day buil... (diff) | |
| download | rust-023e5af6398f8892fee429759096ba8c2480ed7d.tar.xz rust-023e5af6398f8892fee429759096ba8c2480ed7d.zip | |
The few and proud isolated bits from stdlib-work so far that don't break everything. Note util.rs hasn't yet been declared mod in the std crate. Don't do that yet, as it breaks make check.
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/_int.rs | 6 | ||||
| -rw-r--r-- | src/lib/_vec.rs | 11 | ||||
| -rw-r--r-- | src/lib/util.rs | 46 |
3 files changed, 63 insertions, 0 deletions
diff --git a/src/lib/_int.rs b/src/lib/_int.rs index 1bb6cb45..2afb3c4f 100644 --- a/src/lib/_int.rs +++ b/src/lib/_int.rs @@ -18,3 +18,9 @@ iter range(mutable int lo, int hi) -> int { } } +iter urange(mutable uint lo, uint hi) -> uint { + while (lo < hi) { + put lo; + lo += uint(1); + } +} diff --git a/src/lib/_vec.rs b/src/lib/_vec.rs index c938e6fb..86733fb5 100644 --- a/src/lib/_vec.rs +++ b/src/lib/_vec.rs @@ -28,3 +28,14 @@ fn len[T](vec[T] v) -> uint { fn buf[T](vec[T] v) -> vbuf { ret rustrt.vec_buf[T](v); } + +// Ought to take mutable &vec[T] v and just mutate it instead of copy +// and return. Blocking on issue #89 for this. +fn grow[T](mutable vec[T] v, int n, T initval) -> vec[T] { + let int i = n; + while (i > 0) { + i -= 1; + v += vec(initval); + } + ret v; +} diff --git a/src/lib/util.rs b/src/lib/util.rs new file mode 100644 index 00000000..bf57bb52 --- /dev/null +++ b/src/lib/util.rs @@ -0,0 +1,46 @@ +type option[T] = tag(none(), some(T)); +type box[T] = tup(@T); +type boxo[T] = option[box[T]]; +type boxm[T] = tup(mutable @T); +type boxmo[T] = option[boxm[T]]; + +type map[T, U] = fn(&T) -> U; + +fn option_map[T, U](map[T, U] f, &option[T] opt) -> option[U] { + alt (opt) { + case (some[T](x)) { + ret some[U](f[T, U](x)); + } + case (none[T]()) { + ret none[U](); + } + } +} + +fn unbox[T](&box[T] b) -> T { + ret b._0; +} + + +fn unboxm[T](&boxm[T] b) -> T { + ret b._0; +} + +fn unboxo[T](boxo[T] b) -> option[T] { + // Pending issue #90, no need to alias the function item in order to pass + // it as an arg. + let map[box[T], T] f = unbox[T]; + be option_map[box[T], T](f, b); +} + +fn unboxmo[T](boxmo[T] b) -> option[T] { + // Issue #90, as above + let map[boxm[T], T] f = unboxm[T]; + be option_map[boxm[T], T](f, b); +} + +fn id[T](T x) -> T { + ret x; +} + +type rational = rec(int num, int den); |