Open jacobwilliams opened 2 years ago
Yes, Julia also has this feature. I think this can be done, it would bring Fortran closer to Python and Julia.
I like the idea. I think maybe we should use something different than pointer assignment for the syntax though. This is because
->
) leaves open the possibility that it could be overridden for derived types, giving the possibility of custom iterators. We don't need to think out all the technicalities of how such custom iterators would be implemented right now, but we can at least leave the possibility open for the future.
Another option is a new .in.
operator.
I have wanted the same thing and support the proposal, but note that Impure elemental subroutines can simulate for loops over collections
how about this syntax?
do [x, y] .in. [[10, 23, 1], ['a', 'b', 'cc']]
do_something(x, y)
end do
The feature is called foreach loop, it is already discussed in https://github.com/j3-fortran/fortran_proposals/issues/116 with the following syntax close to the one by @waynelapierre:
do element in array
write(*,*) element
end do
ZIP
could be an intrinsic function, which would take two 1D arrays of size n
and create a 2D array of size 2xn (due to Fortran having column major order. The foreach loop would then iterate over the columns of this array:
do col in ZIP(array1, array2)
write(*,*) col(1), col(2)
end do
But using ZIP
the way you want to would require unpacking variables from arrays, which is discussed in https://github.com/j3-fortran/fortran_proposals/issues/186. It would add a syntax like this:
real :: a(2) = [1,2]
real :: x, y
...
[x,y] = a
and thus allow for what you had in mind by implicit application of array unpacking:
do [x, y] in ZIP(array1, array2)
write(*,*) x, y
end do
I would be very much in favor of both features. They increase readability of code by a lot.
I like this syntax:
...
vec = [1,2,3,4]
for (element) in vec do
print *, element
end do
Also:
...
vec = [1,2,3,4]
for (element, index) in vec do
print *, element, index
end do
Using the "possible" zip() function:
...
vec1 = [1,2,3,4]
vec2 = [-1,-2,-3,-4]
for (v1, v2, index) in zip(vec1,vec2) do
print *, index, v1, v2
end do
It looks readable to me, and also preserves the "do ... end do" syntax.
Not sure if this has already been proposed or not. But I keep wishing Fortran had something like the python
for
style loops like this:or with
zip
:of course, the fortran way would be something like that:
What do people think?
I don't know what the syntax would be...