My concern about no classification by Procedural/Imperative and Functional programming Paradigm
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
- Issue type
- Documentation
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- javascript
- Domain
- documentation
Research direction
Start by reading the README and the factorial example under /algorithms/math/factorial, then review the issue discussion for any settled direction. Done means documenting an agreed classification approach for algorithm programming paradigms and clarifying how contributors should apply it.
Written by the indexing model from the issue text.
Description
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.
- Dominant language
- JavaScript
- Stars
- 197k
- Forks
- 31k
- PR merge metrics
- No merged PRs in 30d
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from trekhleb/javascript-algorithms
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
trekhleb/javascript-algorithms#2102 · 1 comment ·
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
trekhleb/javascript-algorithms#2085 · 6 comments · 1 reaction ·
-
LinkedList methods. Open
Difficulty 3/5 1-2 days Newbie friendliness 35/100
trekhleb/javascript-algorithms#2065 · 1 comment ·
-
Difficulty 3/5 1-2 days Newbie friendliness 55/100
trekhleb/javascript-algorithms#2057 · 1 comment ·
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
trekhleb/javascript-algorithms#2056 ·
All issues in trekhleb/javascript-algorithms
Similar issues
-
code-quality refactoring
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
github/gh-aw-firewall#8816 ·
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
langchain-ai/deepagents#6450 ·
-
optimization optimization:agents-md-curator
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
githubnext/gh-aw-cao#13143 ·
-
status: needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100