Last active
August 29, 2015 14:13
-
-
Save oprypin/f063fe6706201504580d to your computer and use it in GitHub Desktop.
Replacement for negative indices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var s = "abcdef" | |
echo s[2 .. -3] # I suggest to remove this | |
# cd | |
proc `^`(x: int): int = assert false | |
template ex{`[]`(s, `..`(a, `^`(b)))}(s: expr{noSideEffect}, a, b: int): expr = | |
s[a .. s.len+b] | |
echo s[2 .. ^ -3] # Easy fix in code | |
# cd | |
# Or even this | |
proc `^-`(x: int): int = assert false | |
template ex{`[]`(s, `..`(a, `^-`(b)))}(s: expr{noSideEffect}, a, b: int): expr = | |
s[a .. s.len-b] | |
echo s[2 .. ^-3] | |
# cd | |
echo(@[1,2,3,4,5,6][2 .. ^-3]) | |
# @[3, 4] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment