rust/src/lib/option.rs

40 lines
985 B
Rust
Raw Normal View History

// lib/option::rs
2011-08-09 19:36:07 -05:00
tag t<@T> { none; some(T); }
fn get<@T>(opt: t<T>) -> T {
alt opt { some(x) { x } none. { fail "option none" } }
2011-08-09 19:36:07 -05:00
}
2010-11-03 19:10:37 -05:00
fn map<@T, @U>(f: block(T) -> U, opt: t<T>) -> t<U> {
2011-08-09 19:36:07 -05:00
alt opt { some(x) { some(f(x)) } none. { none } }
2010-11-03 19:10:37 -05:00
}
fn is_none<@T>(opt: t<T>) -> bool {
2011-08-09 19:36:07 -05:00
alt opt { none. { true } some(_) { false } }
2011-03-17 13:21:11 -05:00
}
fn is_some<@T>(opt: t<T>) -> bool { !is_none(opt) }
2011-06-16 12:52:43 -05:00
fn from_maybe<@T>(def: T, opt: t<T>) -> T {
2011-08-09 19:36:07 -05:00
alt opt { some(x) { x } none. { def } }
}
fn maybe<@T, @U>(def: U, f: block(T) -> U, opt: t<T>) -> U {
2011-08-09 19:36:07 -05:00
alt opt { none. { def } some(t) { f(t) } }
}
// Can be defined in terms of the above when/if we have const bind.
fn may<@T>(f: block(T), opt: t<T>) {
2011-07-27 07:19:39 -05:00
alt opt { none. {/* nothing */ } some(t) { f(t); } }
}
2011-08-09 19:36:07 -05:00
2010-11-03 19:10:37 -05:00
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
2010-11-03 19:10:37 -05:00
// End: