aboutsummaryrefslogtreecommitdiff
path: root/src/test/bench/99-bottles/99bob-iter.rs
blob: e43541f492969b3a7a76998013a37bbdef72ea72 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// xfail-stage0
/* -*- mode:rust;indent-tabs-mode:nil -*- 
 * Implementation of 99 Bottles of Beer
 * http://99-bottles-of-beer.net/
 */
use std;
import std._int;
import std._str;

fn b1() -> str {
  ret "# of beer on the wall, # of beer.";
}
fn b2() -> str {
  ret "Take one down and pass it around, # of beer on the wall.";
}

fn b7() ->str {
  ret "No more bottles of beer on the wall, no more bottles of beer.";
}
fn b8() -> str {
  ret "Go to the store and buy some more, # of beer on the wall.";
}

fn sub(str t, int n) -> str {
  let str b = "";
  let uint i = 0u;
  let str ns;
  alt (n) {
    case (0) {
      ns = "no more bottles";
    }
case (1) {
    ns = "1 bottle";
  }
 case (_) {
    ns = _int.to_str(n, 10u) + " bottles";
  }
  }
  while (i < _str.byte_len(t)) {
    if (t.(i) == ('#' as u8)) {
      b += ns;
    }
    else {
      _str.push_byte(b, t.(i));
    }
    i += 1u;
  }
  ret b;
}

/* Using an interator */
iter ninetynine() -> int {
    let int n = 100;
    while (n > 1) {
      n -= 1;
      put n;
    }
  }
fn main() {
  for each (int n in ninetynine()) {
    log sub(b1(), n);
    log sub(b2(), n-1);
    log "";
  }
  log b7();
  log b8();
}