Skip to content

Commit cf0f760

Browse files
committed
auto merge of #6333 : bjz/rust/cond-macro, r=thestinger
Addressing issue #6037, this Scheme-style conditional helps to improve code clarity in instances where the `if`, `else if`, and `else` keywords obscure predicates undesirably. Here is an example: ~~~rust let clamped = if x > mx { mx } else if x < mn { mn } else { x }; ~~~ Using `cond!`, the above could be written as: ~~~rust let clamped = cond!( (x > mx) { mx } (x < mn) { mn } _ { x } ); ~~~ The optional default case is denoted by `_`. I have altered `std::fun_treemap` to demonstrate it in use. I am definitely interested in using it for some of the numeric functions, but I will have to wait for it to reach `stage0` first.
2 parents c30414f + 7e4a176 commit cf0f760

File tree

5 files changed

+105
-29
lines changed

5 files changed

+105
-29
lines changed

src/libstd/fun_treemap.rs

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,45 +33,40 @@ enum TreeNode<K, V> {
3333
pub fn init<K, V>() -> Treemap<K, V> { @Empty }
3434

3535
/// Insert a value into the map
36-
pub fn insert<K:Copy + Eq + Ord,V:Copy>(m: Treemap<K, V>, k: K, v: V)
37-
-> Treemap<K, V> {
36+
pub fn insert<K:Copy + Eq + Ord,V:Copy>(m: Treemap<K, V>, k: K, v: V) -> Treemap<K, V> {
3837
@match m {
39-
@Empty => Node(@k, @v, @Empty, @Empty),
40-
@Node(@copy kk, vv, left, right) => {
41-
if k < kk {
42-
Node(@kk, vv, insert(left, k, v), right)
43-
} else if k == kk {
44-
Node(@kk, @v, left, right)
45-
} else { Node(@kk, vv, left, insert(right, k, v)) }
46-
}
47-
}
38+
@Empty => Node(@k, @v, @Empty, @Empty),
39+
@Node(@copy kk, vv, left, right) => cond!(
40+
(k < kk) { Node(@kk, vv, insert(left, k, v), right) }
41+
(k == kk) { Node(@kk, @v, left, right) }
42+
_ { Node(@kk, vv, left, insert(right, k, v)) }
43+
)
44+
}
4845
}
4946

5047
/// Find a value based on the key
5148
pub fn find<K:Eq + Ord,V:Copy>(m: Treemap<K, V>, k: K) -> Option<V> {
5249
match *m {
53-
Empty => None,
54-
Node(@ref kk, @copy v, left, right) => {
55-
if k == *kk {
56-
Some(v)
57-
} else if k < *kk { find(left, k) } else { find(right, k) }
58-
}
50+
Empty => None,
51+
Node(@ref kk, @copy v, left, right) => cond!(
52+
(k == *kk) { Some(v) }
53+
(k < *kk) { find(left, k) }
54+
_ { find(right, k) }
55+
)
5956
}
6057
}
6158

6259
/// Visit all pairs in the map in order.
6360
pub fn traverse<K, V: Copy>(m: Treemap<K, V>, f: &fn(&K, &V)) {
6461
match *m {
65-
Empty => (),
66-
/*
67-
Previously, this had what looked like redundant
68-
matches to me, so I changed it. but that may be a
69-
de-optimization -- tjc
70-
*/
71-
Node(@ref k, @ref v, left, right) => {
72-
traverse(left, f);
73-
f(k, v);
74-
traverse(right, f);
75-
}
62+
Empty => (),
63+
// Previously, this had what looked like redundant
64+
// matches to me, so I changed it. but that may be a
65+
// de-optimization -- tjc
66+
Node(@ref k, @ref v, left, right) => {
67+
traverse(left, f);
68+
f(k, v);
69+
traverse(right, f);
70+
}
7671
}
7772
}

src/libstd/std.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ pub mod flatpipes;
6363

6464
pub mod bitv;
6565
pub mod deque;
66+
#[cfg(not(stage0))]
6667
pub mod fun_treemap;
6768
pub mod list;
6869
pub mod priority_queue;

src/libsyntax/ext/expand.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,41 @@ pub fn core_macros() -> ~str {
542542
}
543543
)
544544

545-
545+
//
546+
// A scheme-style conditional that helps to improve code clarity in some instances when
547+
// the `if`, `else if`, and `else` keywords obscure predicates undesirably.
548+
//
549+
// # Example
550+
//
551+
// ~~~
552+
// let clamped =
553+
// if x > mx { mx }
554+
// else if x < mn { mn }
555+
// else { x };
556+
// ~~~
557+
//
558+
// Using `cond!`, the above could be written as:
559+
//
560+
// ~~~
561+
// let clamped = cond!(
562+
// (x > mx) { mx }
563+
// (x < mn) { mn }
564+
// _ { x }
565+
// );
566+
// ~~~
567+
//
568+
// The optional default case is denoted by `_`.
569+
//
570+
macro_rules! cond (
571+
( $(($pred:expr) $body:block)+ _ $default:block ) => (
572+
$(if $pred $body else)+
573+
$default
574+
);
575+
// for if the default case was ommitted
576+
( $(($pred:expr) $body:block)+ ) => (
577+
$(if $pred $body)else+
578+
);
579+
)
546580
}";
547581
}
548582
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn clamp<T:Copy + Ord + Signed>(x: T, mn: T, mx: T) -> T {
12+
cond!(
13+
(x > mx) { return mx; }
14+
(x < mn) { return mn; }
15+
)
16+
return x;
17+
}
18+
19+
fn main() {
20+
assert_eq!(clamp(1, 2, 4), 2);
21+
assert_eq!(clamp(8, 2, 4), 4);
22+
assert_eq!(clamp(3, 2, 4), 3);
23+
}

src/test/run-pass/cond-macro.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn clamp<T:Copy + Ord + Signed>(x: T, mn: T, mx: T) -> T {
12+
cond!(
13+
(x > mx) { mx }
14+
(x < mn) { mn }
15+
_ { x }
16+
)
17+
}
18+
19+
fn main() {
20+
assert_eq!(clamp(1, 2, 4), 2);
21+
assert_eq!(clamp(8, 2, 4), 4);
22+
assert_eq!(clamp(3, 2, 4), 3);
23+
}

0 commit comments

Comments
 (0)