enum color { Red, Yellow, Blue }
-type creature_info = { name: uint, color: color };
+struct CreatureInfo {
+ name: uint,
+ color: color
+}
fn show_color(cc: color) -> ~str {
match (cc) {
fn creature(
name: uint,
color: color,
- from_rendezvous: oldcomm::Port<Option<creature_info>>,
- to_rendezvous: oldcomm::Chan<creature_info>,
+ from_rendezvous: oldcomm::Port<Option<CreatureInfo>>,
+ to_rendezvous: oldcomm::Chan<CreatureInfo>,
to_rendezvous_log: oldcomm::Chan<~str>
) {
let mut color = color;
loop {
// ask for a pairing
- oldcomm::send(to_rendezvous, {name: name, color: color});
+ oldcomm::send(to_rendezvous, CreatureInfo {name: name, color: color});
let resp = oldcomm::recv(from_rendezvous);
// log and change, or print and quit
}
// these ports will allow us to hear from the creatures
- let from_creatures: oldcomm::Port<creature_info> = oldcomm::Port();
+ let from_creatures: oldcomm::Port<CreatureInfo> = oldcomm::Port();
let from_creatures_log: oldcomm::Port<~str> = oldcomm::Port();
// these channels will be passed to the creatures so they can talk to us
let to_rendezvous_log = oldcomm::Chan(&from_creatures_log);
// these channels will allow us to talk to each creature by 'name'/index
- let to_creature: ~[oldcomm::Chan<Option<creature_info>>] =
+ let to_creature: ~[oldcomm::Chan<Option<CreatureInfo>>] =
vec::mapi(set, |ii, col| {
// create each creature as a listener with a port, and
// give us a channel to talk to each
// set up meetings...
for nn.times {
- let fst_creature: creature_info = oldcomm::recv(from_creatures);
- let snd_creature: creature_info = oldcomm::recv(from_creatures);
+ let fst_creature: CreatureInfo = oldcomm::recv(from_creatures);
+ let snd_creature: CreatureInfo = oldcomm::recv(from_creatures);
creatures_met += 2;
fn LINE_LENGTH() -> uint { return 60u; }
-type myrandom = @{mut last: u32};
+struct MyRandom {
+ mut last: u32
+}
-fn myrandom_next(r: myrandom, mx: u32) -> u32 {
+fn myrandom_next(r: @MyRandom, mx: u32) -> u32 {
r.last = (r.last * 3877u32 + 29573u32) % 139968u32;
mx * r.last / 139968u32
}
-type aminoacids = {ch: char, prob: u32};
+struct AminoAcids {
+ ch: char,
+ prob: u32
+}
-fn make_cumulative(aa: ~[aminoacids]) -> ~[aminoacids] {
+fn make_cumulative(aa: ~[AminoAcids]) -> ~[AminoAcids] {
let mut cp: u32 = 0u32;
- let mut ans: ~[aminoacids] = ~[];
- for aa.each |a| { cp += a.prob; ans += ~[{ch: a.ch, prob: cp}]; }
+ let mut ans: ~[AminoAcids] = ~[];
+ for aa.each |a| {
+ cp += a.prob;
+ ans += ~[AminoAcids {ch: a.ch, prob: cp}];
+ }
return ans;
}
-fn select_random(r: u32, genelist: ~[aminoacids]) -> char {
+fn select_random(r: u32, genelist: ~[AminoAcids]) -> char {
if r < genelist[0].prob { return genelist[0].ch; }
- fn bisect(v: ~[aminoacids], lo: uint, hi: uint, target: u32) -> char {
+ fn bisect(v: ~[AminoAcids], lo: uint, hi: uint, target: u32) -> char {
if hi > lo + 1u {
let mid: uint = lo + (hi - lo) / 2u;
if target < v[mid].prob {
} else { return bisect(v, mid, hi, target); }
} else { return v[hi].ch; }
}
- return bisect(copy genelist, 0, vec::len::<aminoacids>(genelist) - 1, r);
+ return bisect(copy genelist, 0, vec::len::<AminoAcids>(genelist) - 1, r);
}
-fn make_random_fasta(wr: io::Writer, id: ~str, desc: ~str, genelist: ~[aminoacids], n: int) {
+fn make_random_fasta(wr: io::Writer, id: ~str, desc: ~str, genelist: ~[AminoAcids], n: int) {
wr.write_line(~">" + id + ~" " + desc);
- let rng = @{mut last: rand::Rng().next()};
+ let rng = @MyRandom {mut last: rand::Rng().next()};
let mut op: ~str = ~"";
for uint::range(0u, n as uint) |_i| {
str::push_char(&mut op, select_random(myrandom_next(rng, 100u32),
}
}
-fn acid(ch: char, prob: u32) -> aminoacids { return {ch: ch, prob: prob}; }
+fn acid(ch: char, prob: u32) -> AminoAcids {
+ return AminoAcids {ch: ch, prob: prob};
+}
fn main() {
let args = os::args();
let n = int::from_str(args[1]).get();
- let iub: ~[aminoacids] =
+ let iub: ~[AminoAcids] =
make_cumulative(~[acid('a', 27u32), acid('c', 12u32), acid('g', 12u32),
acid('t', 27u32), acid('B', 2u32), acid('D', 2u32),
acid('H', 2u32), acid('K', 2u32), acid('M', 2u32),
acid('N', 2u32), acid('R', 2u32), acid('S', 2u32),
acid('V', 2u32), acid('W', 2u32), acid('Y', 2u32)]);
- let homosapiens: ~[aminoacids] =
+ let homosapiens: ~[AminoAcids] =
make_cumulative(~[acid('a', 30u32), acid('c', 20u32), acid('g', 20u32),
acid('t', 30u32)]);
let alu: ~str =
args
};
let n = int::from_str(args[1]).get();
- let mut bodies: ~[Body::props] = NBodySystem::make();
+ let mut bodies: ~[Body::Props] = NBodySystem::make();
io::println(fmt!("%f", NBodySystem::energy(bodies)));
let mut i = 0;
while i < n {
mod NBodySystem {
use Body;
- pub fn make() -> ~[Body::props] {
- let mut bodies: ~[Body::props] =
+ pub fn make() -> ~[Body::Props] {
+ let mut bodies: ~[Body::Props] =
~[Body::sun(),
Body::jupiter(),
Body::saturn(),
return bodies;
}
- pub fn advance(bodies: &mut [Body::props], dt: float) {
+ pub fn advance(bodies: &mut [Body::Props], dt: float) {
let mut i = 0;
while i < 5 {
let mut j = i + 1;
}
}
- pub fn advance_one(bi: &mut Body::props,
- bj: &mut Body::props,
+ pub fn advance_one(bi: &mut Body::Props,
+ bj: &mut Body::Props,
dt: float) {
unsafe {
let dx = bi.x - bj.x;
}
}
- pub fn move_(b: &mut Body::props, dt: float) {
+ pub fn move_(b: &mut Body::Props, dt: float) {
b.x += dt * b.vx;
b.y += dt * b.vy;
b.z += dt * b.vz;
}
- pub fn energy(bodies: &[Body::props]) -> float {
+ pub fn energy(bodies: &[Body::Props]) -> float {
unsafe {
let mut dx;
let mut dy;
// was 4 * PI * PI originally
pub const DAYS_PER_YEAR: float = 365.24;
- pub type props =
+ pub struct Props
{mut x: float,
mut y: float,
mut z: float,
mut vx: float,
mut vy: float,
mut vz: float,
- mass: float};
+ mass: float}
- pub fn jupiter() -> Body::props {
- return {mut x: 4.84143144246472090e+00,
+ pub fn jupiter() -> Body::Props {
+ return Props {mut x: 4.84143144246472090e+00,
mut y: -1.16032004402742839e+00,
mut z: -1.03622044471123109e-01,
mut vx: 1.66007664274403694e-03 * DAYS_PER_YEAR,
mass: 9.54791938424326609e-04 * SOLAR_MASS};
}
- pub fn saturn() -> Body::props {
- return {mut x: 8.34336671824457987e+00,
+ pub fn saturn() -> Body::Props {
+ return Props {mut x: 8.34336671824457987e+00,
mut y: 4.12479856412430479e+00,
mut z: -4.03523417114321381e-01,
mut vx: -2.76742510726862411e-03 * DAYS_PER_YEAR,
mass: 2.85885980666130812e-04 * SOLAR_MASS};
}
- pub fn uranus() -> Body::props {
- return {mut x: 1.28943695621391310e+01,
+ pub fn uranus() -> Body::Props {
+ return Props {mut x: 1.28943695621391310e+01,
mut y: -1.51111514016986312e+01,
mut z: -2.23307578892655734e-01,
mut vx: 2.96460137564761618e-03 * DAYS_PER_YEAR,
mass: 4.36624404335156298e-05 * SOLAR_MASS};
}
- pub fn neptune() -> Body::props {
- return {mut x: 1.53796971148509165e+01,
+ pub fn neptune() -> Body::Props {
+ return Props {mut x: 1.53796971148509165e+01,
mut y: -2.59193146099879641e+01,
mut z: 1.79258772950371181e-01,
mut vx: 2.68067772490389322e-03 * DAYS_PER_YEAR,
mass: 5.15138902046611451e-05 * SOLAR_MASS};
}
- pub fn sun() -> Body::props {
- return {mut x: 0.0,
+ pub fn sun() -> Body::Props {
+ return Props {mut x: 0.0,
mut y: 0.0,
mut z: 0.0,
mut vx: 0.0,
mass: SOLAR_MASS};
}
- pub fn offset_momentum(props: &mut Body::props,
+ pub fn offset_momentum(props: &mut Body::Props,
px: float, py: float, pz: float) {
props.vx = -px / SOLAR_MASS;
props.vy = -py / SOLAR_MASS;
p.recv()
}
-type config = {stress: bool};
+struct Config {
+ stress: bool
+}
-fn parse_opts(argv: ~[~str]) -> config {
+fn parse_opts(argv: ~[~str]) -> Config {
let opts = ~[getopts::optflag(~"stress")];
- let opt_args = vec::slice(argv, 1u, vec::len(argv));
+ let opt_args = vec::slice(argv, 1, argv.len());
match getopts::getopts(opt_args, opts) {
- Ok(ref m) => { return {stress: getopts::opt_present(m, ~"stress")} }
+ Ok(ref m) => {
+ return Config {stress: getopts::opt_present(m, ~"stress")}
+ }
Err(_) => { fail; }
}
}
type nillist = List<()>;
// Filled with things that have to be unwound
-enum st {
- st_({
- box: @nillist,
- unique: ~nillist,
- fn_box: fn@() -> @nillist,
- fn_unique: fn~() -> ~nillist,
- tuple: (@nillist, ~nillist),
- vec: ~[@nillist],
- res: r
- })
+
+struct State {
+ box: @nillist,
+ unique: ~nillist,
+ fn_box: fn@() -> @nillist,
+ fn_unique: fn~() -> ~nillist,
+ tuple: (@nillist, ~nillist),
+ vec: ~[@nillist],
+ res: r
}
struct r {
}
}
-fn recurse_or_fail(depth: int, st: Option<st>) {
+fn recurse_or_fail(depth: int, st: Option<State>) {
if depth == 0 {
debug!("unwinding %.4f", precise_time_s());
fail;
let st = match st {
None => {
- st_({
+ State {
box: @Nil,
unique: ~Nil,
fn_box: fn@() -> @nillist { @Nil::<()> },
tuple: (@Nil, ~Nil),
vec: ~[@Nil],
res: r(@Nil)
- })
+ }
}
Some(st) => {
let fn_box = st.fn_box;
let fn_unique = copy st.fn_unique;
- st_({
+ State {
box: @Cons((), st.box),
unique: ~Cons((), @*st.unique),
fn_box: fn@() -> @nillist { @Cons((), fn_box()) },
~Cons((), @*st.tuple.second())),
vec: st.vec + ~[@Cons((), st.vec.last())],
res: r(@Cons((), st.res._l))
- })
+ }
}
};