2. In Python if I want to return multiple variables from a proc I just do:
return var1, var2, var3, var4
Is there a similar way of doing it in Nim if the variables have DIFFERENT types ie two strings, one int and one float? Thank you for the answer! :-D
A follow up on question 1: let's say all I care for is that the input parameter can be iterated over. Is there a way of specifying it or am I too pythonic in my way of thinking?
or am I too pythonic in my way of thinking?
You probably are too "pythonic" in your way of thinking in getting used to static typing rather than dynamic typing. Static is safer when you get used to it. To pass an iterator as a paramenter:
proc inputiter[T](x: iterator(): T) =
for v in x(): echo v
To call it for instance with iterators made from a seq, array, and string; note that the iterator can be created in the call as follows:
inputiter(iterator(): int =
for e in @[1,2,3]: yield e)
inputiter(iterator(): int =
for e in [4,5,6]: yield e)
inputiter(iterator(): char =
for c in "abc": yield c)
You can iterate on an openarray, of course.
proc p(x: openarray[int]) =
for val in x:
echo val
p([1, 2, 3])
p(@[4, 5, 6])
You can iterate over an openarray, of course. But not on a tuple, as fields may have different types.
let a = (1, "two", @[3, 4])
for x in fields(a):
echo x
let's say all I care for is that the input parameter can be iterated over.
If I'm understanding you correctly, you want to iterate over your "list"? If so:
iterator myIterator(a: openArray[int]): int =
for x in a:
yield 10*x
let l = @[1, 2, 3]
for n in myIterator(l):
echo 5*n
prints:
50
100
150