# Map

`pure elemental real function square(x)  real, intent(in) :: x  square = x**2end function square`
`pure recursive integer function fibonacci(n) result(fib)  integer, intent(in) :: n  if (n == 0) then    fib = 0  else if (n == 1) then    fib = 1  else    fib = fibonacci(n-1) + fibonacci(n-2)  end ifend function fibonacci`
`\$ gfortran map.f90 map.f90:7:27:pure elemental recursive integer function fibonacci(n) result(fib)                           1Error: ELEMENTAL attribute conflicts with RECURSIVE attribute at (1)`
`pure function map(f, x)  procedure(f_int) :: f ! Mapping function  integer, intent(in) :: x(:) ! Input array  integer :: map(size(x)), i  map = [(f(x(i)), i = 1, size(x))]end function map`
`interface  pure integer function f_int(x)    integer, intent(in) :: x  end function f_intend interface`

# Filter

`pure logical function even(n)  integer, intent(in) :: n  if (mod(n, 2) == 0) then    even = .true.  else    even = .false.  end ifend function even`
`result = pack(array, mask[, vector])`
`pure function filter(f, x)  procedure(f_int_logical) :: f ! An int -> logical function  integer, intent(in) :: x(:) ! Input array  integer, allocatable :: filter(:)  integer :: i  filter = pack(x, [(f(x(i)), i = 1, size(x))])end function filter`
`interface  pure logical function f_int_logical(x)    integer, intent(in) :: x  end function f_int_logicalend interface`
`result = filter(even, map(fibonacci, [(n, n = 1, 30)]))`

# Reduce

## The Fortran 2018 reduce

`reduce(array, operation[, mask, identity, ordered])reduce(array, operation, dim[, mask, identity, ordered])`

## Implementing your own reduce function

`result = reduce(add, filter(even, map(fibonacci, x)), 0)`
`pure recursive integer function reduce(f, x, start) result(res)  procedure(f2_int) :: f  integer, intent(in) :: x(:), start  if (size(x) < 1) then    res = start  else    res = reduce(f, x(2:), f(start, x(1)))  end ifend function reduce`
`pure integer function f2_int(x, y)  integer, intent(in) :: x, yend function f2_int`
`pure recursive integer function reduce_right(f, x, start) result(res)  procedure(f2_int) :: f  integer, intent(in) :: x(:), start  if (size(x) < 1) then    res = start  else    res = f(x(1), reduce_right(f, x(2:), start))  end ifend function reduce_right`
`integer, allocatable :: x(:)x = [(n, n = 1, 30)]print *, reduce(add, filter(even, map(fibonacci, x)), 0)`

# Summary

Written by

Written by

## Milan Curcic

#### I’m a scientist, founder, and author. 