Map Function in Swift - swift

I am working with the map function in Swift. I am seeing use of "$0" and do not know what it means. Is the "$0" a pointer to the current element of the array?
stringArray = newStringArray.map({"\($0)New"})

I wouldn't use the word pointer, but I think you have the right idea. When you use map here, you're taking an array and applying a function to every element in that array. Here, the function takes in one argument (a string) and outputs another string. $0 refers to the first argument to the function you're calling which, in this case, is the only argument.
The anonymous sort of functions you pass to map are called closures. Looking at Apple's official documentation on closures might be helpful! Here's a link: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html

Related

Using the Python C API, how can I write a function that accepts any number of arguments, including none at all?

METH_VARARGS requires at least one argument; METH_NOARGS doesn't seem to let me pass any at all.
How can I define a function build() that can be called as either build() or build(True)/build(False)?
Calling a METH_VARARGS function with no arguments results in:
TypeError: function takes exactly 1 argument (0 given)
I was thinking about the problem wrong. It's not the definition, but rather the parsing that rose my TypeError.
To prevent it, I just had to use "|O" instead of "O" in PyArg_ParseTuple!

Spread syntax in function call in Reason

In Javascript you can use the spread syntax in a function call like this:
console.log(...[1,2,3]);
Is there an equivalent in Reason? I tried the following:
let bound = (number, lower, upper) => {
max(lower, min(upper, number));
};
let parameters = (1,0,20);
bound(...parameters) |> Js.log;
But this gives an unknown syntax error:
Try reason snippet
There's not. Reason is a statically typed language, and lists are dynamically-sized and homogenous. It would be of very limited use, and not at all obvious how it would deal with too few or too many arguments. If you want to pass a list, you should just accept a list and deal with it appropriately, as a separate function if desired.
You could of course use a tuple instead, which is fixed-size and heterogenous, but I don't see a use-case for that either, since you might as well just call the function directly then.
For JavaScript FFI there is however the bs.splice attribute, which will allow you to apply a variable number of arguments to a js function using an array. But it needs to be called with an array literal, not just any array.

What is the difference between ByRef and Output method argument modifiers?

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.

Perl references. How do we know it is one?

I am new to Perl and reading about references.
I can not understand how doe one know if the variable he work on is a reference.
For instance if I understand correctly, this:
$b = $a could be assigning scalars or references. How do we know which is it?
In C or C++ we would know via the function signature (*a or &a of **a). But in Perl there is no signature of parameters.
So how do we know in code what is a reference and what is not? Or if it is a reference to scalar or array or hash or another reference?
Perl has a ref that you can use for that:
Returns a non-empty string if EXPR is a reference, the empty string otherwise. [...]
The string returned (if non-empty) will tell you the type of object the reference references.
You're asking the wrong question.
While there is a function called ref and another called reftype, these are not functions you should ever need to use.
It's bad to check the type of variables, because there's no way to effectively know without actually using it as intended due to overloading and magic.
For example, say you designed a function that accepts a reference or a string. That would be a bad design because an object that overloads stringification is both.
A good interface would use context to differentiate the arguments. For example, it could differentiate based on the number of arguments,
foo($point_obj)
-vs-
foo(x => $x, y => $y)
based on the value of other arguments,
foo(fh => $fh)
-vs-
foo(str => $file_contents)
or based on the choice of function called
foo_from_fh($fh)
-vs-
foo($file_contents)
So the answer is: You know it's a reference because your documentation instructs the caller of your function to pass a reference. If you got passed something other than a reference and it's used as a reference, the caller will get a strict error for their error.
The ref function is what you're looking for. Documentation is available at http://perldoc.perl.org/functions/ref.html
ref EXPR
Returns a non-empty string if EXPR is a reference, the empty string otherwise. If EXPR is
not specified, $_ will be used. The value returned depends on the type of thing the
reference is a reference to...

Arrayfun syntax and usage with class method

Here's a line of code that's giving me trouble.
arrayfun(#(x)container.nodelist(x).config(#a_func_handle,0),2:6);
Container is a class with one of its properties being an object array of nodes, and that array is called nodelist.
Each node has a function called config that is used to initialize it. Config expects one input, one of which is a handle to a function. The function handle I'm passing needs a constant passed along with it, which is represented by the 0.
In this case, I want to configure the nodes at positions 2 through 6 in nodelist with a specific function, so I thought to use arrayfun instead of a for loop.
Unfortunately, Matlab barfs with "too many inputs" for function config. What am I writing wrong? Is this example clear?
I figured it out. What I ended up doing was using nested anonymous functions, like so:
arrayfun(#(y)y.config(#(x)(configSlave(x,0))),exp.pico_list(2:6));
If I've understood correctly, config is a method of the objects contained within your nodelist array. In which case, in the usual MATLAB manner, the object on which you're invoking the method gets passed as the first argument. For example, you might need to write the config method like this:
function config(obj, fcnHandle, value)
obj.FunctionHandle = fcnHandle;
obj.Value = value;
end