Hacker News new | ask | show | jobs
by icey 5527 days ago

  > (+ 1 1 1 1)
  => 2

  > (* 3 4 5 6)
  => 12
Does it only consider the first and second items in a list?
2 comments

Man, I forget how nice it is to be able to look into the source of stuff.

Other than being obviously new, this is pretty cool.

Edit: Would there be a better way to fix it than to add functions like this?

  function adder() {
    var args = Array.prototype.slice.call(arguments);
    return args.reduce(function(a, b){return a + b})
  }

  function subtractor() {
    var args = Array.prototype.slice.call(arguments);
    return args.reduce(function(a, b){return a - b})
  }

  function multiplier() {
    var args = Array.prototype.slice.call(arguments);
    return args.reduce(function(a, b){return a * b})
  }

  function divider() {
    var args = Array.prototype.slice.call(arguments);
    return args.reduce(function(a, b){return a / b})
  }
(I haven't really put too much thought into this, but would love to hear of stronger approaches or obvious bad ideas in this one)
In principle that is a nice way to do it, and given that reduce is implemented natively on some platforms it might even be sufficiently performant, but to be honest I'd probably just write them as loops mutating a local variable. A language runtime is one of the places where a little bit of readability can be sacrificed for performance (although of course, one should measure it to make sure the gains are worth the cost).

Someone has actually already submitted a pull request doing just this:

https://github.com/jcoglan/fargo/pull/3

It will be fixed soon cf. https://github.com/jcoglan/fargo/pull/3