Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My concern about no classification by Procedural/Imperative and Functional programming Paradigm #119

Open
ken-okabe opened this issue Jul 29, 2018 · 4 comments
Labels
enhancement New feature or request

Comments

@ken-okabe
Copy link

ken-okabe commented Jul 29, 2018

Hi, this is a great project. Thanks.
I have a concern that I would like to share.

For instance, looking at /algorithms/math/factorial which is one of the most basic math topic:
https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/factorial

I found 2 implementations:

factorial.js

export default function factorial(number) {
  let result = 1;

  for (let i = 2; i <= number; i += 1) {
    result *= i;
  }

  return result;
}

factorialRecursive.js

export default function factorialRecursive(number) {
  return number > 1 ? number * factorialRecursive(number - 1) : 1;
}

factorial.js is a code of Procedural/Imperative programming style, and uses mutable variables.

factorialRecursive.js is recursive, and can be said functional programming style, immutable. Alghouth this is a typical implementation which I can see everywhere, in terms of "Big O notations". this is rather anti-pattern.

A better or I would say, a proper way is,

factorialFunctional.js

//[...Array(5).keys()]
//-> [ 0, 1, 2, 3 ,4 ]

const natural = n => {
    const arr0 = [...Array(n + 1).keys()];
    const [first, ...arr] = arr0;
    return arr;
};

console.log(
    natural(5) //[ 1, 2, 3, 4, 5 ]
);

function factorialFunctional(number) {
    const list = number < 1
        ? [1]
        : natural(number)
    const multiply = (a, b) => a * b;
    return list.reduce(multiply);
}

console.log(
    factorialFunctional(5)//120
);

This is as efficient as the factorial.js in terms of "Big O notations", and immutable.

I think when algorithms are presented, it's significantly important to clarify what kind of programming paradigm the algorithms are based on.

Currently, it seems the contributions are updated randomly without formal classification for them, and I think it's a good idea to show a guideline in the README that to clarify which paradigm every algorithm belongs to. In this manner, a contributors notice "oh, here, there is no Functional or Imperative pattern yet, so I will add.."

Thanks.

@trekhleb
Copy link
Owner

Thank you for your suggestion @kenokabe.

Interesting implementation of factorial!

What programming paradigms do you mean? I mean how do you suggest to split algorithms in main README? Is it something from https://en.wikipedia.org/wiki/Programming_paradigm like imperative and declarative? Or more detailed like object-oriented, procedural and functional?

@trekhleb trekhleb added the enhancement New feature or request label Jul 30, 2018
@ken-okabe
Copy link
Author

ken-okabe commented Jul 31, 2018

Thanks for your response and the great project again @trekhleb .
As you suggest, the classification is another issue to be considered. It's often not easy.

An idea to classify things, especially these days, is not to "split" but to "tag" the algorithms.
One algorithm can be tagged as "#imperative #procedual #object-oriented", another can be "#functional #declarative #lazy-evaluation". Tagging is a good method to express classifications that cross over.

Again, I really think it's not great especially for learners that we put algorithms for a certain target in the same box of mixed programming paradigms without any classifications.

@trekhleb
Copy link
Owner

@kenokabe I like the idea with tagging! It may be one of the next enhancements to do here in this repo. Thank you for suggestion!

@georgebullock
Copy link

Is this going to happen? For a learning project, I think it would be valuable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants