Why doesn't the new hat-operator index from the C# 8 array-slicing feature start at 0?
C# 8.0 introduces a convenient way to slice arrays - see official C# 8.0 blogpost.
The syntax to access the last element of an array is
int value = { 10, 11, 12, 13 };
int a = value[^1]; // 13
int b = value[^2]; // 12
I'm wondering why the indexing for accessing the elements backwards starts at 1 instead of 0? Is there a technical reason for this?
c# arrays c#-8.0
add a comment |
C# 8.0 introduces a convenient way to slice arrays - see official C# 8.0 blogpost.
The syntax to access the last element of an array is
int value = { 10, 11, 12, 13 };
int a = value[^1]; // 13
int b = value[^2]; // 12
I'm wondering why the indexing for accessing the elements backwards starts at 1 instead of 0? Is there a technical reason for this?
c# arrays c#-8.0
8
Note that C++ ranges are also[beginInclusive, endExclusive)
. It is a common convention.
– bommelding
Jan 8 at 13:16
3
@Sinatr: Based on that blog post, the syntax to return everything would bevalue[0..^0]
, since the ending index is exclusive (which is how most other languages work, too). Also, conveniently,value[^i..^0]
will give you the lasti
items.
– BlueRaja - Danny Pflughoeft
Jan 8 at 18:13
1
@bommelding: C++rbegin()
somewhat disagrees with that notion -- the first item out of that range isn't the one-beyond-the-end either. ;-)
– DevSolar
Jan 9 at 12:37
Oh cool, this looks like the equivalent of negative indexing in python:value[-1] # 13
– coldspeed
Jan 9 at 18:45
@coldspeed And in Ruby the same as Python. I'm guessing they both borrowed this convention from Perl.
– Wayne Conrad
Jan 9 at 20:15
add a comment |
C# 8.0 introduces a convenient way to slice arrays - see official C# 8.0 blogpost.
The syntax to access the last element of an array is
int value = { 10, 11, 12, 13 };
int a = value[^1]; // 13
int b = value[^2]; // 12
I'm wondering why the indexing for accessing the elements backwards starts at 1 instead of 0? Is there a technical reason for this?
c# arrays c#-8.0
C# 8.0 introduces a convenient way to slice arrays - see official C# 8.0 blogpost.
The syntax to access the last element of an array is
int value = { 10, 11, 12, 13 };
int a = value[^1]; // 13
int b = value[^2]; // 12
I'm wondering why the indexing for accessing the elements backwards starts at 1 instead of 0? Is there a technical reason for this?
c# arrays c#-8.0
c# arrays c#-8.0
edited Jan 9 at 8:23
Martin Zikmund
24k63460
24k63460
asked Jan 8 at 13:04
Michael PittinoMichael Pittino
6382714
6382714
8
Note that C++ ranges are also[beginInclusive, endExclusive)
. It is a common convention.
– bommelding
Jan 8 at 13:16
3
@Sinatr: Based on that blog post, the syntax to return everything would bevalue[0..^0]
, since the ending index is exclusive (which is how most other languages work, too). Also, conveniently,value[^i..^0]
will give you the lasti
items.
– BlueRaja - Danny Pflughoeft
Jan 8 at 18:13
1
@bommelding: C++rbegin()
somewhat disagrees with that notion -- the first item out of that range isn't the one-beyond-the-end either. ;-)
– DevSolar
Jan 9 at 12:37
Oh cool, this looks like the equivalent of negative indexing in python:value[-1] # 13
– coldspeed
Jan 9 at 18:45
@coldspeed And in Ruby the same as Python. I'm guessing they both borrowed this convention from Perl.
– Wayne Conrad
Jan 9 at 20:15
add a comment |
8
Note that C++ ranges are also[beginInclusive, endExclusive)
. It is a common convention.
– bommelding
Jan 8 at 13:16
3
@Sinatr: Based on that blog post, the syntax to return everything would bevalue[0..^0]
, since the ending index is exclusive (which is how most other languages work, too). Also, conveniently,value[^i..^0]
will give you the lasti
items.
– BlueRaja - Danny Pflughoeft
Jan 8 at 18:13
1
@bommelding: C++rbegin()
somewhat disagrees with that notion -- the first item out of that range isn't the one-beyond-the-end either. ;-)
– DevSolar
Jan 9 at 12:37
Oh cool, this looks like the equivalent of negative indexing in python:value[-1] # 13
– coldspeed
Jan 9 at 18:45
@coldspeed And in Ruby the same as Python. I'm guessing they both borrowed this convention from Perl.
– Wayne Conrad
Jan 9 at 20:15
8
8
Note that C++ ranges are also
[beginInclusive, endExclusive)
. It is a common convention.– bommelding
Jan 8 at 13:16
Note that C++ ranges are also
[beginInclusive, endExclusive)
. It is a common convention.– bommelding
Jan 8 at 13:16
3
3
@Sinatr: Based on that blog post, the syntax to return everything would be
value[0..^0]
, since the ending index is exclusive (which is how most other languages work, too). Also, conveniently, value[^i..^0]
will give you the last i
items.– BlueRaja - Danny Pflughoeft
Jan 8 at 18:13
@Sinatr: Based on that blog post, the syntax to return everything would be
value[0..^0]
, since the ending index is exclusive (which is how most other languages work, too). Also, conveniently, value[^i..^0]
will give you the last i
items.– BlueRaja - Danny Pflughoeft
Jan 8 at 18:13
1
1
@bommelding: C++
rbegin()
somewhat disagrees with that notion -- the first item out of that range isn't the one-beyond-the-end either. ;-)– DevSolar
Jan 9 at 12:37
@bommelding: C++
rbegin()
somewhat disagrees with that notion -- the first item out of that range isn't the one-beyond-the-end either. ;-)– DevSolar
Jan 9 at 12:37
Oh cool, this looks like the equivalent of negative indexing in python:
value[-1] # 13
– coldspeed
Jan 9 at 18:45
Oh cool, this looks like the equivalent of negative indexing in python:
value[-1] # 13
– coldspeed
Jan 9 at 18:45
@coldspeed And in Ruby the same as Python. I'm guessing they both borrowed this convention from Perl.
– Wayne Conrad
Jan 9 at 20:15
@coldspeed And in Ruby the same as Python. I'm guessing they both borrowed this convention from Perl.
– Wayne Conrad
Jan 9 at 20:15
add a comment |
1 Answer
1
active
oldest
votes
Official answer
For better visibility, here is a comment from Mads Torgersen explaining this design decision from the C# 8 blogpost:
We decided to follow Python when it comes to the from-beginning and from-end arithmetic.
0
designates the first element (as always), and^0
the “length’th” element, i.e. the one right off the end. That way you get a simple relationship, where an elements position from beginning plus its position from end equals the length. thex
in^x
is what you would have subtracted from the length if you’d done the math yourself.
Why not use minus (
-
) instead of the new hat (^
) operator? This primarily has to do with ranges. Again in keeping with Python and most of the industry, we want our ranges to be inclusive at the beginning, exclusive at the end. What is the index you pass to say that a range should go all the way to the end? In C# the answer is simple:x..^0
goes fromx
to the end. In Python there is no explicit index you can give:-0
doesn’t work, because it is equal to0
, the first element! So in Python you have to leave the end index off completely to express a range that goes to the end:x..
. If the end of the range is computed, then you need to remember to have special logic in case it comes out to0
. As inx..-y
, wherey
was computed and came out to0
. This is a common nuisance and source of bugs.
Finally, note that indices and ranges are first class types in .NET/C#. Their behavior is not tied to what they are applied to, or even to being used in an indexer. You can totally define your own indexer that takes Index and another one that takes
Range
– and we’re going to add such indexers to e.g.Span
. But you can also have methods that take ranges, for instance.
My answer
I think this is to match the classic syntax we are used to:
value[^1] == value[value.Length - 1]
If it used 0, it would be confusing when the two syntaxes were used side-by-side. This way it has lower cognitive load.
Other languages like Python also use the same convention.
11
Minor correction to Mads comment: you do not have to leave off the end index completely in python. You can useNone
in place of a number:[0,1,2,3,4][2:None] == [2,3,4]
. But, yes you cannot use an integer as end index (without computing the length obviously).
– Giacomo Alzetta
Jan 8 at 17:01
4
Wait.. what's wrong withx..
? That seems fine and I've never had problem with the python[3:]
syntax.
– mowwwalker
Jan 8 at 21:27
2
@mowwwalker nothing wrong. I seems thatx..
syntax will be supported too. It's in example of ranges proposal
– Mariusz Pawelski
Jan 9 at 0:22
7
@mowwwalker - isn't that already covered in the quote? "So in Python ... If the end of the range is computed, then you need to remember to have special logic in case it comes out to 0"
– Damien_The_Unbeliever
Jan 9 at 8:26
3
It's good to see they're not repeating Python's mistake with the -0 thing. Handling that special case is a huge hassle and way too easy to forget.
– user2357112
Jan 9 at 19:14
|
show 2 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54092458%2fwhy-doesnt-the-new-hat-operator-index-from-the-c-sharp-8-array-slicing-feature%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Official answer
For better visibility, here is a comment from Mads Torgersen explaining this design decision from the C# 8 blogpost:
We decided to follow Python when it comes to the from-beginning and from-end arithmetic.
0
designates the first element (as always), and^0
the “length’th” element, i.e. the one right off the end. That way you get a simple relationship, where an elements position from beginning plus its position from end equals the length. thex
in^x
is what you would have subtracted from the length if you’d done the math yourself.
Why not use minus (
-
) instead of the new hat (^
) operator? This primarily has to do with ranges. Again in keeping with Python and most of the industry, we want our ranges to be inclusive at the beginning, exclusive at the end. What is the index you pass to say that a range should go all the way to the end? In C# the answer is simple:x..^0
goes fromx
to the end. In Python there is no explicit index you can give:-0
doesn’t work, because it is equal to0
, the first element! So in Python you have to leave the end index off completely to express a range that goes to the end:x..
. If the end of the range is computed, then you need to remember to have special logic in case it comes out to0
. As inx..-y
, wherey
was computed and came out to0
. This is a common nuisance and source of bugs.
Finally, note that indices and ranges are first class types in .NET/C#. Their behavior is not tied to what they are applied to, or even to being used in an indexer. You can totally define your own indexer that takes Index and another one that takes
Range
– and we’re going to add such indexers to e.g.Span
. But you can also have methods that take ranges, for instance.
My answer
I think this is to match the classic syntax we are used to:
value[^1] == value[value.Length - 1]
If it used 0, it would be confusing when the two syntaxes were used side-by-side. This way it has lower cognitive load.
Other languages like Python also use the same convention.
11
Minor correction to Mads comment: you do not have to leave off the end index completely in python. You can useNone
in place of a number:[0,1,2,3,4][2:None] == [2,3,4]
. But, yes you cannot use an integer as end index (without computing the length obviously).
– Giacomo Alzetta
Jan 8 at 17:01
4
Wait.. what's wrong withx..
? That seems fine and I've never had problem with the python[3:]
syntax.
– mowwwalker
Jan 8 at 21:27
2
@mowwwalker nothing wrong. I seems thatx..
syntax will be supported too. It's in example of ranges proposal
– Mariusz Pawelski
Jan 9 at 0:22
7
@mowwwalker - isn't that already covered in the quote? "So in Python ... If the end of the range is computed, then you need to remember to have special logic in case it comes out to 0"
– Damien_The_Unbeliever
Jan 9 at 8:26
3
It's good to see they're not repeating Python's mistake with the -0 thing. Handling that special case is a huge hassle and way too easy to forget.
– user2357112
Jan 9 at 19:14
|
show 2 more comments
Official answer
For better visibility, here is a comment from Mads Torgersen explaining this design decision from the C# 8 blogpost:
We decided to follow Python when it comes to the from-beginning and from-end arithmetic.
0
designates the first element (as always), and^0
the “length’th” element, i.e. the one right off the end. That way you get a simple relationship, where an elements position from beginning plus its position from end equals the length. thex
in^x
is what you would have subtracted from the length if you’d done the math yourself.
Why not use minus (
-
) instead of the new hat (^
) operator? This primarily has to do with ranges. Again in keeping with Python and most of the industry, we want our ranges to be inclusive at the beginning, exclusive at the end. What is the index you pass to say that a range should go all the way to the end? In C# the answer is simple:x..^0
goes fromx
to the end. In Python there is no explicit index you can give:-0
doesn’t work, because it is equal to0
, the first element! So in Python you have to leave the end index off completely to express a range that goes to the end:x..
. If the end of the range is computed, then you need to remember to have special logic in case it comes out to0
. As inx..-y
, wherey
was computed and came out to0
. This is a common nuisance and source of bugs.
Finally, note that indices and ranges are first class types in .NET/C#. Their behavior is not tied to what they are applied to, or even to being used in an indexer. You can totally define your own indexer that takes Index and another one that takes
Range
– and we’re going to add such indexers to e.g.Span
. But you can also have methods that take ranges, for instance.
My answer
I think this is to match the classic syntax we are used to:
value[^1] == value[value.Length - 1]
If it used 0, it would be confusing when the two syntaxes were used side-by-side. This way it has lower cognitive load.
Other languages like Python also use the same convention.
11
Minor correction to Mads comment: you do not have to leave off the end index completely in python. You can useNone
in place of a number:[0,1,2,3,4][2:None] == [2,3,4]
. But, yes you cannot use an integer as end index (without computing the length obviously).
– Giacomo Alzetta
Jan 8 at 17:01
4
Wait.. what's wrong withx..
? That seems fine and I've never had problem with the python[3:]
syntax.
– mowwwalker
Jan 8 at 21:27
2
@mowwwalker nothing wrong. I seems thatx..
syntax will be supported too. It's in example of ranges proposal
– Mariusz Pawelski
Jan 9 at 0:22
7
@mowwwalker - isn't that already covered in the quote? "So in Python ... If the end of the range is computed, then you need to remember to have special logic in case it comes out to 0"
– Damien_The_Unbeliever
Jan 9 at 8:26
3
It's good to see they're not repeating Python's mistake with the -0 thing. Handling that special case is a huge hassle and way too easy to forget.
– user2357112
Jan 9 at 19:14
|
show 2 more comments
Official answer
For better visibility, here is a comment from Mads Torgersen explaining this design decision from the C# 8 blogpost:
We decided to follow Python when it comes to the from-beginning and from-end arithmetic.
0
designates the first element (as always), and^0
the “length’th” element, i.e. the one right off the end. That way you get a simple relationship, where an elements position from beginning plus its position from end equals the length. thex
in^x
is what you would have subtracted from the length if you’d done the math yourself.
Why not use minus (
-
) instead of the new hat (^
) operator? This primarily has to do with ranges. Again in keeping with Python and most of the industry, we want our ranges to be inclusive at the beginning, exclusive at the end. What is the index you pass to say that a range should go all the way to the end? In C# the answer is simple:x..^0
goes fromx
to the end. In Python there is no explicit index you can give:-0
doesn’t work, because it is equal to0
, the first element! So in Python you have to leave the end index off completely to express a range that goes to the end:x..
. If the end of the range is computed, then you need to remember to have special logic in case it comes out to0
. As inx..-y
, wherey
was computed and came out to0
. This is a common nuisance and source of bugs.
Finally, note that indices and ranges are first class types in .NET/C#. Their behavior is not tied to what they are applied to, or even to being used in an indexer. You can totally define your own indexer that takes Index and another one that takes
Range
– and we’re going to add such indexers to e.g.Span
. But you can also have methods that take ranges, for instance.
My answer
I think this is to match the classic syntax we are used to:
value[^1] == value[value.Length - 1]
If it used 0, it would be confusing when the two syntaxes were used side-by-side. This way it has lower cognitive load.
Other languages like Python also use the same convention.
Official answer
For better visibility, here is a comment from Mads Torgersen explaining this design decision from the C# 8 blogpost:
We decided to follow Python when it comes to the from-beginning and from-end arithmetic.
0
designates the first element (as always), and^0
the “length’th” element, i.e. the one right off the end. That way you get a simple relationship, where an elements position from beginning plus its position from end equals the length. thex
in^x
is what you would have subtracted from the length if you’d done the math yourself.
Why not use minus (
-
) instead of the new hat (^
) operator? This primarily has to do with ranges. Again in keeping with Python and most of the industry, we want our ranges to be inclusive at the beginning, exclusive at the end. What is the index you pass to say that a range should go all the way to the end? In C# the answer is simple:x..^0
goes fromx
to the end. In Python there is no explicit index you can give:-0
doesn’t work, because it is equal to0
, the first element! So in Python you have to leave the end index off completely to express a range that goes to the end:x..
. If the end of the range is computed, then you need to remember to have special logic in case it comes out to0
. As inx..-y
, wherey
was computed and came out to0
. This is a common nuisance and source of bugs.
Finally, note that indices and ranges are first class types in .NET/C#. Their behavior is not tied to what they are applied to, or even to being used in an indexer. You can totally define your own indexer that takes Index and another one that takes
Range
– and we’re going to add such indexers to e.g.Span
. But you can also have methods that take ranges, for instance.
My answer
I think this is to match the classic syntax we are used to:
value[^1] == value[value.Length - 1]
If it used 0, it would be confusing when the two syntaxes were used side-by-side. This way it has lower cognitive load.
Other languages like Python also use the same convention.
edited Jan 9 at 8:20
answered Jan 8 at 13:07
Martin ZikmundMartin Zikmund
24k63460
24k63460
11
Minor correction to Mads comment: you do not have to leave off the end index completely in python. You can useNone
in place of a number:[0,1,2,3,4][2:None] == [2,3,4]
. But, yes you cannot use an integer as end index (without computing the length obviously).
– Giacomo Alzetta
Jan 8 at 17:01
4
Wait.. what's wrong withx..
? That seems fine and I've never had problem with the python[3:]
syntax.
– mowwwalker
Jan 8 at 21:27
2
@mowwwalker nothing wrong. I seems thatx..
syntax will be supported too. It's in example of ranges proposal
– Mariusz Pawelski
Jan 9 at 0:22
7
@mowwwalker - isn't that already covered in the quote? "So in Python ... If the end of the range is computed, then you need to remember to have special logic in case it comes out to 0"
– Damien_The_Unbeliever
Jan 9 at 8:26
3
It's good to see they're not repeating Python's mistake with the -0 thing. Handling that special case is a huge hassle and way too easy to forget.
– user2357112
Jan 9 at 19:14
|
show 2 more comments
11
Minor correction to Mads comment: you do not have to leave off the end index completely in python. You can useNone
in place of a number:[0,1,2,3,4][2:None] == [2,3,4]
. But, yes you cannot use an integer as end index (without computing the length obviously).
– Giacomo Alzetta
Jan 8 at 17:01
4
Wait.. what's wrong withx..
? That seems fine and I've never had problem with the python[3:]
syntax.
– mowwwalker
Jan 8 at 21:27
2
@mowwwalker nothing wrong. I seems thatx..
syntax will be supported too. It's in example of ranges proposal
– Mariusz Pawelski
Jan 9 at 0:22
7
@mowwwalker - isn't that already covered in the quote? "So in Python ... If the end of the range is computed, then you need to remember to have special logic in case it comes out to 0"
– Damien_The_Unbeliever
Jan 9 at 8:26
3
It's good to see they're not repeating Python's mistake with the -0 thing. Handling that special case is a huge hassle and way too easy to forget.
– user2357112
Jan 9 at 19:14
11
11
Minor correction to Mads comment: you do not have to leave off the end index completely in python. You can use
None
in place of a number: [0,1,2,3,4][2:None] == [2,3,4]
. But, yes you cannot use an integer as end index (without computing the length obviously).– Giacomo Alzetta
Jan 8 at 17:01
Minor correction to Mads comment: you do not have to leave off the end index completely in python. You can use
None
in place of a number: [0,1,2,3,4][2:None] == [2,3,4]
. But, yes you cannot use an integer as end index (without computing the length obviously).– Giacomo Alzetta
Jan 8 at 17:01
4
4
Wait.. what's wrong with
x..
? That seems fine and I've never had problem with the python [3:]
syntax.– mowwwalker
Jan 8 at 21:27
Wait.. what's wrong with
x..
? That seems fine and I've never had problem with the python [3:]
syntax.– mowwwalker
Jan 8 at 21:27
2
2
@mowwwalker nothing wrong. I seems that
x..
syntax will be supported too. It's in example of ranges proposal– Mariusz Pawelski
Jan 9 at 0:22
@mowwwalker nothing wrong. I seems that
x..
syntax will be supported too. It's in example of ranges proposal– Mariusz Pawelski
Jan 9 at 0:22
7
7
@mowwwalker - isn't that already covered in the quote? "So in Python ... If the end of the range is computed, then you need to remember to have special logic in case it comes out to 0"
– Damien_The_Unbeliever
Jan 9 at 8:26
@mowwwalker - isn't that already covered in the quote? "So in Python ... If the end of the range is computed, then you need to remember to have special logic in case it comes out to 0"
– Damien_The_Unbeliever
Jan 9 at 8:26
3
3
It's good to see they're not repeating Python's mistake with the -0 thing. Handling that special case is a huge hassle and way too easy to forget.
– user2357112
Jan 9 at 19:14
It's good to see they're not repeating Python's mistake with the -0 thing. Handling that special case is a huge hassle and way too easy to forget.
– user2357112
Jan 9 at 19:14
|
show 2 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54092458%2fwhy-doesnt-the-new-hat-operator-index-from-the-c-sharp-8-array-slicing-feature%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
8
Note that C++ ranges are also
[beginInclusive, endExclusive)
. It is a common convention.– bommelding
Jan 8 at 13:16
3
@Sinatr: Based on that blog post, the syntax to return everything would be
value[0..^0]
, since the ending index is exclusive (which is how most other languages work, too). Also, conveniently,value[^i..^0]
will give you the lasti
items.– BlueRaja - Danny Pflughoeft
Jan 8 at 18:13
1
@bommelding: C++
rbegin()
somewhat disagrees with that notion -- the first item out of that range isn't the one-beyond-the-end either. ;-)– DevSolar
Jan 9 at 12:37
Oh cool, this looks like the equivalent of negative indexing in python:
value[-1] # 13
– coldspeed
Jan 9 at 18:45
@coldspeed And in Ruby the same as Python. I'm guessing they both borrowed this convention from Perl.
– Wayne Conrad
Jan 9 at 20:15