Magical, Mystical JavaScript Transducers
jrsinclair.com
Magical, Mystical JavaScript Transducers
1–10 of 51 posts
Re: Magical, Mystical JavaScript Transducers
#2Re: Magical, Mystical JavaScript Transducers
#3Why is this not the obvious solution to the stated problem?
function avgPopularity(slang) {
let sum = 0;
let count = 0;
for (item of slang) {
if (item.popularity > 0) {
sum += item.popularity;
count += 1;
}
}
return sum / count;
}Re: Magical, Mystical JavaScript Transducers
#4Impressive amount of code and words. I think I'd think twice before approving this in a code review, though. Why is this not the obvious solution to the stated problem? function avgPopularity(slang) { let sum = 0; let count = 0; for (item of slang) { if (item.popularity > 0) { sum += item.popularity; count += 1; } } return sum / count; }
Re: Magical, Mystical JavaScript Transducers
#5Impressive amount of code and words. I think I'd think twice before approving this in a code review, though. Why is this not the obvious solution to the stated problem? function avgPopularity(slang) { let sum = 0; let count = 0; for (item of slang) { if (item.popularity > 0) { sum += item.popularity; count += 1; } } return sum / count; }
Re: Magical, Mystical JavaScript Transducers
#6Impressive amount of code and words. I think I'd think twice before approving this in a code review, though. Why is this not the obvious solution to the stated problem? function avgPopularity(slang) { let sum = 0; let count = 0; for (item of slang) { if (item.popularity > 0) { sum += item.popularity; count += 1; } } return sum / count; }
This point isn’t about transducers specifically but more about avoiding specifying the things you don’t care about. Eg, in Common Lisp (which was old enough to somewhat care about how to iterate things):
(defun avg-popularity (list)
(loop for item in list
when (plusp (popularity item))
sum pop into s
count t into c
finally (return (/ s c))))
This avoids having to care about the mechanics of how to sum or count things and I think it’s too sequential, when you don’t really care about that. One can certainly imagine a simpler to express solution in e.g. apl.Re: Magical, Mystical JavaScript Transducers
#7Impressive amount of code and words. I think I'd think twice before approving this in a code review, though. Why is this not the obvious solution to the stated problem? function avgPopularity(slang) { let sum = 0; let count = 0; for (item of slang) { if (item.popularity > 0) { sum += item.popularity; count += 1; } } return sum / count; }
Re: Magical, Mystical JavaScript Transducers
#8Impressive amount of code and words. I think I'd think twice before approving this in a code review, though. Why is this not the obvious solution to the stated problem? function avgPopularity(slang) { let sum = 0; let count = 0; for (item of slang) { if (item.popularity > 0) { sum += item.popularity; count += 1; } } return sum / count; }
Re: Magical, Mystical JavaScript Transducers
#9Impressive amount of code and words. I think I'd think twice before approving this in a code review, though. Why is this not the obvious solution to the stated problem? function avgPopularity(slang) { let sum = 0; let count = 0; for (item of slang) { if (item.popularity > 0) { sum += item.popularity; count += 1; } } return sum / count; }
dot = field => obj => obj[field] // unsafe
Array.average = self -> reduce(add, self) / self.length // unsafe
slang.filter(s -> s.popularity > 0)
.map(dot('popularity'))
.average()
Bonus points: - no state
- reusable pure functions
- a tad shorter
- maybe as readable as the `for over state` idiom ? (depending on habits)Re: Magical, Mystical JavaScript Transducers
#10Impressive amount of code and words. I think I'd think twice before approving this in a code review, though. Why is this not the obvious solution to the stated problem? function avgPopularity(slang) { let sum = 0; let count = 0; for (item of slang) { if (item.popularity > 0) { sum += item.popularity; count += 1; } } return sum / count; }
The problem with functional programming is that the obvious approach is not efficient since it passes the array several times. So the goal is to make the functional approach as efficient as the imperative for loop.
Now if you do that by hand, you end up with some messy code. So the article shows how you can combine the operations with a helper library in a structured way.
That said, it kind of reminds me of The Evolution of a Haskell Programmer [0]
[0] https://www.cs.utexas.edu/~cannata/cs345/Class%20Notes/10%20...