4 ms·
Commenting on the last exercise, "loops" on https://javascript.sumankunwar.com.np/ https://javascript.sumankunwar.com.np/ The average functional programmer wri
by dontchooseanick 3y ago
Commenting on the last exercise, "loops" on https://javascript.sumankunwar.com.np/ https://javascript.sumankunwar.com.np/
The average functional programmer writes this - and not a for loop :
````
const fizzbuzz = (n)=>(n%15==0)?"FizzBuzz":(n%5==0)?"Buzz":(n%3==0)?"Fizz":n;
Array(101).fill(0).map((_,i)=>fizzbuzz(i))
````
- metalliqaz 3y agoI don't think that was the intention of the exercise.
- simonw 3y agoThat's covered in a later chapter: https://javascript.sumankunwar.com.np/arrays/map.html https://javascript.sumankunwar.com.np/arrays/map.html
- LordDragonfang 3y agoArray(101).fill(0).map((_,i)=> [...]) It's astonishing to me that even with all the niceties added to ES7+, JS still needs to resort to a hack like this for a simple range generator. At least give iterators support for forEach(), reduce(), and map(), so we can use `Array(100).keys()` without having to wrap it with the spread operator. Also, can't help but snark that apparently the "average" FP is so preoccupied with showing off their one-liners that they fail the problem requirements (should be `Array(100).fill(0).forEach((_,i)=>console.log(fizzbuzz(i+1)))`).
- graftak 3y agoA little bit better: Array.from({ length: 100 }, () => ...)
- iamsaitam 3y agoIndeed, much cleaner to do `Array.from({length: 100}, (_,i) => fizzbuzz(i));`
- LordDragonfang 3y agoWhile we're cleaning it up: fizzbuzz = i => (i % 3 ? "" : "Fizz") + (i % 5 ? "" : "Buzz") || i Array.from({length: 100}, (_,i) => console.log(fizzbuzz(i+1)))
- boringuser2 3y agoActual developers just use lodash. Honestly, I don't understand the complaint, a lean stdlib isn't a shortcoming.
- LordDragonfang 3y ago...It absolutely is a shortcoming. Python is the second-most popular language out there, despite its awful performance, largely because of its comprehensive stdlib. (First is JS, because web). And I don't understand how you can't see the irony of praising a "lean" stdlib immediately after saying every "actual" developer has to add the bloat of importing lodash.
- boringuser2 3y agoAgain, a lean stdlib is a feature, not a bug. You can have theoretical disagreements with this, but you didn't invent the most popular programming in the world. As for Python, not interested, it's mostly a "performance doesn't matter, being able to do this without expertise, time or even necessarily intellect does" niche. JS has been slow to roll in core functionality, but is has made great strides by being highly conservative, not generating the kind of bloat that PHP has.
- LordDragonfang 3y ago>you didn't invent the most popular programming in the world. This is such a weird ad hominin (especially from a two-week-old account). Regardless, the absolute, overwhelming popularity of jquery pre-ES6 goes to show that practically every developer was of the opinion that JS's weak stdlib was a bug, and the community did their best to patch it. ES7+ is leagues better for it, but it still has room for improvement.