In Ruby I can call methods with array elements used as positional parameters like this
method(fixed_arg1, fixed_arg2, *array_of_additional_args)
Here the "*" operator expands the array in place.
I'm trying to do the same in CoffeeScript, but haven't found a way. Specifically, I want to pass additional arguments in a call to a jQuery function
$('#my-element').toggle(true, *config.toggleOptions)
The syntax above does not work, obviously, and I'm looking for a way that does.
Try
$('#my-element').toggle(true, config.toggleOptions...)
You need to splat it.
fun(1,2,3,4,5)
fun = (first, second, rest...) ->
alert first # 1
alert second # 2
alert rest # [3, 4, 5 ]
Related
I am trying to follow a tutorial but the tutorial is in construct 2 (https://www.construct.net/en/tutorials/multiplayer-tutorial-chat-room-591/chat-events-3?vic=12)
In Construct 2, you would use: self.text & newline & Function.Param(0)
However, in Construct 3 it is showing an error on the Function.Param(0) part.
How would I write this?
In Construct 3 function, right click on the function name and add parameter as shown in image below. then you can use the parameter like how you use local variable
Hi! Could you please explain why the function runs twice in console?
def changeList(myList1):
myList2 = myList1.append(4)
print(myList2)
return
myList1 = [1,2,3]
changeList(myList1)
print (myList1)
The result in console:
None
[1, 2, 3, 4]
Does it mean function runs twice as "None" appears in the console?
tl;dr - the function is only running once -- there are two print statements producing output
The function is not running twice: indeed, it is only being run once. The output in the console is instead coming from the two calls to print() contained within your program: one inside the function changeList() and one outside the function (print(myList1)).
None is being printed to the console because the return statement within the function changeList() isn't returning anything - there is no value to return:
If an expression list is present, it is evaluated, else None is
substituted.
[Taken from the Python 3.6 Documentation]
Seeing as how the return statement isn't doing anything, you can safely remove it - the function will still end anyway.
Hope that helps you out!
The function is running only once. You are appending one item to list and tried to store in other list by just assigning list with one more item appending which returns None and assigns to myList2. So, the code is wrong because append() function return's None.
I think you wan't to do like this so, here is the correct code:
comment if is it solved your problem or not.
def changeList(myList1):
myList2=[]
myList2.extend(myList1)
myList2.append(4)
print(myList2)
return
myList1 = [1,2,3]
changeList(myList1)
print (myList1)
Because in the function definition of changeList, there is a print statement, and then another print statement after calling changeList. The function is only running once actually, but you simply have two separate print statements.
I am using scopt to parse command line arguments in scala. I want it to be able to parse options with more than one value. For instance, the range option, if specified, should take exactly two values.
--range 25 45
Coming, from python background, I am basically looking for a way to do the following with scopt instead of python's argparse:
parser.add_argument("--range", default=None, nargs=2, type=float,
metavar=('start', 'end'),
help=(" Foo bar start and stop "))
I dont think minOccurs and maxOccurs solves my problem exactly, nor the key:value example in its help.
Looking at the source code, this is not possible. The Read type class used has a member tuplesToRead, but it doesn't seem to be working when you force it to 2 instead of 1. You will have to make a feature request, I guess, or work around this by using --min 25 --max 45, or --range '25 45' with a custom Read instance that splits this string into two parts. As #roterl noted, this is not a standard way of parsing.
It should be ok if only your values are delimited with something else than a space...
--range 25-45
... although you need to split them manually. Parse it with something like:
opt[String]('r', "range").action { (x, c) =>
val rx = "([0-9]+)\\-([0-9]+)".r
val rx(from, to) = x
c.copy(from = from.toInt, to = to.toInt)
}
// ...
println(s" Got range ${parsedArgs.from}..${parsedArgs.to}")
When using WWW::Mechanize::Firefox to select an item, is it possible to iterate through a number of selectors that have the same name?
I use the following code:
my $un = $mech->selector('input.normal', single => 1);
The response is 2 elements found for CSS selector. Is there a way to use XPath or a better method, or is it possible to loop through the results?
Bonus point: typing into the inputs even though it is not in form elements (ie. uses JavaScript)
With the single option you have specified that there should be exactly one element that matches the selector. That is why you get an error message when it finds two matches.
The method will return a list of matches, and you can either use one => 1 in place of single => 1, which will throw van error if there isn't at least one match, or you can leave the option out altogether, when it will simply return all that it finds.
my #inputs = $mech->selector('input.normal')
will fill the array #inputs with a list of matching <input> elements, however many there are.
Module documentation contain these examples:
my $link = $mech->xpath('//a[id="clickme"]', one => 1);
# croaks if there is no link or more than one link found
my #para = $mech->xpath('//p');
# Collects all paragraphs
It seems to me that some of these should fail, but they all output what they are supposed to:
$, = "\n";
%test = (
"one" => ["one_0", "one_1"],
"two" => ["two_0", "two_1"]
);
print #{$test{"one"}}[0],
#{$test{"one"}}->[0],
$test{"two"}->[0],
$test{"one"}[1];
Why is this?
Your first example is different than the others. It is an array slice -- but in disguise since you are only asking for one item.
#{$test{"one"}}[0];
#{$test{"one"}}[1, 0]; # Another slice example.
Your other examples are alternative ways of de-referencing items within a multi-level data structure. However, the use of an array for de-referencing is deprecated (see perldiag).
#{$test{"one"}}->[0]; # Deprecated. Turn on 'use warnings'.
$test{"two"}->[0]; # Valid.
$test{"one"}[1]; # Valid but easier to type.
Regarding the last example, two subscripts sitting next to each other have an implied -> between them. See, for example, the discussion of the Arrow Rule in perlreftut.