Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

IMO even more readable if we ditch the functional looking stuff:

  function slamTimTam(timtam) {
    timtam.biteArbitraryCorner();
    timtam.biteOppositeCorner();
    timtam.insertInBeverage();
    timtam.drawLiquid();
    timtam.insertIntoMouth();
  }

  function slamTimTams(timtams) {
    const slammedTimTams = timtams.slice(0, MAX_TIMTAMS);

    for (const timtam of slammedTimTams) {
      slamTimTam(timtam);
    }

    return slammedTimTams;
  }


> functional looking stuff

That doesn't look "functional" at all. The "slamTimTam" function is modifying an object it's been given. Modifying an object that you've taken as an argument should almost never happen anywhere in any codebase, much less a functional one.

Although it's not totally your fault: TFA seems completely confused about what "map" actually does, itself. I can't really tell whether they know what immutability is or not.

In fact one of my biggest pet peeves is "fluent" style code that modifies the object it's being called on. D3 in JavaScript is a perfect example of this abominable perversion of that "functional" style.


> That doesn't look "functional" at all.

Yes... because he ditched it.

And further - there isn't really anything wrong with modifying an object. Functional programs are nice because they remove lots of extraneous state, but there are many situations where you still have to track state somewhere - doing it in a contained object that is changed is fine.

The issue (particularly in JS) is when you allow assignment by reference value, and not by copy - in that case modifying an object can be dangerous because other parts of the code may be holding onto the same reference, and you've accidentally introduced subtle shared state.

Ideally - all users would be aware, and would make sure that assignment only happens with a call to something like copy()/clone() but the language doesn't really have the tooling to enforce this (unlike many other languages ex: c++/Rust/others)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: