Functional programming: are the cards sequential? Consequences of closure

I will illustrate Julia:

Suppose I have a function counter (), which is a closure.

function mycl()                                                                                                                                                                                                    
        state=0                                                                                                                                                                                                    
        function counter()                                                                                                                                                                                         
                state=state+1                                                                                                                                                                                      
        end                                                                                                                                                                                                        
end  

Now suppose I create mycoutner function:

mycounter=mycl()

and now display this function in an array of length 10, with all elements equal to 1.

map(x->x+mycounter(),ones(1:10))

The output is as follows:

julia> map(x->x+mycounter(),ones(1:10))
10-element Array{Int64,1}:
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11

It seems that the function is applied sequentially over the displayed array.

In the end, I try to avoid cycles, but with a local state variable of the state of mutation of the circuit, I need it to be applied sequentially. It seems so, is it an accepted standard? (did not test the equivalent version of R using * apply). And is it really “functional” because the local state variable mutates?

+4
3

Julia map (-) , . , , . , map, - , tmap " " - , , .

+5

, , , .

, . R7RS :" [t] proc list "(. 51, ), , " " (, ). OCaml manual , List.map" f a1,..., an [f a1; ...; f an] , f ", , let a::l -> let r = f a in r :: map f l.

+1

, , " ", ​​ , . ., , haskell .

Julia, , for . for , , , , , map.

+1

Source: https://habr.com/ru/post/1648711/


All Articles