I study Erlang, tried to write something like an analogue of the built-in "convolution" function lists:foldl , in javascript:

 foldl = (fn, x, list, acc) => { try { if(!acc) { acc = 0; } if(!list.length) { return acc; } return foldl(fn, x, list, acc + fn(list.shift(), x)); } catch(e) { return 0; } }; console.log( foldl((x, y) => x + y, 2, [1,2,3]) ); // 12 

In this example, the "level of abstraction increases", the purity of the function is maintained. Is it acceptable to write (or try to write) code completely in a functional style, like in Erlang, in javascript? What could this ultimately lead to?

Closed due to the fact that it is necessary to reformulate the question so that it was possible to give an objectively correct answer by the participants of D-side , Grundy , aleksandr barakin , user194374, Dmitriy Simushev July 28, 16 at 10:42 .

The question gives rise to endless debates and discussions based not on knowledge, but on opinions. To get an answer, rephrase your question so that it can be given an unambiguously correct answer, or delete the question altogether. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Functions in js are described more simply - recursion is valid. => there is too much. - nick_n_a
  • one
    you made a reduce function - Grundy
  • one
    Can. React / Redux architecture even requires it in some places. - Nofate
  • one
    By the way, this code is not functional; the initial variable list is changing - Grundy
  • 3
    Why not? Write as you like. For javascript there are lots of libraries for programming in a functional style - for example, Bacon.js. And even translators from functional languages ​​- Clojurescript, for example. - Sergey Gornostaev

0