Change a bunch of places in the stdlib to use blocks.
This commit is contained in:
parent
de4b383a0f
commit
8a9766000e
@ -36,7 +36,7 @@ fn create(nbits: uint, init: bool) -> t {
|
||||
ret @{storage: storage, nbits: nbits};
|
||||
}
|
||||
|
||||
fn process(op: &fn(uint, uint) -> uint , v0: &t, v1: &t) -> bool {
|
||||
fn process(op: &block(uint, uint) -> uint , v0: &t, v1: &t) -> bool {
|
||||
let len = ivec::len(v1.storage);
|
||||
assert (ivec::len(v0.storage) == len);
|
||||
assert (v0.nbits == v1.nbits);
|
||||
|
@ -5,11 +5,8 @@ import option::none;
|
||||
|
||||
tag t[T, U] { left(T); right(U); }
|
||||
|
||||
type operator[T, U] = fn(&T) -> U ;
|
||||
|
||||
fn either[T, U,
|
||||
V](f_left: &operator[T, V], f_right: &operator[U, V],
|
||||
value: &t[T, U]) -> V {
|
||||
fn either[T, U, V](f_left: &block(&T) -> V, f_right: &block(&U) -> V,
|
||||
value: &t[T, U]) -> V {
|
||||
alt value { left(l) { f_left(l) } right(r) { f_right(r) } }
|
||||
}
|
||||
|
||||
|
@ -5,8 +5,6 @@ import option::some;
|
||||
import uint::next_power_of_two;
|
||||
import ptr::addr_of;
|
||||
|
||||
type operator2[T, U, V] = fn(&T, &U) -> V ;
|
||||
|
||||
native "rust-intrinsic" mod rusti {
|
||||
fn ivec_len[T](v: &[T]) -> uint;
|
||||
}
|
||||
@ -192,7 +190,7 @@ fn grow_set[@T](v: &mutable [mutable T], index: uint, initval: &T, val: &T) {
|
||||
|
||||
// Functional utilities
|
||||
|
||||
fn map[@T, @U](f: fn(&T) -> U , v: &[mutable? T]) -> [U] {
|
||||
fn map[@T, @U](f: &block(&T) -> U , v: &[mutable? T]) -> [U] {
|
||||
let result = ~[];
|
||||
reserve(result, len(v));
|
||||
for elem: T in v {
|
||||
@ -202,7 +200,8 @@ fn map[@T, @U](f: fn(&T) -> U , v: &[mutable? T]) -> [U] {
|
||||
ret result;
|
||||
}
|
||||
|
||||
fn filter_map[@T, @U](f: fn(&T) -> option::t[U] , v: &[mutable? T]) -> [U] {
|
||||
fn filter_map[@T, @U](f: &block(&T) -> option::t[U],
|
||||
v: &[mutable? T]) -> [U] {
|
||||
let result = ~[];
|
||||
for elem: T in v {
|
||||
let elem2 = elem; // satisfies alias checker
|
||||
@ -214,20 +213,20 @@ fn filter_map[@T, @U](f: fn(&T) -> option::t[U] , v: &[mutable? T]) -> [U] {
|
||||
ret result;
|
||||
}
|
||||
|
||||
fn foldl[@T, @U](p: fn(&U, &T) -> U , z: &U, v: &[mutable? T]) -> U {
|
||||
fn foldl[@T, @U](p: &block(&U, &T) -> U , z: &U, v: &[mutable? T]) -> U {
|
||||
let sz = len(v);
|
||||
if sz == 0u { ret z; }
|
||||
let first = v.(0);
|
||||
let rest = slice(v, 1u, sz);
|
||||
ret p(foldl[T, U](p, z, rest), first);
|
||||
ret p(foldl(p, z, rest), first);
|
||||
}
|
||||
|
||||
fn any[T](f: fn(&T) -> bool , v: &[T]) -> bool {
|
||||
fn any[T](f: &block(&T) -> bool , v: &[T]) -> bool {
|
||||
for elem: T in v { if f(elem) { ret true; } }
|
||||
ret false;
|
||||
}
|
||||
|
||||
fn all[T](f: fn(&T) -> bool , v: &[T]) -> bool {
|
||||
fn all[T](f: &block(&T) -> bool , v: &[T]) -> bool {
|
||||
for elem: T in v { if !f(elem) { ret false; } }
|
||||
ret true;
|
||||
}
|
||||
@ -243,9 +242,9 @@ fn count[T](x: &T, v: &[mutable? T]) -> uint {
|
||||
ret cnt;
|
||||
}
|
||||
|
||||
fn find[@T](f: fn(&T) -> bool , v: &[T]) -> option::t[T] {
|
||||
for elt: T in v { if f(elt) { ret some[T](elt); } }
|
||||
ret none[T];
|
||||
fn find[@T](f: &block(&T) -> bool , v: &[T]) -> option::t[T] {
|
||||
for elt: T in v { if f(elt) { ret some(elt); } }
|
||||
ret none;
|
||||
}
|
||||
|
||||
fn unzip[@T, @U](v: &[{_0: T, _1: U}]) -> {_0: [T], _1: [U]} {
|
||||
|
@ -13,7 +13,7 @@ fn from_vec[@T](v: vec[T]) -> list[T] {
|
||||
ret l;
|
||||
}
|
||||
|
||||
fn foldl[@T, @U](ls_: &list[T], u: &U, f: fn(&T, &U) -> U ) -> U {
|
||||
fn foldl[@T, @U](ls_: &list[T], u: &U, f: &block(&T, &U) -> U ) -> U {
|
||||
let accum: U = u;
|
||||
let ls = ls_;
|
||||
while true {
|
||||
@ -25,7 +25,8 @@ fn foldl[@T, @U](ls_: &list[T], u: &U, f: fn(&T, &U) -> U ) -> U {
|
||||
ret accum;
|
||||
}
|
||||
|
||||
fn find[@T, @U](ls_: &list[T], f: fn(&T) -> option::t[U] ) -> option::t[U] {
|
||||
fn find[@T, @U](ls_: &list[T], f: &block(&T) -> option::t[U])
|
||||
-> option::t[U] {
|
||||
let ls = ls_;
|
||||
while true {
|
||||
alt ls {
|
||||
|
@ -2,8 +2,6 @@
|
||||
|
||||
tag t[@T] { none; some(T); }
|
||||
|
||||
type operator[@T, @U] = fn(&T) -> U ;
|
||||
|
||||
fn get[@T](opt: &t[T]) -> T {
|
||||
alt opt {
|
||||
some(x) { x }
|
||||
@ -11,7 +9,7 @@ fn get[@T](opt: &t[T]) -> T {
|
||||
}
|
||||
}
|
||||
|
||||
fn map[@T, @U](f: &operator[T, U], opt: &t[T]) -> t[U] {
|
||||
fn map[@T, @U](f: &block(&T) -> U, opt: &t[T]) -> t[U] {
|
||||
alt opt { some(x) { some(f(x)) } none. { none } }
|
||||
}
|
||||
|
||||
@ -25,12 +23,12 @@ fn from_maybe[@T](def: &T, opt: &t[T]) -> T {
|
||||
alt opt { some(x) { x } none. { def } }
|
||||
}
|
||||
|
||||
fn maybe[@T, @U](def: &U, f: fn(&T) -> U , opt: &t[T]) -> U {
|
||||
fn maybe[@T, @U](def: &U, f: &block(&T) -> U, opt: &t[T]) -> U {
|
||||
alt opt { none. { def } some(t) { f(t) } }
|
||||
}
|
||||
|
||||
// Can be defined in terms of the above when/if we have const bind.
|
||||
fn may[@T](f: fn(&T) , opt: &t[T]) {
|
||||
fn may[@T](f: &block(&T), opt: &t[T]) {
|
||||
alt opt { none. {/* nothing */ } some(t) { f(t); } }
|
||||
}
|
||||
|
||||
|
@ -9,10 +9,10 @@ export merge_sort;
|
||||
export quick_sort;
|
||||
export quick_sort3;
|
||||
|
||||
type lteq[T] = fn(&T, &T) -> bool ;
|
||||
type lteq[T] = block(&T, &T) -> bool ;
|
||||
|
||||
fn merge_sort[@T](le: lteq[T], v: vec[T]) -> vec[T] {
|
||||
fn merge[@T](le: lteq[T], a: vec[T], b: vec[T]) -> vec[T] {
|
||||
fn merge_sort[@T](le: <eq[T], v: vec[T]) -> vec[T] {
|
||||
fn merge[@T](le: <eq[T], a: vec[T], b: vec[T]) -> vec[T] {
|
||||
let rs: vec[T] = [];
|
||||
let a_len: uint = len[T](a);
|
||||
let a_ix: uint = 0u;
|
||||
@ -42,8 +42,8 @@ fn swap[@T](arr: vec[mutable T], x: uint, y: uint) {
|
||||
arr.(y) = a;
|
||||
}
|
||||
|
||||
fn part[@T](compare_func: lteq[T], arr: vec[mutable T], left: uint,
|
||||
right: uint, pivot: uint) -> uint {
|
||||
fn part[@T](compare_func: <eq[T], arr: vec[mutable T], left: uint,
|
||||
right: uint, pivot: uint) -> uint {
|
||||
let pivot_value = arr.(pivot);
|
||||
swap[T](arr, pivot, right);
|
||||
let storage_index: uint = left;
|
||||
@ -59,8 +59,8 @@ fn part[@T](compare_func: lteq[T], arr: vec[mutable T], left: uint,
|
||||
ret storage_index;
|
||||
}
|
||||
|
||||
fn qsort[@T](compare_func: lteq[T], arr: vec[mutable T], left: uint,
|
||||
right: uint) {
|
||||
fn qsort[@T](compare_func: <eq[T], arr: vec[mutable T], left: uint,
|
||||
right: uint) {
|
||||
if right > left {
|
||||
let pivot = (left + right) / 2u;
|
||||
let new_pivot = part[T](compare_func, arr, left, right, pivot);
|
||||
@ -72,7 +72,7 @@ fn qsort[@T](compare_func: lteq[T], arr: vec[mutable T], left: uint,
|
||||
}
|
||||
}
|
||||
|
||||
fn quick_sort[@T](compare_func: lteq[T], arr: vec[mutable T]) {
|
||||
fn quick_sort[@T](compare_func: <eq[T], arr: vec[mutable T]) {
|
||||
if len[T](arr) == 0u { ret; }
|
||||
qsort[T](compare_func, arr, 0u, len[T](arr) - 1u);
|
||||
}
|
||||
@ -82,7 +82,7 @@ fn quick_sort[@T](compare_func: lteq[T], arr: vec[mutable T]) {
|
||||
// http://www.cs.princeton.edu/~rs/talks/QuicksortIsOptimal.pdf
|
||||
// According to these slides this is the algorithm of choice for
|
||||
// 'randomly ordered keys, abstract compare' & 'small number of key values'
|
||||
fn qsort3[@T](compare_func_lt: lteq[T], compare_func_eq: lteq[T],
|
||||
fn qsort3[@T](compare_func_lt: <eq[T], compare_func_eq: <eq[T],
|
||||
arr: vec[mutable T], left: int, right: int) {
|
||||
if right <= left { ret; }
|
||||
let v: T = arr.(right);
|
||||
@ -130,7 +130,7 @@ fn qsort3[@T](compare_func_lt: lteq[T], compare_func_eq: lteq[T],
|
||||
qsort3[T](compare_func_lt, compare_func_eq, arr, i, right);
|
||||
}
|
||||
|
||||
fn quick_sort3[@T](compare_func_lt: lteq[T], compare_func_eq: lteq[T],
|
||||
fn quick_sort3[@T](compare_func_lt: <eq[T], compare_func_eq: <eq[T],
|
||||
arr: vec[mutable T]) {
|
||||
if vec::len[T](arr) == 0u { ret; }
|
||||
qsort3[T](compare_func_lt, compare_func_eq, arr, 0,
|
||||
@ -144,8 +144,8 @@ mod ivector {
|
||||
|
||||
type lteq[T] = fn(&T, &T) -> bool ;
|
||||
|
||||
fn merge_sort[@T](le: lteq[T], v: &[T]) -> [T] {
|
||||
fn merge[@T](le: lteq[T], a: &[T], b: &[T]) -> [T] {
|
||||
fn merge_sort[@T](le: <eq[T], v: &[T]) -> [T] {
|
||||
fn merge[@T](le: <eq[T], a: &[T], b: &[T]) -> [T] {
|
||||
let rs: [T] = ~[];
|
||||
let a_len: uint = ilen[T](a);
|
||||
let a_ix: uint = 0u;
|
||||
@ -175,8 +175,8 @@ mod ivector {
|
||||
arr.(y) = a;
|
||||
}
|
||||
|
||||
fn part[@T](compare_func: lteq[T], arr: &[mutable T], left: uint,
|
||||
right: uint, pivot: uint) -> uint {
|
||||
fn part[@T](compare_func: <eq[T], arr: &[mutable T], left: uint,
|
||||
right: uint, pivot: uint) -> uint {
|
||||
let pivot_value = arr.(pivot);
|
||||
swap[T](arr, pivot, right);
|
||||
let storage_index: uint = left;
|
||||
@ -192,8 +192,8 @@ mod ivector {
|
||||
ret storage_index;
|
||||
}
|
||||
|
||||
fn qsort[@T](compare_func: lteq[T], arr: &[mutable T], left: uint,
|
||||
right: uint) {
|
||||
fn qsort[@T](compare_func: <eq[T], arr: &[mutable T], left: uint,
|
||||
right: uint) {
|
||||
if right > left {
|
||||
let pivot = (left + right) / 2u;
|
||||
let new_pivot = part[T](compare_func, arr, left, right, pivot);
|
||||
@ -205,7 +205,7 @@ mod ivector {
|
||||
}
|
||||
}
|
||||
|
||||
fn quick_sort[@T](compare_func: lteq[T], arr: &[mutable T]) {
|
||||
fn quick_sort[@T](compare_func: <eq[T], arr: &[mutable T]) {
|
||||
if ilen[T](arr) == 0u { ret; }
|
||||
qsort[T](compare_func, arr, 0u, ilen[T](arr) - 1u);
|
||||
}
|
||||
@ -216,8 +216,8 @@ mod ivector {
|
||||
// According to these slides this is the algorithm of choice for
|
||||
// 'randomly ordered keys, abstract compare' & 'small number of key
|
||||
// values'
|
||||
fn qsort3[@T](compare_func_lt: lteq[T], compare_func_eq: lteq[T],
|
||||
arr: &[mutable T], left: int, right: int) {
|
||||
fn qsort3[@T](compare_func_lt: <eq[T], compare_func_eq: <eq[T],
|
||||
arr: &[mutable T], left: int, right: int) {
|
||||
if right <= left { ret; }
|
||||
let v: T = arr.(right);
|
||||
let i: int = left - 1;
|
||||
@ -264,8 +264,8 @@ mod ivector {
|
||||
qsort3[T](compare_func_lt, compare_func_eq, arr, i, right);
|
||||
}
|
||||
|
||||
fn quick_sort3[@T](compare_func_lt: lteq[T], compare_func_eq: lteq[T],
|
||||
arr: &[mutable T]) {
|
||||
fn quick_sort3[@T](compare_func_lt: <eq[T], compare_func_eq: <eq[T],
|
||||
arr: &[mutable T]) {
|
||||
if ilen[T](arr) == 0u { ret; }
|
||||
qsort3[T](compare_func_lt, compare_func_eq, arr, 0,
|
||||
(ilen[T](arr) as int) - 1);
|
||||
|
@ -39,9 +39,8 @@ fn test_slice() {
|
||||
#[test]
|
||||
fn test_map() {
|
||||
fn square(x: &int) -> int { ret x * x; }
|
||||
let op: option::operator[int, int] = square;
|
||||
let v: vec[int] = [1, 2, 3, 4, 5];
|
||||
let s: vec[int] = map[int, int](op, v);
|
||||
let s: vec[int] = map(square, v);
|
||||
let i: int = 0;
|
||||
while i < 5 { assert (v.(i) * v.(i) == s.(i)); i += 1; }
|
||||
}
|
||||
@ -92,4 +91,4 @@ fn test_position_pred() {
|
||||
let v1: vec[int] = [5, 4, 3, 2, 1];
|
||||
assert (position_pred(less_than_three, v1) == option::some[uint](3u));
|
||||
assert (position_pred(is_eighteen, v1) == option::none[uint]);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user