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
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:
Replace c style for-loop in Swift 2.2.1
(1 answer)
Closed 6 years ago.
I wrote the following code in swift(2.2):
for var i = 2; sqrt(Double(num)) >= Double(i); i += 1 {.....}
However, I keep getting a warning message which reads "c-style for statement is deprecated and will be removed in future version of swift".
So, what is the proper way of writing the same loop is "Swift style"? Casting the value of num as double gives me error with "Swift style". Any suggestions?
Thank you.
You can refactor your deprecated C-style for loop using the for-in construct
for i in 2...Int(sqrt(Double(num))) { }
However if you really want to go essential try defining this operator to find the square root of an int rounded down to the closest int.
prefix operator √ {}
prefix func √ (number: Int) -> Int {
return Int(sqrt(Double(number)))
}
Now you can write your for loop this way
for i in 2...(√num) { }
The For loop in Swift 2.2 is re-designed for use in iterating over a sequence, such as ranges of numbers, items in an array, or characters in a string. The condition in your For loop is not easily converted into sequence or range, so it would be best to re-write it as a While loop.
This question already has answers here:
What does an ampersand (&) mean in the Swift language?
(5 answers)
Closed 7 years ago.
I'm new to Swift and I'm trying to learn the concept of 'inout' keyword. I saw this code in "the swift programming language 2.1". My question is, why is there a "&" in "swap(&someInt, &anotherInt)". What does it represent? What is its function?
func swapTwoInts(inout a: Int, inout _ b: Int){
let temporaryA = a
a = b
b = temporaryA
}
var someInt = 3
var anotherInt = 107
swap(&someInt, &anotherInt)
print("someInt is now \(someInt) and anotherInt is now \(anotherInt)")
Just like passing by reference in C++, the ampersands in the calling bit of the code just tell the compiler that you give permission to function swapTwoInts to change both someInt and anotherInt. If you had not put the ampersands there, the code would not compile.
This question already has answers here:
What does this caret ^ syntax, with void on either side mean? [duplicate]
(4 answers)
Closed 9 years ago.
I keep seeing lines of code with ^{ some code } in it... I thought that maybe it allowed to run a function inline similar to a lambda function. But I can not find any documentation on it. Could someone please enlighten me?
It is a block.
See the documentation.
Tis a block!
http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/bxGettingStarted.html#//apple_ref/doc/uid/TP40007502-CH7-SW1
To steal Apple's example:
int multiplier = 7;
int (^myBlock)(int) = ^(int num) {
return num * multiplier;
};
printf("%d", myBlock(3));
// prints "21"
Yes, a block indeed...
Here is a tutorial for people who are beginners to blocks!
As Apple states in their documentation:
You use the ^ operator to declare a block variable and to indicate the
beginning of a block literal. The body of the block itself is
contained within {}, as shown in this example (as usual with C, ;
indicates the end of the statement):
int multiplier = 7;
int (^myBlock)(int) = ^(int num) {
return num * multiplier;
};
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).