diff options
| author | Marijn Haverbeke <[email protected]> | 2011-05-06 22:13:13 +0200 |
|---|---|---|
| committer | Marijn Haverbeke <[email protected]> | 2011-05-06 22:51:19 +0200 |
| commit | a3ec0b1f643d00b9418e4884bd7caa07bf052201 (patch) | |
| tree | 82000510ac9c9cf3f0c7cf4ae5f3c6b123b559cb /src/lib/Option.rs | |
| parent | Register new snapshots. (diff) | |
| download | rust-a3ec0b1f643d00b9418e4884bd7caa07bf052201.tar.xz rust-a3ec0b1f643d00b9418e4884bd7caa07bf052201.zip | |
Rename std modules to be camelcased
(Have fun mergining your stuff with this.)
Diffstat (limited to 'src/lib/Option.rs')
| -rw-r--r-- | src/lib/Option.rs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/lib/Option.rs b/src/lib/Option.rs new file mode 100644 index 00000000..3fb9aa3b --- /dev/null +++ b/src/lib/Option.rs @@ -0,0 +1,60 @@ +// lib/Option.rs + +tag t[T] { + none; + some(T); +} + +type operator[T, U] = fn(&T) -> U; + +fn get[T](&t[T] opt) -> T { + alt (opt) { + case (some[T](?x)) { + ret x; + } + case (none[T]) { + fail; + } + } + fail; // FIXME: remove me when exhaustiveness checking works +} + +fn map[T, U](&operator[T, U] f, &t[T] opt) -> t[U] { + alt (opt) { + case (some[T](?x)) { + ret some[U](f(x)); + } + case (none[T]) { + ret none[U]; + } + } + fail; // FIXME: remove me when exhaustiveness checking works +} + +fn is_none[T](&t[T] opt) -> bool { + alt (opt) { + case (none[T]) { ret true; } + case (some[T](_)) { ret false; } + } +} + +fn from_maybe[T](&T def, &t[T] opt) -> T { + auto f = bind Util.id[T](_); + ret maybe[T, T](def, f, opt); +} + +fn maybe[T, U](&U def, fn(&T) -> U f, &t[T] opt) -> U { + alt (opt) { + case (none[T]) { ret def; } + case (some[T](?t)) { ret f(t); } + } +} +// 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 .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'"; +// End: + |