function-calls

Is it possible to emulate a function using your own data type?

∥☆過路亽.° 提交于 2019-12-01 03:06:28
Is it possible to emulate a function with your own data type with some GHC extension? What I want to do is e.g. (imaginary syntax) data MyFunc = MyFunc String (Int->Int) instance (Int->Int) MyFunc where ($) (MyFunc _ f) i = f i inc = MyFunc "increment" (1+) test = inc 1 I.e. data that carries some meta-information with it and can be pattern matched, but which can still be called like a regular function. Now, I know that I could define my own infix operator like $$ and call inc $$ 1 , but being able to use the regular function call syntax would be very useful in embedded DSLs. Yes, it can be

Sequencing of function parameter destruction

不羁岁月 提交于 2019-12-01 02:28:19
According to C++14 [expr.call]/4: The lifetime of a parameter ends when the function in which it is defined returns. This seems to imply that a parameter's destructor must run before the code which called the function goes on to use the function's return value. However, this code shows differently: #include <iostream> struct G { G(int): moved(0) { std::cout << "G(int)\n"; } G(G&&): moved(1) { std::cout << "G(G&&)\n"; } ~G() { std::cout << (moved ? "~G(G&&)\n" : "~G()\n"); } int moved; }; struct F { F(int) { std::cout << "F(int)\n"; } ~F() { std::cout << "~F()\n"; } }; int func(G gparm) { std:

Sequencing of function parameter destruction

南楼画角 提交于 2019-11-30 22:05:34
问题 According to C++14 [expr.call]/4: The lifetime of a parameter ends when the function in which it is defined returns. This seems to imply that a parameter's destructor must run before the code which called the function goes on to use the function's return value. However, this code shows differently: #include <iostream> struct G { G(int): moved(0) { std::cout << "G(int)\n"; } G(G&&): moved(1) { std::cout << "G(G&&)\n"; } ~G() { std::cout << (moved ? "~G(G&&)\n" : "~G()\n"); } int moved; };

Passing functions which have multiple return values as arguments in Python

对着背影说爱祢 提交于 2019-11-30 11:25:18
So, Python functions can return multiple values. It struck me that it would be convenient (though a bit less readable) if the following were possible. a = [[1,2],[3,4]] def cord(): return 1, 1 def printa(y,x): print a[y][x] printa(cord()) ...but it's not. I'm aware that you can do the same thing by dumping both return values into temporary variables, but it doesn't seem as elegant. I could also rewrite the last line as "printa(cord()[0], cord()[1])", but that would execute cord() twice. Is there an elegant, efficient way to do this? Or should I just see that quote about premature optimization

Python - Can I access the object who call me?

社会主义新天地 提交于 2019-11-30 10:24:20
If I have this: class A: def callFunction(self, obj): obj.otherFunction() class B: def callFunction(self, obj): obj.otherFunction() class C: def otherFunction(self): # here I wan't to have acces to the instance of A or B who call me. ... # in main or other object (not matter where) a = A() b = B() c = C() a.callFunction(c) # How 'c' know that is called by an instance of A... b.callFunction(c) # ... or B Despite the design or other issues, this is only a question of an inquiring mind. Note: This has to be done without changing otherFunction signature If this is for debugging purposes you can

Passing missing argument from function to function in R

流过昼夜 提交于 2019-11-30 08:35:27
I’ve learned that it’s common practice to use optional arguments in function and check them with missing() (e.g. as discussed in SO 22024082 ) In this example round0 is the optional argument (I know, round0 could be defined as logical). foo = function(a, round0) { a = a * pi if(!missing(round0)) round(a) else a } But what if I call this function from another function, how can I pass “missing”? bar = function(b) { if(b > 10) round1=T foo(b, round1) } If b < 10 then round1 in bar() is not defined, but is passed to foo anyway. If I modify foo(): foo = function(a, round0) { a = a * pi print

What is a callback function and how do I use it with OOP

☆樱花仙子☆ 提交于 2019-11-30 06:59:13
I want to use the php simple HTML DOM parser to grab the image, title, date, and description from each article on a page full of articles. When looking at the API I notice it has a set_callback which Sets a callback function. However im not sure what this does or how I would use it? In one of the examples its used to call a function which strips out some stuff, im wondering if you have to use this to call all functions? I guess im wondering why I use this, and what does it do as I have never come across a callback function before! Here's a basic callback function example: <?php function

Calling a user defined function in jQuery

帅比萌擦擦* 提交于 2019-11-29 19:49:12
I am trying to call a user defined function in jQuery: $(document).ready(function() { $('#btnSun').click(function() { myFunction(); }); $.fn.myFunction = function() { alert('hi'); } }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="btnSun">Say hello!</button> I tried the following as well: $(document).ready(function() { $('#btnSun').click(function() { myFunction(); }); }); function myFunction() { alert('hi'); } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="btnSun">Say hello!</button>

How to call VBA-function for specifying columns in SQL TRANSFORM query?

一个人想着一个人 提交于 2019-11-29 14:42:14
Here is my query: PARAMETERS ... TRANSFORM ... SELECT ... ... PIVOT Mytable.type In ("Other","Info"); This is a cross query and I need to set all the column headings with this row: PIVOT Mytable.type In ("Other","Info") and now I have hard-coded the headings, Other and Info . But I want to do this dynamically. So what I want to do is to call a vba-function that returns all the headings I need. Something like this: PIVOT Mytable.type In (myVbaFunction()); So my question is: How to call a vba-function inside the sql-query? Christian Specht Yes, it is possible. However, I don't think it's

How to force matlab to call a regular function rather than class method when they are overloaded?

扶醉桌前 提交于 2019-11-29 14:07:10
Assume I have an object X of class MyClass . MyClass has a method compute , and when I call U = compute(X,...) , matlab automatically calls the class method. However, what I actually want is to call another function also called compute whose parameters start with a MyClass object though. How do I force matlab to call this regular function rather than go into the class method? Bee There is no way to do this without making some changes either to the function's name or location. If you check Matlab's function precedence order , methods always run before normal external functions. Your only