Function calling variable output number - function

I've found myself trying to interface a custom class with builtin functions, and I've encoutered a situation I could only solve with eval, I'd like a "cleaner" way.
Basically, the builtin function is defined as varargout=blabla(varargin)
I defined an overriden function in the custom class, as varargout=blabla(varargin). The function looks like:
function varargout=blabla(varargin)
varargout=blabla(function_of_varargin)
end
The function of varargin transforms it from the custom class to the builtin clas.
But it doesn't work as-is: Basically when the builtin function is called inside the overriden function, it sees only one output parameter (varargout), even if the custom overriden function is called with more than one output parameter.
I solved it by basically calling this :
[varargout{1},varargout{2},...,varargout{nargout}]=blabla(function_of_varargin)
Constructing the LHS with a loop and eval-ing.

Have you tried this:
[varargout{1:nargout}] = blabla(varargin{:})
?

Related

Which find() function is called when " using namespace std" added to code with while using STL set containers?

I've read that STL Set container has a specialized find() function which was more efficient than std::find(). My question is that which find() function would be called if I'd use using namespace std?
std::find is a free function, std::set::find is a member function of set, the way to call them is different.
Refer to: http://www.cplusplus.com/reference/set/set/find/
https://www.cplusplus.com/reference/algorithm/find/
I will fill in the details once I reach home, but it should be obvious.

Reference to "this" changed inside Sortables Class

I was testing Jakobs patch on the Sortables Class and this line this.reset() gave me a Uncaught TypeError: undefined is not a function.
I don't understand why since the Class has a method reset.
So my solution was to a var self = this; inside the same end: method (here), and called self.reset(); in the same line as I had this.reset(); before. Worked good. Why?
Then just to check (I suspected already) I did a console.log(this == self) and gave false.
Why does using self work but not this?
Fiddle
In javascript the this keyword change accordingly with the execution context
in global code this refer to the global object
inside eval the scope is the same as the calling context one, if no context provided then is the same as above
in all the case below if the this argument passed to .bind .call or .apply is not an object (or null) this will be the global object
when using a function which has been binded to a specific object using .bind then this refers to the this argument passed to bind, the function is now permabinded.
when running a function the context is provided from the caller, if before the function call operator () there is a dot(.) or a [] operator then this refers to the part on the left of such operator, unless the function is permabinded to something else or we are using .call or .apply if so this refers to the this argument unless the function was previously permabinded;
if before the function call operator () there neither the . nor the [] operators then this will refer to the global object (unless the function stores the result of the .bind function)
when running a constructor function (basically when using new) this refers to the object we are creating
now when using the use strict directive things changes a bit, mostly instead of the global object when the context is not given this will be null, but not in all the cases.
I rarely use "use strict" so I just suggest to try it by yourself when in need.
now, what happens when a function is cached inside a variable like this:
var cache = 'A.foo'
if that you lose the context in which the original function was stored, so in this case foo will not be anymore a property on the instance A and when you run it using
cache()
the context will be evaluated using the rules I wrote above in this case the this will refer to the global object.
The semantics of "this" in Javascript are not what is expected by OO programmers. The symbol "this" refers to the dynamic/runtime calling context, not the lexicographic context. For example, if you have an object A with "method" and then do B.method = A.method; B.method(); then the context is now B and that is what this will point to. The difference becomes very apparent in "handler" type situations where the calling context is usually the object with the handler installed.
Your solution using self is sound.
kentaromiura's answer is absolutely right.
That said, mootools provides function.bind() as a way to decide what this will refer inside of your function. this means that if you simply do this :
var destroy = function () {
`bind() [...]
this.reset();
}.bind(this);
it will work as you intended (that is, this will be the same inside of destroy() and outside).
Now, a lot of coders will balk at fiddling with the context, with good reason as it is very difficult to read and maintain. But here you have it and I think bind() is a very nifty trick of mootools.

Why the pointer to function gets value 0x00000000?

Why proc which is a function pointer gets null value?
EDITED:
My guess would be that the library you loaded doesn't export a function called "StartHook".
If the library is written in C++, which it looks like it is, the function name will be mangled based on its argument types (e.g, to something like _Z9StartHookv). Wrap the definition in extern "C" { ... } to prevent this.
NULL is the documented return value for when the function fails. To get the reason, call GetLastError and look it up here.

tkinter command to call function from another Python script

I am having a few issues, calling Python functions defined in another script using tkinter. I would prefer to have a separate script for my functions that the GUI uses when needed. At the moment I am doing it like this.
ttk.Button(mainframe, text="1", command=one).grid(column=1, row=1, sticky=NW)
def one():
code_entry.insert(END,"1")
The above calls the command one on a button click, which will print the character one in a entry field with the GUI. I thought I could create a separate script to hold my functions and call them like this:
ttk.Button(mainframe, text="1", command=functions.one()).grid(column=1, row=1, sticky=NW)
And then simply add an import statement at the top of my GUI, like below:
import functions
This doesn't work and looking for some advice on how to approach this.
You didn't specify any error messages, but it's most likely that you're doing fuctions.one() - actually calling the one() function of that module before the Button is created. It's simply fixed by removing the () part - when you specify a function without (), you are passing a reference of the function object.
Also keep in mind the scope of the code_entry variable - if you were using it as a module level global before (or function local, if one() was inside the same function as your ttk.Button call), it won't be available when you move it to a new namespace without code_entry.
To solve this you should pass code_entry as a parameter to the callback without calling one() at first. The usual approach for this is creating a lambda - essentially creating a function that works on the same scope of the original one(), having access to variables like code_entry, but also calling a function in a different module.
ttk.Button(mainframe, text="1", command=lambda: functions.one(code_entry))
Note that this is basically the same as:
def some_anonymous_function():
functions.one(code_entry)
ttk.Button(mainframe, text="1", command=some_anonymous_function)
Both examples create a function object and pass that object as reference - the functions.one() call of the lambda is actually inside the body of the lambda function, to be called later by tkinter.
Of course you also have to redefine one() to accept this new parameter:
def one(code_entry):
code_entry.insert(END,"1")

When calling a function in matlab, how can I output the result to a matrix in the original file?

Basically I would like to call a function that I have written, and because of the amount of results, I would like the function to output its solution into a matrix that gets passed to the program that called it.
You define the output of a function in the function declaration at the top of your script:
function [output] = myFunction(input)
All you need to do is define the output variable somewhere in your script.
The confusing part (to me) was that you need to put the output variables in both your main program and your function definition. So in your main program you have:
[out1,out2,out3] = function_name(in1,in2);
and in your function definition, you have:
function [out1,out2,out3] = function_name(in1,in2).
The variables don't have to have the same name, but they need to be oriented similarly, so that you can then pass the outputs back to the main program.