getify / Functional-Light-JS

Pragmatic, balanced FP in JavaScript. @FLJSBook on twitter.
http://FLJSBook.com
Other
16.6k stars 1.96k forks source link

Ch. 5, Purely Relative: Median function doesn't return median #156

Open mikmart opened 6 years ago

mikmart commented 6 years ago

In the Purely Relative section of Chapter 5 you define a function called median to use in an example, but it doesn't actually find the median: it's just returning the average of the first and last values in the array. (Which just coincidentally is also the median in the specific array used in the example.)

Actually defining median inline might be a bit too complicated (as the purpose of the function in the example is just to illustrate the side effects of modifying the array referenced by the closure) so maybe the function should just be renamed? Or alternatively it could be changed to calculate the mean, since that would give the same output in the example, and is simple to define?

getify commented 6 years ago

Thanks for bringing up this point.

IMO, this is a classic case of why sometimes using foo(..) / bar(..) type names is better than trying to come up with domain-specific names. The point of the section has nothing to do with what the utility does, only how it does it. Any domain-specific name invites the reader to be distracted by what it does. But, I agree I shouldn't have used a name like median(..), since that name implies it's a generalized utility.

I could have called it more accurately center(..) or middle(..), though those names only work if you know the list is at least partially sorted (the lowest number first, highest number last). I don't really want to complicate what the function does, since it isn't the point of the discussion. I may just change it to foo(..) here.