2012-12-10 19:32:48 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-09-19 15:59:44 -05:00
|
|
|
// xfail-fast
|
2012-08-02 18:01:38 -05:00
|
|
|
|
|
|
|
// A trait for objects that can be used to do an if-then-else
|
|
|
|
// (No actual need for this to be static, but it is a simple test.)
|
|
|
|
trait bool_like {
|
2013-05-07 23:33:31 -05:00
|
|
|
fn select<A>(b: Self, x1: A, x2: A) -> A;
|
2012-08-02 18:01:38 -05:00
|
|
|
}
|
|
|
|
|
2013-02-20 19:07:17 -06:00
|
|
|
fn andand<T:bool_like + Copy>(x1: T, x2: T) -> T {
|
2012-12-18 20:05:16 -06:00
|
|
|
bool_like::select(x1, x2, x1)
|
2012-08-02 18:01:38 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl bool_like for bool {
|
2013-05-07 23:33:31 -05:00
|
|
|
fn select<A>(b: bool, x1: A, x2: A) -> A {
|
2013-02-15 04:44:18 -06:00
|
|
|
if b { x1 } else { x2 }
|
2012-08-02 18:01:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl bool_like for int {
|
2013-05-07 23:33:31 -05:00
|
|
|
fn select<A>(b: int, x1: A, x2: A) -> A {
|
2013-02-15 04:44:18 -06:00
|
|
|
if b != 0 { x1 } else { x2 }
|
2012-08-02 18:01:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A trait for sequences that can be constructed imperatively.
|
|
|
|
trait buildable<A> {
|
2013-05-07 23:33:31 -05:00
|
|
|
fn build_sized(size: uint, builder: &fn(push: &fn(v: A))) -> Self;
|
2012-08-02 18:01:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl<A> buildable<A> for @[A] {
|
2012-08-02 18:01:38 -05:00
|
|
|
#[inline(always)]
|
2013-05-07 23:33:31 -05:00
|
|
|
fn build_sized(size: uint, builder: &fn(push: &fn(v: A))) -> @[A] {
|
2012-08-02 18:01:38 -05:00
|
|
|
at_vec::build_sized(size, builder)
|
|
|
|
}
|
|
|
|
}
|
2013-02-14 13:47:00 -06:00
|
|
|
impl<A> buildable<A> for ~[A] {
|
2012-08-02 18:01:38 -05:00
|
|
|
#[inline(always)]
|
2013-05-07 23:33:31 -05:00
|
|
|
fn build_sized(size: uint, builder: &fn(push: &fn(v: A))) -> ~[A] {
|
2012-08-02 18:01:38 -05:00
|
|
|
vec::build_sized(size, builder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2013-05-07 23:33:31 -05:00
|
|
|
fn build<A, B: buildable<A>>(builder: &fn(push: &fn(v: A))) -> B {
|
2012-12-18 20:05:16 -06:00
|
|
|
buildable::build_sized(4, builder)
|
2012-08-02 18:01:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Apply a function to each element of an iterable and return the results
|
2012-08-14 18:54:13 -05:00
|
|
|
fn map<T, IT: BaseIter<T>, U, BU: buildable<U>>
|
2013-04-26 16:04:39 -05:00
|
|
|
(v: IT, f: &fn(&T) -> U) -> BU {
|
2012-08-02 18:01:38 -05:00
|
|
|
do build |push| {
|
|
|
|
for v.each() |elem| {
|
2013-04-26 16:04:39 -05:00
|
|
|
push(f(elem));
|
2012-08-02 18:01:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 19:07:17 -06:00
|
|
|
fn seq_range<BT:buildable<int>>(lo: uint, hi: uint) -> BT {
|
2012-12-18 20:05:16 -06:00
|
|
|
do buildable::build_sized(hi-lo) |push| {
|
2012-08-02 18:01:38 -05:00
|
|
|
for uint::range(lo, hi) |i| {
|
|
|
|
push(i as int);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-01 21:43:17 -06:00
|
|
|
pub fn main() {
|
2012-08-27 18:26:35 -05:00
|
|
|
let v: @[int] = seq_range(0, 10);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(v, @[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
2012-08-02 18:01:38 -05:00
|
|
|
|
2013-04-26 16:04:39 -05:00
|
|
|
let v: @[int] = map(&[1,2,3], |&x| 1+x);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(v, @[2, 3, 4]);
|
2013-04-26 16:04:39 -05:00
|
|
|
let v: ~[int] = map(&[1,2,3], |&x| 1+x);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(v, ~[2, 3, 4]);
|
2012-08-02 18:01:38 -05:00
|
|
|
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(bool_like::select(true, 9, 14), 9);
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(!andand(true, false));
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(andand(7, 12), 12);
|
|
|
|
assert_eq!(andand(0, 12), 0);
|
2012-08-02 18:01:38 -05:00
|
|
|
}
|