about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorNiko Matsakis <niko@alum.mit.edu>2012-03-22 20:06:01 -0700
committerNiko Matsakis <niko@alum.mit.edu>2012-03-23 21:47:28 -0700
commit042c532a084fa3871a3a2d8955ff82c246db8015 (patch)
tree9b69f961a78deae15a4f8544bec7a4f215a90a5e /src/libcore
parent40443768b16747cb3ef90a2fb2fd9e7f317ff383 (diff)
downloadrust-042c532a084fa3871a3a2d8955ff82c246db8015.tar.gz
rust-042c532a084fa3871a3a2d8955ff82c246db8015.zip
Implement new inference algorithm.
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/iter.rs4
-rw-r--r--src/libcore/result.rs34
-rw-r--r--src/libcore/vec.rs26
3 files changed, 57 insertions, 7 deletions
diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs
index 141f7db2cb7..3d4ad1cf762 100644
--- a/src/libcore/iter.rs
+++ b/src/libcore/iter.rs
@@ -169,14 +169,14 @@ fn test_enumerate() {
 #[test]
 fn test_map_and_to_list() {
     let a = bind vec::iter([0, 1, 2], _);
-    let b = bind map(a, {|i| i*2}, _);
+    let b = bind map(a, {|i| 2*i}, _);
     let c = to_list(b);
     assert c == [0, 2, 4];
 }
 
 #[test]
 fn test_map_directly_on_vec() {
-    let b = bind map([0, 1, 2], {|i| i*2}, _);
+    let b = bind map([0, 1, 2], {|i| 2*i}, _);
     let c = to_list(b);
     assert c == [0, 2, 4];
 }
diff --git a/src/libcore/result.rs b/src/libcore/result.rs
index 3e040c14a0a..37a55e97aa1 100644
--- a/src/libcore/result.rs
+++ b/src/libcore/result.rs
@@ -91,6 +91,24 @@ fn chain<T, U: copy, V: copy>(res: result<T, V>, op: fn(T) -> result<U, V>)
     }
 }
 
+#[doc = "
+Call a function based on a previous result
+
+If `res` is `err` then the value is extracted and passed to `op`
+whereupon `op`s result is returned. if `res` is `ok` then it is
+immediately returned.  This function can be used to pass through a
+successful result while handling an error.
+"]
+fn chain_err<T: copy, U: copy, V: copy>(
+    res: result<T, V>,
+    op: fn(V) -> result<T, U>)
+    -> result<T, U> {
+    alt res {
+      ok(t) { ok(t) }
+      err(v) { op(v) }
+    }
+}
+
 // ______________________________________________________________________
 // Note:
 //
@@ -171,6 +189,22 @@ fn map2<S,T,U:copy,V:copy,W>(ss: [S], ts: [T],
     ret nxt(vs);
 }
 
+fn iter2<S,T,U:copy>(ss: [S], ts: [T],
+                     op: fn(S,T) -> result<(),U>)
+    : vec::same_length(ss, ts)
+    -> result<(),U> {
+    let n = vec::len(ts);
+    let mut i = 0u;
+    while i < n {
+        alt op(ss[i],ts[i]) {
+          ok(()) { }
+          err(u) { ret err(u); }
+        }
+        i += 1u;
+    }
+    ret ok(());
+}
+
 #[cfg(test)]
 mod tests {
     fn op1() -> result::result<int, str> { result::ok(666) }
diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs
index 71c4bbfe6ea..c760813bf7f 100644
--- a/src/libcore/vec.rs
+++ b/src/libcore/vec.rs
@@ -27,6 +27,7 @@ export rsplit;
 export rsplitn;
 export shift;
 export pop;
+export clear;
 export push;
 export grow;
 export grow_fn;
@@ -164,6 +165,13 @@ fn from_mut<T>(+v: [mutable T]) -> [T] unsafe {
     r
 }
 
+// This function only exists to work around bugs in the type checker.
+fn from_const<T>(+v: [const T]) -> [T] unsafe {
+    let r = ::unsafe::reinterpret_cast(v);
+    ::unsafe::forget(v);
+    r
+}
+
 // Accessors
 
 #[doc = "Returns the first element of a vector"]
@@ -336,6 +344,14 @@ fn pop<T>(&v: [const T]) -> T unsafe {
     val
 }
 
+#[doc = "
+Removes all elements from a vector without affecting
+how much space is reserved.
+"]
+fn clear<T>(&v: [const T]) unsafe {
+    unsafe::set_len(v, 0u);
+}
+
 #[doc = "Append an element to a vector"]
 fn push<T>(&v: [const T], +initval: T) {
     v += [initval];
@@ -466,8 +482,8 @@ Concatenate a vector of vectors.
 Flattens a vector of vectors of T into a single vector of T.
 "]
 fn concat<T: copy>(v: [const [const T]]) -> [T] {
-    let mut r: [T] = [];
-    for inner: [T] in v { r += inner; }
+    let mut r = [];
+    for inner in v { r += from_const(inner); }
     ret r;
 }
 
@@ -477,9 +493,9 @@ Concatenate a vector of vectors, placing a given separator between each
 fn connect<T: copy>(v: [const [const T]], sep: T) -> [T] {
     let mut r: [T] = [];
     let mut first = true;
-    for inner: [T] in v {
+    for inner in v {
         if first { first = false; } else { push(r, sep); }
-        r += inner;
+        r += from_const(inner);
     }
     ret r;
 }
@@ -885,7 +901,7 @@ fn as_mut_buf<E,T>(v: [mutable E], f: fn(*mutable E) -> T) -> T unsafe {
 }
 
 #[doc = "An extension implementation providing a `len` method"]
-impl vec_len<T> for [T] {
+impl vec_len<T> for [const T] {
     #[doc = "Return the length of the vector"]
     #[inline(always)]
     fn len() -> uint { len(self) }