Slices in Fortran

program slices_example
    implicit none
    
    ! In Fortran, arrays are the primary data structure for sequences.
    ! Unlike Go slices, Fortran arrays have fixed sizes.
    
    ! Declare an array of strings (character arrays in Fortran)
    character(len=1), dimension(:), allocatable :: s
    integer :: i
    
    ! Allocate memory for the array
    allocate(s(3))
    
    ! We can set and get elements just like with Go slices
    s(1) = 'a'
    s(2) = 'b'
    s(3) = 'c'
    print *, "set:", s
    print *, "get:", s(3)
    
    ! len() in Fortran returns the length of the array
    print *, "len:", size(s)
    
    ! To add elements, we need to reallocate the array
    call add_element(s, 'd')
    call add_element(s, 'e')
    call add_element(s, 'f')
    print *, "apd:", s
    
    ! Create a copy of the array
    call array_copy(s)
    
    ! Fortran allows array slicing similar to Go
    print *, "sl1:", s(3:5)
    print *, "sl2:", s(:5)
    print *, "sl3:", s(3:)
    
    ! We can declare and initialize an array in a single line
    character(len=1), dimension(3) :: t = ['g', 'h', 'i']
    print *, "dcl:", t
    
    ! Create a 2D array (similar to slice of slices in Go)
    call create_2d_array()
    
contains

    subroutine add_element(arr, elem)
        character(len=1), dimension(:), allocatable, intent(inout) :: arr
        character(len=1), intent(in) :: elem
        character(len=1), dimension(:), allocatable :: temp
        integer :: n
        
        n = size(arr)
        allocate(temp(n+1))
        temp(1:n) = arr
        temp(n+1) = elem
        call move_alloc(temp, arr)
    end subroutine add_element
    
    subroutine array_copy(arr)
        character(len=1), dimension(:), intent(in) :: arr
        character(len=1), dimension(:), allocatable :: c
        
        allocate(c(size(arr)))
        c = arr
        print *, "cpy:", c
    end subroutine array_copy
    
    subroutine create_2d_array()
        integer, dimension(:,:), allocatable :: twoD
        integer :: i, j, innerLen
        
        allocate(twoD(3, 3))
        do i = 1, 3
            innerLen = i
            do j = 1, innerLen
                twoD(i, j) = i + j - 2
            end do
        end do
        print *, "2d: ", twoD
    end subroutine create_2d_array
    
end program slices_example

This Fortran program demonstrates concepts similar to Go slices, adapted to Fortran’s array features. Here are some key differences and explanations:

  1. Fortran uses fixed-size arrays by default, but we can use allocatable arrays to mimic some of the dynamic behavior of Go slices.

  2. The allocate statement is used to dynamically allocate memory for arrays.

  3. To add elements to an array in Fortran, we need to reallocate the array. The add_element subroutine demonstrates this.

  4. Fortran has built-in array slicing capabilities similar to Go.

  5. The size function in Fortran is equivalent to len for Go slices.

  6. Fortran doesn’t have a direct equivalent to Go’s append. We simulate this behavior with the add_element subroutine.

  7. Multi-dimensional arrays in Fortran are more straightforward than slices of slices in Go.

  8. Fortran doesn’t have a built-in string type. We use character arrays instead.

This example shows how to work with arrays in Fortran, covering initialization, manipulation, slicing, and multi-dimensional arrays. While the syntax and some concepts differ from Go, the general ideas of working with sequences of data are similar.