Decrementing For() loop [duplicate] - for-loop

This question already has answers here:
How to iterate for loop in reverse order in swift?
(16 answers)
Closed 6 years ago.
What is the syntax of a decrementing for() loop?
I would like to do something like the following without getting a compilation error:
for ndx in 5...4 {
print("do something")
}

This prints the value of i in reverse order:
for i in (1...10).reversed() {
print(i)
}

Related

Is there any way to write two values in one name in python [duplicate]

This question already has answers here:
How to concatenate (join) items in a list to a single string
(11 answers)
Closed 3 years ago.
f = open("dict.txt","a")
f.write("Customer_Name: "+Name+"\nMovies: "+Movies_Name+"\nQuantity: "+str(quantity)+"\n")
f.close()
Movies_Name is a list which might contain more than one movies. Now I want to write the names of the movies individually instead of writing them as a list.
For me it has nothing to do with discord itself. It is just Python question.
So answer can be found here
By using ''.join
list1 = ['1', '2', '3']
str1 = ''.join(list1)
In your case:
f.write("Customer_Name: "+Name+"\nMovies: "+''.join(Movies_Name)+"\nQuantity: "+str(quantity)+"\n")

Ruby what does star prefix means in a loop variable? [duplicate]

This question already has answers here:
What does the (unary) * operator do in this Ruby code?
(3 answers)
Closed 5 years ago.
I've ecountered this today and I have no idea what it means. I tried to google it but I had no luck. Can someone explain this to me?
combinations.each do |combination|
messages = EventNotification.where('user_id = ? AND message_template = ?', *combination)
...
end
It's called the splat operator, and it unpacks an array into single method arguments. In this case, because the function presumably expects two more arguments after the format string, it's equivalent to:
messages = EventNotification.where('user_id = ? AND message_template = ?',
combination[0], combination[1])
In other languages, this feature is often called "varargs".

How to emulate returning arbitrary values from functions in bash? [duplicate]

This question already has answers here:
How to return a string value from a Bash function
(20 answers)
Closed 7 years ago.
Bash functions are just statements and they don't return values. Can anyone share best practice on writing functions that return values in bash?
Let's say I've a function that joins two strings:
function JoinStrings {
returnValue="$1$2"
}
How do I reuse this function in my code? How do I get returnValue to be returned to caller? Should I just use it as a global after this function call? That leads to many errors with global variables everywhere... How to achieve code reuse in bash?
You can use echo to "return" an arbitrary value:
join_strings() {
echo "$1$2"
}
cat="cat"
dog="dog"
catdog=$(join_strings $cat $dog)
echo $catdog
# catdog
Just output the value.
function JoinStrings {
echo "$1$2"
}
JoinStrings a b #ab
x=$(JoinStrings a bc) #x is now abc

How to use d3.max() with an associative array [duplicate]

This question already has answers here:
How to use d3.min and d3.max within a d3.json command
(3 answers)
Closed 8 years ago.
If I want the maximum value of an array, I can do this:
var data = [1,2,3,4,5];
console.log(d3.max(data)); //output 5
But if I try with an associative array, I get 'undefined':
var data = {'foo' : 4, 'baz' : 8};
console.log(d3.max(data)); //output undefined
How do I use d3.max() with an associative array?
Based on help from the comments to the question, this was the solution:
d3.max(d3.values(data));

Sass error for loop with animation duration [duplicate]

This question already has answers here:
Adding a unit to a number in Sass
(2 answers)
Closed 7 years ago.
I am having an issue with a Sass loop. I want to make the animation .1 seconds, .2 seconds etc...Here is the loop:
#for $i from 1 through 6 {
#elem span:nth-child(#{$i}) {
animation-delay: .#{$i}s;
}
}
The issue seems to be with the period here: .#{$i}s
If I remove it it works fine, but I get my animation in seconds and not fractions of a second as I would like it.
You could wrap the .#{$i}s by quotes and then use unquote() string function to fix the issue, as follows:
#for $i from 1 through 6 {
#elem span:nth-child(#{$i}) {
animation-delay: unquote(".#{$i}s");
}
}
From the doc:
unquote($string) Removes quotes from a string. If the string is
already unquoted, this will return it unmodified.

Resources