Last active
August 29, 2015 14:10
-
-
Save singularperturbation/476ebc90432a9c2c7822 to your computer and use it in GitHub Desktop.
Creating new sequence from a slice
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
#import macros | |
## TODO: Convert to a template/macro | |
#dumpTree: | |
# proc newSeq[T](s: Slice): seq[T] = | |
# var retvar = newSeq[T](len=(s.b-s.a)+1) | |
# for i in s.a..s.b: | |
# retvar[i-(s.a)] =i | |
# return retvar | |
# | |
# var input = newSeq[int](-8..8) | |
# var squares = newSeq[int](len=input.len) | |
# | |
# squares = input.map do (x:int) -> int: | |
# x*x | |
# | |
# echo($squares) | |
# | |
template newSeqSlice(t:typedesc,s: Slice): expr = | |
block: | |
var retVar = newSeq[t](len=(s.b-s.a)+1) | |
for i in s.a..s.b: | |
retvar[i-(s.a)] = i | |
retvar | |
# TODO: Handle a .. b where b < a properly | |
var input = newSeqSlice(int,0 .. 100) | |
var input2 = newSeqSlice(int,-40..40) | |
var squares = newSeq[int](len=input.len) | |
var squares2 = newSeq[int](len=input2.len) | |
squares = input.map do (x: int) -> int: | |
x*x | |
squares2 = input2.map do (x: int) -> int: | |
x*x | |
echo($squares) | |
echo($squares2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment