2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -78,7 +78,7 @@ OP WITH = ( INSPECTTOREPLACE inspected, STRING replace with )REF STRING:
|
|||
FI
|
||||
DO
|
||||
result +:= replace with;
|
||||
rest := rest[ UPB to replace : ]
|
||||
rest := rest[ 1 + UPB to replace : ]
|
||||
OD
|
||||
|
||||
ELIF option OF inspected = replace first
|
||||
|
|
|
|||
58
Task/Metaprogramming/Rust/metaprogramming.rust
Normal file
58
Task/Metaprogramming/Rust/metaprogramming.rust
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
// dry.rs
|
||||
use std::ops::{Add, Mul, Sub};
|
||||
|
||||
macro_rules! assert_equal_len {
|
||||
// The `tt` (token tree) designator is used for
|
||||
// operators and tokens.
|
||||
($a:ident, $b: ident, $func:ident, $op:tt) => (
|
||||
assert!($a.len() == $b.len(),
|
||||
"{:?}: dimension mismatch: {:?} {:?} {:?}",
|
||||
stringify!($func),
|
||||
($a.len(),),
|
||||
stringify!($op),
|
||||
($b.len(),));
|
||||
)
|
||||
}
|
||||
|
||||
macro_rules! op {
|
||||
($func:ident, $bound:ident, $op:tt, $method:ident) => (
|
||||
fn $func<T: $bound<T, Output=T> + Copy>(xs: &mut Vec<T>, ys: &Vec<T>) {
|
||||
assert_equal_len!(xs, ys, $func, $op);
|
||||
|
||||
for (x, y) in xs.iter_mut().zip(ys.iter()) {
|
||||
*x = $bound::$method(*x, *y);
|
||||
// *x = x.$method(*y);
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
// Implement `add_assign`, `mul_assign`, and `sub_assign` functions.
|
||||
op!(add_assign, Add, +=, add);
|
||||
op!(mul_assign, Mul, *=, mul);
|
||||
op!(sub_assign, Sub, -=, sub);
|
||||
|
||||
mod test {
|
||||
use std::iter;
|
||||
macro_rules! test {
|
||||
($func: ident, $x:expr, $y:expr, $z:expr) => {
|
||||
#[test]
|
||||
fn $func() {
|
||||
for size in 0usize..10 {
|
||||
let mut x: Vec<_> = iter::repeat($x).take(size).collect();
|
||||
let y: Vec<_> = iter::repeat($y).take(size).collect();
|
||||
let z: Vec<_> = iter::repeat($z).take(size).collect();
|
||||
|
||||
super::$func(&mut x, &y);
|
||||
|
||||
assert_eq!(x, z);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Test `add_assign`, `mul_assign` and `sub_assign`
|
||||
test!(add_assign, 1u32, 2u32, 3u32);
|
||||
test!(mul_assign, 2u32, 3u32, 6u32);
|
||||
test!(sub_assign, 3u32, 2u32, 1u32);
|
||||
}
|
||||
|
|
@ -1,27 +1,26 @@
|
|||
@(do
|
||||
(defmacro while ((condition : result) . body)
|
||||
(let ((cblk (gensym "cnt-blk-"))
|
||||
(bblk (gensym "brk-blk-")))
|
||||
^(macrolet ((break (value) ^(return-from ,',bblk ,value)))
|
||||
(symacrolet ((break (return-from ,bblk))
|
||||
(continue (return-from ,cblk)))
|
||||
(block ,bblk
|
||||
(for () (,condition ,result) ()
|
||||
(block ,cblk ,*body)))))))
|
||||
(defmacro whil ((condition : result) . body)
|
||||
(let ((cblk (gensym "cnt-blk-"))
|
||||
(bblk (gensym "brk-blk-")))
|
||||
^(macrolet ((break (value) ^(return-from ,',bblk ,value)))
|
||||
(symacrolet ((break (return-from ,bblk))
|
||||
(continue (return-from ,cblk)))
|
||||
(block ,bblk
|
||||
(for () (,condition ,result) ()
|
||||
(block ,cblk ,*body)))))))
|
||||
|
||||
(let ((i 0))
|
||||
(while ((< i 100))
|
||||
(let ((i 0))
|
||||
(whil ((< i 100))
|
||||
(if (< (inc i) 20)
|
||||
continue)
|
||||
(if (> i 30)
|
||||
break)
|
||||
(prinl i)))
|
||||
|
||||
(prinl
|
||||
(sys:expand
|
||||
'(whil ((< i 100))
|
||||
(if (< (inc i) 20)
|
||||
continue)
|
||||
(if (> i 30)
|
||||
break)
|
||||
(prinl i)))
|
||||
|
||||
(prinl
|
||||
(sys:expand
|
||||
'(while ((< i 100))
|
||||
(if (< (inc i) 20)
|
||||
continue)
|
||||
(if (> i 30)
|
||||
break)
|
||||
(prinl i)))))
|
||||
(prinl i))))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue