How to convert a fragment to a sequence?

I would like to specify the sequence directly from the slice (rather than iterating through the slice and adding each element individually to the sequence). I tried several different ways, but the obvious ones don't seem to work.

var 
    x = newSeq(1..n)
    y: seq[int] = @[1..n]
    z: seq[int] = 1..n

The only thing I managed to get was to import the list from future

var x: seq[int] = lc[x | (x <- 1..n), int]

I can’t find a way in the documents so that this can be done, which is not related to importing experimental material from the future or overloading the sequence constructor.

+4
source share
2 answers

https://nim-lang.org/docs/sequtils.html#toSeq.t,untyped

import sequtils
var x = toSeq 1..n

seq:

proc toSeq2[T](s: Slice[T]): seq[T] =
  result = @[]
  for x in s.a .. s.b:
    result.add x
+5

, :

proc sliceToSeq[T](s: Slice[T]): seq[T] =
  result = newSeq[T](ord(s.b) - ord(s.a) + 1)
  var i = 0
  for x in s.a .. s.b:
    result[i] = x
    inc(i)
+3

All Articles