I was looking inside the create_vlabel function and noted that to get the graph_name and label_name it is used graph_name = PG_GETARG_NAME(0) and label_name = PG_GETARG_NAME(1). Since these two variables are also passed as parameters, I was thinking that, if I wanted to add one more parameter to this function, then I would need to use PG_GETARG_NAME(2) to get this parameter and use it in the function's logic. Is my assumption correct or do I need to do more tweaks to do this?
You are correct, but you also need to change the function signature in the "age--1.2.0.sql" file, updating the arguments:
CREATE FUNCTION ag_catalog.create_vlabel(graph_name name, label_name name, type new_argument)
RETURNS void
LANGUAGE c
AS 'MODULE_PATHNAME';
Note that all arguments come as a "Datum" struct, and PG_GETARG_NAME automatically converts it to a "Name" struct. If you need an argument as int32, for example, you should use PG_GETARG_INT32(index_of_the_argument), for strings, PG_GETARG_CSTRING(n), and so on.
Yes, your assumption is correct. If you want to add an additional parameter to the create_vlabel function in PostgreSQL, you can retrieve the value of the third argument using PG_GETARG_NAME(2). Keep in mind that you may need to make additional modifications to the function's logic to handle the new parameter correctly.
The answers given by Fahad Zaheer and Marco Souza are correct, but you can also create a Variadic function, with which you could have n number of arguments but one drawback is that you would have to check the type yourself. You can find more information here. You can also check many Apache Age functions made this way e.g agtype_to_int2.
Related
In the KDB server, we have two functions defined as
q)t:{0N!x[`min]; 0N!x[`max];}
q).up.map:{[keyList; valueList] keyList!valueList}
The KDB server, does not allow to pass dict()!() as an argument directly to a function, rather one has to use .up.map.
Calling t function from kdb would be like
q)t[.up.map[`min`max;10 20]]
I want to call the t function from qpython sendSync() method passing another function .up.map[`min`max;10 20] as an argument to t.
Unfortunately, I cannot find a solution in the qptyhon doc - https://qpython.readthedocs.io/en/latest/qpython.html#qpython.qconnection.QConnection.sendSync
Error -
When I tried sendSync() method, below error is raised -
qpython.qtype.QException: b'['
The KDB server, does not allow to pass dict()!() as an argument directly to a function, rather one has to use .up.map.
May I know why this is so? It's not a bad idea to challenge the original design before looking for workarounds. If dictionary were allowed as its parameter, it could have been as simple as
params = QDictionary(qlist(numpy.array(["min", "max"], dtype=numpy.string_), qtype=QSYMBOL_LIST),
qlist(numpy.array([10, 20], dtype=numpy.int64), qtype=QLONG_LIST))
with qconnection.QConnection(host='localhost', port=5000) as q:
q.sendSync("t", params)
If you want to do what you can do in q console via qpython, it's actually also simple: you pass the same string over. Effectively it's the same mechanism as a q client passing a string via IPC to the server, where the string is parsed and evaluated. Here you need to convert the input to the given string format in your Python code, thus not as clean as the above (although it looks more verbose).
with qconnection.QConnection(host='localhost', port=5000) as q:
q.sendSync("t[.up.map[`min`max;10 20]]")
Maybe you can use a lambda for this. That way it's just the arguments that need be serialized:
q.sendSync("{t[.up.map[x;y]]}", qlist(["min", "max"], qtype=QSYMBOL_LIST), [10, 20])
If that's not permitted, you could create it as a named wrapper function on the kdb side, which could be.
Alternatively, you could format your call with arguments as a string. A bit hacky; but workable for simple input.
q.sendSync(f"t[.up.map[`{'`'.join(['min', 'max'])};{' '.join(['10', '20'])}]]")
func mapEachElement (inArray arr: [Int], withFunc aFunc: (Int))
Why would there be "inArray and then "arr"...what's the point?
Same for "withFunc and "aFunc", it makes it more code complicated and also messier to read, why are they even used?
inArray is external name which the caller of the function should use when passing parameters. arr is the internal name which the function implementer uses in the implementation to refer to the parameter. You don't have to supply external name.It makes it more readable. It is more like to make swift function names and parameters readable as Objective-C functions are.
I'm not sold on the issue of having different internal and external names either.
Having or not having external names makes sense to me.
object.BeepXTimes(6) doesn't need an external name for it's parameter because the function name implies the meaning of the parameter.
The rationale, I think, is that sometimes the naming of a function's external parameters is phrased to make sense to the caller in a sentence:
someView.animateInWithDuration(0.25, withTimingFunction: .easeIn)
The external name withTimingFunction makes the function call read like a sentence, but withTimingFunction makes a lousy internal parameter name. You would probably want your parameter name to be just timingFunction. Having separate internal and external parameter names lets you do that.
For me, though, the extra complexity this requires in the function definition doesn't seem worth it. The good news is that the default is to create parameters using the same name for both internal and external parameter names, so the standard syntax is simple and easy to read.
These are internal and external parameter names.
Just like in Obj-C, you might have
- (void)calculateFoo:(id)foo withBar:(id)bar ...
[object calculateFoo:var1 withBar:var2];
in Swift, we use
func calculateFoo(foo: AnyObject, withBar bar: AnyObject) ...
object.calculateFoo(var1, withBar: var2)
The internal names foo and bar are only accessible inside the function/method. The external names provide the argument labels which you use to call the function.
In your case, you need to use the internal name nibBundle. That's the one accessible inside the function body.
All is in the subject, really.
I fail to see what the difference in behavior is between those two methods for x:
// first version
Method m(ByRef x As whatever)
{
// play with x
}
// second version
Method m(Output x As whatever)
{
// play with x
}
There must be some reason why both those modifiers exist, however my "mastery" (uhm) of the language is not enough to understand the difference. I have tried and read the documentation, search it etc, to no avail so far.
So, what is the difference between those two argument modifiers?
Well those are just "prettifiers", they don't do much in terms of actual language behaviour, and only used to provide documentation. Idea is that arguments documented as ByRef provide both input and output, for example you can pass an array to be sorted, and Output arguments only provide output, for example list of errors. Output modifier was introduced later, and a lot of system code still use ByRef for both use cases.
If argument is actually passed by reference is only determined by method caller, and keyword doesn't really matter. You will call your method as ..m(.parameter) to pass variable by reference, and ..m(parameter) to pass variable by value.
func mapEachElement (inArray arr: [Int], withFunc aFunc: (Int))
Why would there be "inArray and then "arr"...what's the point?
Same for "withFunc and "aFunc", it makes it more code complicated and also messier to read, why are they even used?
inArray is external name which the caller of the function should use when passing parameters. arr is the internal name which the function implementer uses in the implementation to refer to the parameter. You don't have to supply external name.It makes it more readable. It is more like to make swift function names and parameters readable as Objective-C functions are.
I'm not sold on the issue of having different internal and external names either.
Having or not having external names makes sense to me.
object.BeepXTimes(6) doesn't need an external name for it's parameter because the function name implies the meaning of the parameter.
The rationale, I think, is that sometimes the naming of a function's external parameters is phrased to make sense to the caller in a sentence:
someView.animateInWithDuration(0.25, withTimingFunction: .easeIn)
The external name withTimingFunction makes the function call read like a sentence, but withTimingFunction makes a lousy internal parameter name. You would probably want your parameter name to be just timingFunction. Having separate internal and external parameter names lets you do that.
For me, though, the extra complexity this requires in the function definition doesn't seem worth it. The good news is that the default is to create parameters using the same name for both internal and external parameter names, so the standard syntax is simple and easy to read.
These are internal and external parameter names.
Just like in Obj-C, you might have
- (void)calculateFoo:(id)foo withBar:(id)bar ...
[object calculateFoo:var1 withBar:var2];
in Swift, we use
func calculateFoo(foo: AnyObject, withBar bar: AnyObject) ...
object.calculateFoo(var1, withBar: var2)
The internal names foo and bar are only accessible inside the function/method. The external names provide the argument labels which you use to call the function.
In your case, you need to use the internal name nibBundle. That's the one accessible inside the function body.
Is there an idiomatic way in Matlab to bind the value of an expression to the nth return value of another expression?
For example, say I want an array of indices corresponding to the maximum value of a number of vectors stored in a cell array. I can do that by
function I = max_index(varargin)
[~,I]=max(varargin{:});
cellfun(#max_index, my_data);
But this requires one to define a function (max_index) specific for each case one wants to select a particular return value in an expression. I can of course define a generic function that does what I want:
function y = nth_return(n,fun,varargin)
[vals{1:n}] = fun(varargin{:});
y = vals{n};
And call it like:
cellfun(#(x) nth_return(2,#max,x), my_data)
Adding such functions, however, makes code snippets less portable and harder to understand. Is there an idiomatic to achieve the same result without having to rely on the custom nth_return function?
This is as far as I know not possible in another way as with the solutions you mention. So just use the syntax:
[~,I]=max(var);
Or indeed create an extra function. But I would also suggest against this. Just write the extra line of code, in case you want to use the output in another function. I found two earlier questions on stackoverflow, which adress the same topic, and seem to confirm that this is not possible.
Skipping outputs with anonymous function in MATLAB
How to elegantly ignore some return values of a MATLAB function?
The reason why the ~ operator was added to MATLAB some versions ago was to prevent you from saving variables you do not need. If there would be a syntax like the one you are searching for, this would not have been necessary.