This question already has answers here:
How to compare one value against multiple values - Swift
(8 answers)
Closed 6 years ago.
I have a situation where I have to make a check like this:
if foo == a || foo == b || foo == c {
//do stuff
}
is there any way to chain these operands into something smaller like IDK
foo == a||b||c
Try this:
if [a, b, c].contains(foo) {
//do stuff
}
Related
This question already has answers here:
Is there a way to count with macros?
(4 answers)
Counting length of repetition in macro
(3 answers)
Using a macro to initialize a big array of non-Copy elements
(3 answers)
Closed 6 years ago.
Say we need to declare a fixed size array with values, where the size of the array is defined by a constant that may change depending on compile time settings.
So for example:
let my_array = expand_into_array!(j, ARRAY_SIZE, -v0[j] * f);
Where ARRAY_SIZE is a constant, for example:
const ARRAY_SIZE: usize = 3;
Could expand into something like...
let my_array = [
{let j = 0; {-v0[j] * f}},
{let j = 1; {-v0[j] * f}},
{let j = 2; {-v0[j] * f}},
];
Since the expression is a fixed size array, it may be possible to use pattern matching, for a limited number of items ... up to 32 for example.
Is it possible to write a macro that expands an expression N times, based on a constant integer?
Details...
Looking into this, I wrote a macro which defines an array, then fills it in, eg:
const ARRAY_SIZE: usize = 3;
macro_rules! expand_into_array {
($index_var:ident, $const_size:expr, $body:expr) => {
{
let mut tmp: [_; $const_size] = [0.0; $const_size];
for $index_var in 0..$const_size {
tmp[$index_var] = $body;
}
// TODO, check $body _never_ breaks.
tmp
}
}
}
pub fn negated_array(v0: &[f64; ARRAY_SIZE]) -> [f64; ARRAY_SIZE] {
expand_into_array!(j, ARRAY_SIZE, {
-v0[j]
})
}
This works as expected, and besides the wrinkle (that the $body expression could include a break). this works without problems.
However initializing the array to 0.0 isn't getting optimized out (changing this value shows up as changes when run with: cargo rustc --release -- --emit asm
I'd rather not use unsafe { std::mem::uninitialized }.
Update, from asking another question, it seems macros can only match against literals, and not constants.
So this is simply not possible with macros in Rust.
This question already has an answer here:
How does CoffeeScript's existential operator work?
(1 answer)
Closed 8 years ago.
Is there a way to force Coffeescript to always convert
if x?
console.log "hello"
to
if (typeof x !== "undefined" && x !== null) {
console.log("hello");
}
The reason I'm asking is because if we have the following:
x = "hello"
if x?
console.log "hello"
It gets converted to:
var x;
x = "hello";
if (x != null) {
console.log("hello");
}
While this is not a problem in the code above, it is problematic in functions to which an undefined variable is passed.
x != null also covers x != undefined so the converted code should not be entering the if statement if x is undefined.
See more in this answer
This question already has answers here:
matlab double comparison
(3 answers)
Closed 9 years ago.
I'm trying to compare three numbers in a if statement but they don't match even if they are the same.
here are the numbers from the command window,
>> a = (round2(final_aucscore(10, 1),1e-4))
a =
0.9369
>> b = (round2(final_aucscore(10, 2),1e-4))
b =
0.9598
>> c = (round2(final_aucscore(10, 3),1e-4))
c =
0.9509
the function round2 can be found in the file echange here.
here's my code:
for mmm = 1:265
a = (round2(final_aucscore(mmm, 1),1e-4));
b = (round2(final_aucscore(mmm, 2),1e-4));
c = (round2(final_aucscore(mmm, 3),1e-4));
if a == 0.9369 && b == 0.9598 && c == 0.9509
auc_idx = idx(1:kk);
save('auc_idx', 'auc_idx', 'mmm');
break;
end
end
shouldn't it stop and save when mmm = 10?
The problem you're facing here comes from rounding error.
Try this
foo = 0.9369;
bar = (round2(foo,1e-4));
disp(bar == foo)
disp(foo-bar)
You could instead make a function to compare only within a certain precision.
What every computer scientist should know about floating-point arithmetic. Read it and weep.
This question already has an answer here:
Calling C function from Perl within embedded C application
(1 answer)
Closed 9 years ago.
test.c:
int sum(int a, int b)
{
return (a+b);
}
test.pl:
# how do I call sum here?
use Inline C => <<'__END_OF_C__';
int sum(int a, int b)
{
return (a+b);
}
__END_OF_C__
say sum($x,$y);
You'll need XS. It's quite involved, so it's best to look at the official manual and tutorial pages for it:
http://perldoc.perl.org/perlxstut.html
http://perldoc.perl.org/perlxs.html
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Scala operator oddity
I'm very new to Scala and I read that in this language everything is an Object, cool. Also, if a method has only 1 argument, then we can omit the '.' and the parentesis '( )', that's ok.
So, if we take the following Scala example: 3 + 2, '3' and '2' are two Int Objects and '+' is a method, sounds ok. Then 3 + 2 is just the shorthand for 3.+(2), this looks very weird but I still get it.
Now let's compare the following blocks on code on the REPL:
scala> 3 + 2
res0: Int = 5
scala> 3.+(2)
res1: Double = 5.0
What's going on here? Why does the explicit syntax return a Double while the shorthand returns an Int??
3. is a Double. The lexer gets there first. Try (3).+(2).