4 ms·
First element found at offset 0. Like ruler. Closed and right-open intervals in Ruby: (1..12).to_a #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] (1
by sergeykish 6y ago
First element found at offset 0. Like ruler.
Closed and right-open intervals in Ruby:
(1..12).to_a
#=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
(1...13).to_a
#=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
- erhk 6y agoI see myself losing hours on this typo
- ZephyrBlu 6y agoI couldn't tell the difference for quite a few seconds there.
- bregma 6y agoWe need a language that goes one better, like a cross between Python and Ruby that uses two spaces for closed ranges and three spaces for open.
- TuringTest 6y agoHow about using different symbols? |-1..12-|.to_a ## e.g. months in a year #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] /1..\13.to_a ## e.g. hours from 1h (1AM) to 13h (1PM) #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] It requires compiler support, but hey, you said a new language.
- sergeykish 6y agoI see children losing hours on range(1, 12) in Python, have you noticed error? Unsafe defaults introduced to reuse function as for i in range(len) # 0, 1, ..., len - 1
- stabbles 6y agoAnd Rust does 1..13 and 1..=12
- tragomaskhalos 6y agoCiting Rust is cheating - we know that it does everything in the most elegant manner. A bit like deriding the other kids in the class because they're not as good as the star pupil :)
- daef 6y agoAnd nim does 1..<13 and 1..12 which is why switching between rust and nim is a real pain in the buttox
- ziml77 6y agoI feel like it's nim that made the bad choice here. Not because one meaning of .. naturally makes more sense than another but because it breaks the expectations set by other languages.
- Jtsummers 6y agoBut Nim isn't unique. Ada is, now, approaching 40 years old since publication. It uses closed intervals: 1..12 = [1,12] With no syntactic notion for half-open intervals.
- ziml77 6y agoI didn't realize that the syntax dated back to Ada. Though interestingly it doesn't feel off to me in the context of a 1-based index language.