How to create a Perl subroutine that accepts a block of code
Tag : perl , By : Thaweesak Suksuwan
Date : March 29 2020, 07:55 AM
To fix this issue Perl offers a system called subroutine prototypes that allow you to write user subs that get parsed in a way similar to the builtin functions. The builtins that you want to emulate are map, grep, or sort which can each take a block as their first argument. To do that with prototypes, you use sub name (&) {...} where the & tells perl that the first argument to the function is either a block (with or without sub), or a literal subroutine \&mysub. The (&) prototype specifies one and only one argument, if you need to pass multiple arguments after the code block, you could write it as (&@) which means, a code block followed by a list. sub higher_order_fn (&@) {
my $code = \&{shift @_}; # ensure we have something like CODE
for (@_) {
$code->($_);
}
}
higher_order_fn {$_ * 2} 1, 2, 3;
# or
higher_order_fn(sub {$_ * 2}, 1, 2, 3);
sub higher_order_method {
my $self = shift;
my $code = \&{shift @_};
...
$code->() for @_;
}
...
$obj->higher_order_method(sub {...}, 'some', 'more', 'args', 'here');
|
Javascript: How can I create a function that 1. accepts a global variable as a parameter 2. alters the value of that glo
Date : March 29 2020, 07:55 AM
will be helpful for those in need Since Javascript does not support true reference passing where you can change the original variable, you can't directly do what you're asking. The usual work-around is to wrap the global in an object. var myGlobal = {
counter: 0;
};
function increment(obj, prop) {
++obj[prop];
}
increment(myGlobal, "counter");
increment(myGlobal, "counter");
console.log(myGlobal.counter); // 2
var myGlobal = {
counter: 0;
};
function incrementCnt(obj) {
++obj.counter;
}
incrementCnt(myGlobal);
incrementCnt(myGlobal);
console.log(myGlobal.counter); // 2
var counter = 0;
function increment(val) {
return val + 10;
}
counter = increment(counter);
console.log(counter); // 10
|
Create a mysql function that accepts a result set as parameter?
Tag : mysql , By : Scott Everts
Date : March 29 2020, 07:55 AM
I wish this help you No. MySQL doesn't support defining a function with a resultset as an argument. Unfortunately, MySQL does not support Common Table Expression (CTE), and does not support Analytic functions. SELECT t.id
, (t.score-s.min_score)/(s.max_score-s.min_score) AS normalized_score
FROM (
-- original query here
SELECT id, score FROM ...
) t
CROSS
JOIN ( SELECT MIN(r.score) AS min_score
, MAX(r.score) AS max_score
FROM (
-- original query here
SELECT id, score FROM ...
) r
) s
ORDER BY t.id
SELECT q.id
, (q.score-s.min_score)/(s.max_score-s.min_score) AS normalized_score
FROM ( -- original query goes here
-- ------------------------
select t.id as id, count(*) as score
from tbl t
inner join tbl2 t2 on t.idx = t2.idx
where t2.role in (.....)
-- ------------------------
) q
CROSS
JOIN ( SELECT MIN(r.score) AS min_score
, MAX(r.score) AS max_score
FROM ( -- original query goes here
-- ------------------------
select t.id as id, count(*) as score
from tbl t
inner join tbl2 t2 on t.idx = t2.idx
where t2.role in (.....)
-- ------------------------
) r
) s
ORDER BY q.id
|
Create Arraylist as parameter of a function that accepts any type in Kotlin
Date : March 29 2020, 07:55 AM
help you fix your problem I am new to kotlin with Android Studio. I have written a function in kotlin which accepts an Arraylist as input and randomly shuffles it - , You need to make your function generic, like so: fun <T> randomize(array: ArrayList<T>) {
// do whatever you want to your `ArrayList`
}
val arr = ArrayList<Int>()
// ...
arr.shuffle()
|
C++: How can i create a function that accepts concatenated strings as parameter?
Date : March 29 2020, 07:55 AM
With these it helps It's impossible to do with the exact syntax you asked for unless you resort to macros. But if you don't mind replacing << with ,, then you can do following: #include <iostream>
#include <string>
#include <sstream>
void log_impl(const std::string &str)
{
std::cout << str;
}
template <typename ...P> void log(const P &... params)
{
std::stringstream stream;
(stream << ... << params);
// If you don't have C++17, use following instead of the above line:
// using dummy_array = int[];
// dummy_array{(void(stream << params), 0)..., 0};
log_impl(stream.str());
}
int main()
{
log("1", 2, '3'); // prints 123
}
|