This question already has answers here:
Numerical range iterators in boost?
(3 answers)
Closed 5 years ago.
If I have this:
for (auto iSong = 1; iSong <= iMaxSongNumber; iSong++)
Can I use the new for range approach?
I understand that for containers they need a begin and end method for them to work. But if we have literal max values?
There isn't a built-in mechanism to do this: range-based for works on something for which begin and end can be called.
I wrote a blog post about how to do this: https://www.justsoftwaresolutions.co.uk/cplusplus/generating_sequences.html
Basically, you need to create a "virtual container" with iterators that update the count.
Related
This question already has answers here:
Chaining methods using Symbol#to_proc shorthand in Ruby?
(4 answers)
Closed 5 years ago.
For example, I have enum.map(&:join).map(&:to_i). Is there a syntax in Ruby where I can write something like this: enum.map(&:join:to_i) in order to avoid iterating through the array twice using & operator?
You could use a block (with no &) to iterate just once:
enum.map { |e| e.join.to_i }
This question already has answers here:
F# Functions vs. Values
(4 answers)
Closed 7 years ago.
I have F# code something like this:
let ran = new System.Random()
let makeVal = ran.NextDouble()
Why when I use makeVal do I get the same random number on every call within one run of the app session (i.e. it's not a seed issue).
Values in F# are immutable by default. So makeVal will not change after the first binding. To get different random values you should call ran.NextDouble() again.
For example use the function:
let makeVal() = ran.NextDouble()
This question already has answers here:
Type casting in for-in loop
(7 answers)
Closed 8 years ago.
I would like to specify the type for item in the for in loop below.
for item in items {
}
Currently it is AnyObject but I would like to set it to NSString.
Yes, this is a common problem. The solution is to cast:
for item in items as [NSString] {
It is perhaps a little surprising that you have to cast the array (items) rather than explicitly declaring the type of the loop variable (item). But that's the syntax, and you'll quickly get used to it.
This question already has answers here:
How to declare 8-bit unsigned integer in ruby?
(2 answers)
Closed 9 years ago.
I want a variable to hold a number that can't be assigned a negative number, so that myvar = -1 would just end up being 0. I can easily make my own class to do this, but does ruby already come with one?
No, you'll need to handle validation of the value on your own. Here's more info on the different ruby numerical types: http://www.techotopia.com/index.php/Ruby_Number_Classes_and_Conversions
You'll need a custom class to do it. Since Ruby is dynamically typed, you can't prevent a particular variable from holding a negative integer, or even a string for that matter.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is there a reason that we cannot iterate on “reverse Range” in ruby?
This works like magic.
for i in 1..10
...
end
Isn't it only intuitive that this backward for loop should work as well?
for i in 10..1
...
end
If there is some syntactical reason why this shouldn't work, I feel like ruby has to be changed to allow it. It's just intuitive to write backward for loop that way.
try something like
10.downto(1) { |i| ... }
1..10 is of class Range, not directly linked with any loop constructs. And there are no numbers that are both bigger than 10 and smaller than 1, therefore the range 10..1 is empty.
PS I don't recall when was the last time I wrote a for loop in ruby. Maybe something from http://www.ruby-doc.org/core-1.9.2/Enumerable.html would serve you better?