Magical, Mystical JavaScript Transducers
11–20 of 51 posts
Re: Magical, Mystical JavaScript Transducers
#12Impressive 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; }
because something something mutable state
Re: Magical, Mystical JavaScript Transducers
#13Impressive 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; }
Because a bunch of those lines are concerned with the boring mechanics of how to compute this thing rather than what is being computed. It’s reasonable to say having a big system of composable transducers is pointless for one computation. It’s harder for 1000. 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…
(defun avg-popularity (list)
(loop
for item in list
for pop = (popularity item)
when (plusp pop)
sum pop into s
and
count t into c
finally (return (/ s c))))
Two changes: 1) making `pop' be a thing, and 2) `when' clause of loop only applies to the next clause by default (here, `sum pop into s'), you need to include `and' to chain it with the next one so that both are guarded by `when' condition.Re: Magical, Mystical JavaScript Transducers
#14Every time I see this blog I'm extremely interested in the content, but the stylized presentation is so jarring and tough to read. I doubt this is an original qualm and I'm fully able to switch to reading mode in Firefox to mitigate this problem, but frankly it's off-putting.
Re: Magical, Mystical JavaScript Transducers
#15Impressive 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; }
Yours is the obvious solution but the problem is a simple example to help people understand the more complex idea of transducers. It's for situations where the code inside the for loop is so complex that it would be nice to organize it with functional programming principals. 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…
The puzzle nature of FP can sometimes prevent the simplest solution to a problem. For example, try writing a function that accepts a list of N numbers between 1 and N and returns its histogram (list of counts of each number). There's an imperative solution in O(N) time with one for loop. But in FP, I'm not sure O(N) can be achieved, and even O(N log N) requires tree-like data structures that aren't needed in imperative.
Re: Magical, Mystical JavaScript Transducers
#16Impressive 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
#17Impressive 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 reason to use transducers instead of a for loop here is that it allows you to expose a library function which takes a transducer as input. It's hard to refactor the for loop above to allow someone consuming this function to specify additional things they want to aggregate due to an API change in the definition of the 'slang' type, but with transducers you just take one as an argument.
Re: Magical, Mystical JavaScript Transducers
#18Impressive 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; }
Nitpick: There should be a check for zero count to avoid a divide-by-zero. It's missing in the original article as well.
Re: Magical, Mystical JavaScript Transducers
#19Earlier quoted context omitted.
Yours is the obvious solution but the problem is a simple example to help people understand the more complex idea of transducers. It's for situations where the code inside the for loop is so complex that it would be nice to organize it with functional programming principals. 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…
For me it's the other way around. A for loop is a general purpose solution that changes continuously with the problem: whenever I need to gather some extra data, just add some hairs to the for loop. The time complexity is obvious, so is the space complexity (how much data is in memory at any given time), and it's easy to pause and debug. But if you have a bunch of FP combinators and recursion schemas, when the proble…
Re: Magical, Mystical JavaScript Transducers
#20Earlier quoted context omitted.
For me it's the other way around. A for loop is a general purpose solution that changes continuously with the problem: whenever I need to gather some extra data, just add some hairs to the for loop. The time complexity is obvious, so is the space complexity (how much data is in memory at any given time), and it's easy to pause and debug. But if you have a bunch of FP combinators and recursion schemas, when the proble…
That seems to be such a basic task. Please take it not as me not believing it but could you provide some source about there not being a known O(N) solution for that problem, I'd like to know what the hurdles are and why there might be no solution at all or why finding one is so difficult. (I tried googling it but failed to find something useful)