Hacker News new | past | comments | ask | show | jobs | submit login

Because it is easier to reason about data transformations. Here when I see loops and conditionals it is confusing because anything could be happening. company.staff.filter(notCats).forEach(printMessage) has more information of what is going on for example imho. But there is a lot more to it.



How would you define the `printMessage` function? The `pet` object doesn't have context about `staff`, and the `filter` function is filtering staff, not pets. It could return staff that have both cats and dogs, so it would incorrectly print dogs.


It was a rough example but the idea is thinking in terms of data in and data out and how can it be done. Here the train of thought first would be: do I have the right data form for the thing I am doing? Here we have:

{company: {staff: [{..., pets: []}]}}

And what we want to do is to produce a list of all the pet cats with its owner name.

[{cat: "bla", owner: "bla"}...]

or

[{owner: "bla", cats:[...],...}, ...]

So I guess what Im trying to explain is that what is important is the change in the way of thinking about problems. When you think of them as data transformations there is a whole lot of possibilities that open. And it is not more expensive because you even have things like transducers. When you abstract away the iteration you are able to compose much more easily.

And to answer your question directly in my example the print function would print for each cat belonging to the staff which is not great flattening would be more elegant. What I wanted to convey is thinking in data transformations and abstracting away the implementation details of the iteration.


In javascript:

  const stf = [
      {name: "x", pets: [{type: "cat", name: "kitty"},
                       {type: "cat", name: "kitty2"}]},
      {name: "y", pets: [{type: "dog"}]},
      {name: "z", pets: [{type: "cat", name: "miau"}]},
  ];

  const myTransform = ({ pets, name: ownerName }) => pets
        .filter(({type}) => type === "cat")
        .map(({ name: catName }) => ({ ownerName, catName }))

  stf
      .map(myTransform)
      .flat()
      .forEach(({ownerName, catName}) => console.log(`Cat ${catName} to ${ownerName}`))




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

Search: