Skip to content

Commit

Permalink
De-export iter and result. Part of #3583.
Browse files Browse the repository at this point in the history
  • Loading branch information
graydon committed Oct 2, 2012
1 parent 24fbf88 commit 39f114d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 53 deletions.
2 changes: 0 additions & 2 deletions src/libcore/core.rc
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ mod cmp;
mod num;
mod hash;
mod either;
#[legacy_exports]
mod iter;
mod logging;
mod option;
Expand All @@ -194,7 +193,6 @@ mod option_iter {
#[path = "option.rs"]
mod inst;
}
#[legacy_exports]
mod result;
mod to_str;
mod to_bytes;
Expand Down
74 changes: 42 additions & 32 deletions src/libcore/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,49 @@ The iteration traits and common implementation
use cmp::{Eq, Ord};

/// A function used to initialize the elements of a sequence
type InitOp<T> = &fn(uint) -> T;
pub type InitOp<T> = &fn(uint) -> T;

trait BaseIter<A> {
pub trait BaseIter<A> {
pure fn each(blk: fn(v: &A) -> bool);
pure fn size_hint() -> Option<uint>;
}

trait ExtendedIter<A> {
pub trait ExtendedIter<A> {
pure fn eachi(blk: fn(uint, v: &A) -> bool);
pure fn all(blk: fn(&A) -> bool) -> bool;
pure fn any(blk: fn(&A) -> bool) -> bool;
pure fn foldl<B>(+b0: B, blk: fn(&B, &A) -> B) -> B;
pure fn position(f: fn(&A) -> bool) -> Option<uint>;
}

trait EqIter<A:Eq> {
pub trait EqIter<A:Eq> {
pure fn contains(x: &A) -> bool;
pure fn count(x: &A) -> uint;
}

trait Times {
pub trait Times {
pure fn times(it: fn() -> bool);
}
trait TimesIx{

pub trait TimesIx{
pure fn timesi(it: fn(uint) -> bool);
}

trait CopyableIter<A:Copy> {
pub trait CopyableIter<A:Copy> {
pure fn filter_to_vec(pred: fn(+a: A) -> bool) -> ~[A];
pure fn map_to_vec<B>(op: fn(+v: A) -> B) -> ~[B];
pure fn to_vec() -> ~[A];
pure fn find(p: fn(+a: A) -> bool) -> Option<A>;
}

trait CopyableOrderedIter<A:Copy Ord> {
pub trait CopyableOrderedIter<A:Copy Ord> {
pure fn min() -> A;
pure fn max() -> A;
}

// A trait for sequences that can be by imperatively pushing elements
// onto them.
trait Buildable<A> {
pub trait Buildable<A> {
/**
* Builds a buildable sequence by calling a provided function with
* an argument function that pushes an element onto the back of
Expand All @@ -66,38 +67,42 @@ trait Buildable<A> {
builder: fn(push: pure fn(+v: A))) -> self;
}

pure fn eachi<A,IA:BaseIter<A>>(self: &IA, blk: fn(uint, v: &A) -> bool) {
pub pure fn eachi<A,IA:BaseIter<A>>(self: &IA,
blk: fn(uint, v: &A) -> bool) {
let mut i = 0;
for self.each |a| {
if !blk(i, a) { break; }
i += 1;
}
}

pure fn all<A,IA:BaseIter<A>>(self: &IA, blk: fn(&A) -> bool) -> bool {
pub pure fn all<A,IA:BaseIter<A>>(self: &IA,
blk: fn(&A) -> bool) -> bool {
for self.each |a| {
if !blk(a) { return false; }
}
return true;
}

pure fn any<A,IA:BaseIter<A>>(self: &IA, blk: fn(&A) -> bool) -> bool {
pub pure fn any<A,IA:BaseIter<A>>(self: &IA,
blk: fn(&A) -> bool) -> bool {
for self.each |a| {
if blk(a) { return true; }
}
return false;
}

pure fn filter_to_vec<A:Copy,IA:BaseIter<A>>(self: &IA,
prd: fn(+a: A) -> bool) -> ~[A] {
pub pure fn filter_to_vec<A:Copy,IA:BaseIter<A>>(
self: &IA, prd: fn(+a: A) -> bool) -> ~[A] {
do vec::build_sized_opt(self.size_hint()) |push| {
for self.each |a| {
if prd(*a) { push(*a); }
}
}
}

pure fn map_to_vec<A:Copy,B,IA:BaseIter<A>>(self: &IA, op: fn(+v: A) -> B)
pub pure fn map_to_vec<A:Copy,B,IA:BaseIter<A>>(self: &IA,
op: fn(+v: A) -> B)
-> ~[B] {
do vec::build_sized_opt(self.size_hint()) |push| {
for self.each |a| {
Expand All @@ -106,7 +111,7 @@ pure fn map_to_vec<A:Copy,B,IA:BaseIter<A>>(self: &IA, op: fn(+v: A) -> B)
}
}

pure fn flat_map_to_vec<A:Copy,B:Copy,IA:BaseIter<A>,IB:BaseIter<B>>(
pub pure fn flat_map_to_vec<A:Copy,B:Copy,IA:BaseIter<A>,IB:BaseIter<B>>(
self: &IA, op: fn(+a: A) -> IB) -> ~[B] {

do vec::build |push| {
Expand All @@ -118,7 +123,8 @@ pure fn flat_map_to_vec<A:Copy,B:Copy,IA:BaseIter<A>,IB:BaseIter<B>>(
}
}

pure fn foldl<A,B,IA:BaseIter<A>>(self: &IA, +b0: B, blk: fn(&B, &A) -> B)
pub pure fn foldl<A,B,IA:BaseIter<A>>(self: &IA, +b0: B,
blk: fn(&B, &A) -> B)
-> B {
let mut b <- b0;
for self.each |a| {
Expand All @@ -127,18 +133,18 @@ pure fn foldl<A,B,IA:BaseIter<A>>(self: &IA, +b0: B, blk: fn(&B, &A) -> B)
move b
}

pure fn to_vec<A:Copy,IA:BaseIter<A>>(self: &IA) -> ~[A] {
pub pure fn to_vec<A:Copy,IA:BaseIter<A>>(self: &IA) -> ~[A] {
foldl::<A,~[A],IA>(self, ~[], |r, a| vec::append(copy (*r), ~[*a]))
}

pure fn contains<A:Eq,IA:BaseIter<A>>(self: &IA, x: &A) -> bool {
pub pure fn contains<A:Eq,IA:BaseIter<A>>(self: &IA, x: &A) -> bool {
for self.each |a| {
if *a == *x { return true; }
}
return false;
}

pure fn count<A:Eq,IA:BaseIter<A>>(self: &IA, x: &A) -> uint {
pub pure fn count<A:Eq,IA:BaseIter<A>>(self: &IA, x: &A) -> uint {
do foldl(self, 0) |count, value| {
if *value == *x {
*count + 1
Expand All @@ -148,7 +154,7 @@ pure fn count<A:Eq,IA:BaseIter<A>>(self: &IA, x: &A) -> uint {
}
}

pure fn position<A,IA:BaseIter<A>>(self: &IA, f: fn(&A) -> bool)
pub pure fn position<A,IA:BaseIter<A>>(self: &IA, f: fn(&A) -> bool)
-> Option<uint>
{
let mut i = 0;
Expand All @@ -163,15 +169,15 @@ pure fn position<A,IA:BaseIter<A>>(self: &IA, f: fn(&A) -> bool)
// iter interface, such as would provide "reach" in addition to "each". as is,
// it would have to be implemented with foldr, which is too inefficient.

pure fn repeat(times: uint, blk: fn() -> bool) {
pub pure fn repeat(times: uint, blk: fn() -> bool) {
let mut i = 0;
while i < times {
if !blk() { break }
i += 1;
}
}

pure fn min<A:Copy Ord,IA:BaseIter<A>>(self: &IA) -> A {
pub pure fn min<A:Copy Ord,IA:BaseIter<A>>(self: &IA) -> A {
match do foldl::<A,Option<A>,IA>(self, None) |a, b| {
match a {
&Some(ref a_) if *a_ < *b => {
Expand All @@ -185,7 +191,7 @@ pure fn min<A:Copy Ord,IA:BaseIter<A>>(self: &IA) -> A {
}
}

pure fn max<A:Copy Ord,IA:BaseIter<A>>(self: &IA) -> A {
pub pure fn max<A:Copy Ord,IA:BaseIter<A>>(self: &IA) -> A {
match do foldl::<A,Option<A>,IA>(self, None) |a, b| {
match a {
&Some(ref a_) if *a_ > *b => {
Expand All @@ -199,7 +205,7 @@ pure fn max<A:Copy Ord,IA:BaseIter<A>>(self: &IA) -> A {
}
}

pure fn find<A: Copy,IA:BaseIter<A>>(self: &IA,
pub pure fn find<A: Copy,IA:BaseIter<A>>(self: &IA,
p: fn(+a: A) -> bool) -> Option<A> {
for self.each |i| {
if p(*i) { return Some(*i) }
Expand All @@ -220,7 +226,8 @@ pure fn find<A: Copy,IA:BaseIter<A>>(self: &IA,
* onto the sequence being constructed.
*/
#[inline(always)]
pure fn build<A,B: Buildable<A>>(builder: fn(push: pure fn(+v: A))) -> B {
pub pure fn build<A,B: Buildable<A>>(builder: fn(push: pure fn(+v: A)))
-> B {
build_sized(4, builder)
}

Expand All @@ -238,7 +245,7 @@ pure fn build<A,B: Buildable<A>>(builder: fn(push: pure fn(+v: A))) -> B {
* onto the sequence being constructed.
*/
#[inline(always)]
pure fn build_sized_opt<A,B: Buildable<A>>(
pub pure fn build_sized_opt<A,B: Buildable<A>>(
size: Option<uint>,
builder: fn(push: pure fn(+v: A))) -> B {

Expand All @@ -248,7 +255,8 @@ pure fn build_sized_opt<A,B: Buildable<A>>(
// Functions that combine iteration and building

/// Apply a function to each element of an iterable and return the results
fn map<T,IT: BaseIter<T>,U,BU: Buildable<U>>(v: &IT, f: fn(&T) -> U) -> BU {
pub fn map<T,IT: BaseIter<T>,U,BU: Buildable<U>>(v: &IT, f: fn(&T) -> U)
-> BU {
do build_sized_opt(v.size_hint()) |push| {
for v.each() |elem| {
push(f(elem));
Expand All @@ -262,7 +270,8 @@ fn map<T,IT: BaseIter<T>,U,BU: Buildable<U>>(v: &IT, f: fn(&T) -> U) -> BU {
* Creates a generic sequence of size `n_elts` and initializes the elements
* to the value returned by the function `op`.
*/
pure fn from_fn<T,BT: Buildable<T>>(n_elts: uint, op: InitOp<T>) -> BT {
pub pure fn from_fn<T,BT: Buildable<T>>(n_elts: uint,
op: InitOp<T>) -> BT {
do build_sized(n_elts) |push| {
let mut i: uint = 0u;
while i < n_elts { push(op(i)); i += 1u; }
Expand All @@ -275,7 +284,8 @@ pure fn from_fn<T,BT: Buildable<T>>(n_elts: uint, op: InitOp<T>) -> BT {
* Creates an immutable vector of size `n_elts` and initializes the elements
* to the value `t`.
*/
pure fn from_elem<T: Copy,BT: Buildable<T>>(n_elts: uint, +t: T) -> BT {
pub pure fn from_elem<T: Copy,BT: Buildable<T>>(n_elts: uint,
+t: T) -> BT {
do build_sized(n_elts) |push| {
let mut i: uint = 0;
while i < n_elts { push(t); i += 1; }
Expand All @@ -284,7 +294,7 @@ pure fn from_elem<T: Copy,BT: Buildable<T>>(n_elts: uint, +t: T) -> BT {

/// Appending two generic sequences
#[inline(always)]
pure fn append<T: Copy,IT: BaseIter<T>,BT: Buildable<T>>(
pub pure fn append<T: Copy,IT: BaseIter<T>,BT: Buildable<T>>(
lhs: &IT, rhs: &IT) -> BT {
let size_opt = lhs.size_hint().chain_ref(
|sz1| rhs.size_hint().map(|sz2| *sz1+*sz2));
Expand All @@ -297,7 +307,7 @@ pure fn append<T: Copy,IT: BaseIter<T>,BT: Buildable<T>>(
/// Copies a generic sequence, possibly converting it to a different
/// type of sequence.
#[inline(always)]
pure fn copy_seq<T: Copy,IT: BaseIter<T>,BT: Buildable<T>>(
pub pure fn copy_seq<T: Copy,IT: BaseIter<T>,BT: Buildable<T>>(
v: &IT) -> BT {
do build_sized_opt(v.size_hint()) |push| {
for v.each |x| { push(*x); }
Expand Down
Loading

0 comments on commit 39f114d

Please sign in to comment.