Range Over Iterators in Co-array Fortran
On this page
Let’s start by translating the Go code provided to Co-Array Fortran. The original example utilized iterators, which are not a native feature in Fortran. However, we can approximate the behavior using arrays and custom loops.
Here is the translation:
Example: Range over Iterators
Starting with version 1.23, Go has added support for iterators, which lets us range over pretty much anything!
Let’s look at the List
type from the previous example again. In that example, we had an AllElements
method that returned a slice of all elements in the list. With Co-Array Fortran, we can manage this differently - as shown below.
Our implementation doesn’t use iterators directly but mimics their behavior by manually traversing and collecting elements in a list. This allows iteration over linked list elements in Fortran without native iterator support.
Explanation
The program involves a module list_module
that contains a definition for a linked list and its elements. Here’s a brief breakdown of the components:
Type Definitions:
element
: Represents an element of the linked list containing an integer value and a pointer to the next element.list
: Represents the linked list containing pointers to the head and tail elements. It also includes procedures to push elements into the list and retrieve all elements.
Procedures:
push
: Adds an element to the linked list.all_elements
: Returns an array of all elements in the list.
Main Program:
- In
main
, we create a list, push some integers into it, and then retrieve and print all elements.
- In
By replacing iterators with array-based traversal, we maintain the functional intent of iterating over elements.
Now that we can manage lists and iterate over elements in Fortran, let’s explore more about handling collections in this language.