Skip to content

Instantly share code, notes, and snippets.

@reusee
Created September 24, 2012 15:54
Show Gist options
  • Select an option

  • Save reusee/3776678 to your computer and use it in GitHub Desktop.

Select an option

Save reusee/3776678 to your computer and use it in GitHub Desktop.
..
use task::spawn;
use uint::range;
use io::println;
use pipes::{stream, Port, Chan};
const N: uint = 100;
fn main() {
let mut chans: ~[mut (Chan<uint>, Port<uint>)] = ~[mut];
for range(0, N) |_i| {
vec::push(chans, stream());
}
for range(0, N) |i| {
let input = &chans[i].second();
if i == N - 1 {
let output = &chans[0].first();
do spawn { node(input, output); }
} else {
let output = &chans[i + 1].first();
do spawn { node(input, output); }
}
}
}
fn node(input: &Port<uint>, output: &Chan<uint>) {
let mut n: uint;
loop {
n = input.recv();
if n == 0 {
output.send(0);
break;
}
output.send(n - 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment