1

I'm trying to understand the concept of functional programming. I'm just using javascript here to understand the concepts of it

Usually we would count to 10 in this way :

for(var i=0;i<10;i++){
 console.log(i+1)
}

is this the Functional Programming Equivalent?

var a = 1;
var n = 10;

function counter(){
  console.log(a);
  if(a<n){
    counter(++a);
  }
}

counter();

5
  • 5
    ++a is not something you would do with functional programming Oct 19, 2017 at 18:45
  • You've got an example of recursion there. If you want to understand FP, you might want to try a more functional language like Clojure. Oct 19, 2017 at 18:47
  • Haskell is a pure functional language.
    – user1636522
    Oct 19, 2017 at 18:48
  • A little help : stackoverflow.com/a/37242112/1636522.
    – user1636522
    Oct 19, 2017 at 18:49
  • No, replacing a loop statement with a function that neither takes an argument nor returns a value isn't functional at all. You merely mimic imperative statements with functions. FP is a completely different approach and it takes a lot of time to comprehend it.
    – user6445533
    Oct 19, 2017 at 19:19

3 Answers 3

1

You could use recursion to get a (more) functional programming solution:

(function counter(a, n){
    console.log(a);
    a < n && counter(a+1, n);
})(1, 10);

0

Here's my take on it.

Array(n).fill() is used to generate a range of n element to iterate over.

.forEach(...) iterates through the elements and does the work based on element's index and the b ("base") value provided from the outside.

const counter = (n, b) => Array(n).fill().forEach((_, index) => console.log(index + b));

counter(10, 1);

-1

You can't change mutable data in functional programming. Think of it as doing math in school when you have to solve for a variable. No matter what you do, that variable will always have the same value. In your example, you are changing the value of that variable. To do functional programming, that variable needs to have the same value when that function ends as it did in the beginning.

var a = 1;
var n = 10;

counter(a,n);

function counter(num, limit){
  if (num > limit) 
    return;
  console.log(num);
  counter(num+1, limit);
}

Would be considered functional programming as a and n would still have the same value and thus you have not changed mutable data.

Functional Programming

3
  • "You can't change mutable data" is contradictory. I would say "in functional programming, data is immutable".
    – user1636522
    Oct 19, 2017 at 19:25
  • Yes, but a var is a mutable variable in JavaScript. So even though you can change it, you need to treat it like it's immutable.
    – user8753212
    Oct 19, 2017 at 19:34
  • 1
    Still, your sentence is ambiguous. Keep in mind that you are talking to newbies.
    – user1636522
    Oct 19, 2017 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.