0
\$\begingroup\$

This question applies to any language with flatMap -- e.g. functional languages -- but I'll use an example in JavaScript.


I'm working my way through Functional Programming in JavaScript, which is a set of exercise for learning rxjs. Here's a simplified version of Exercise 12, which I have a question about.

The exercise is: Given a collection of states, which contain cities, which contain public officials, make a list of all the cities and their mayors.

My question is: The first approach (const mayors = ...) is the one I came up with. The second approach (const mayors2 = ...) is the reference solution. My approach seems more comprehensible to me -- but I'm a newbie to FP, and of course I'd be biased towards my code over someone else's.

But is the second solution a more typical style? Are there benefits to it that I don't see? Are there other problems that I'd be able to solve with the second approach but not the first?

const states = [
  {
    name: 'California',
    cities: [
      {
        name: 'San Francisco',
        officials: [
          { name: 'London Breed', position: 'Mayor', },
          { name: 'Reginald Freeman', position: 'Fire Chief', },
        ]
      },
      {
        name: 'Anaheim',
        officials: [
          { name: 'Harry Sidhu', position: 'Mayor', },
        ]
      },
    ]
  },
  {
    name: 'Texas',
    cities: [
      {
        name: 'Houston',
        officials: [
          { name: 'Sylvester Turner', position: 'Mayor', },
        ]
      },
    ]
  },
];
const mayors =
  states
    .flatMap(county => county.cities)
    .flatMap(city =>
      city.officials
        .filter(official => official.position === 'Mayor')
        .map(official => ({ city: city.name, mayor: official.name }))
    );
console.log(mayors);
const mayors2 =
  states
    .flatMap(county =>
      county.cities
        .flatMap(city =>
          city.officials
            .filter(official => official.position === 'Mayor')
            .map(official => ({ city: city.name, mayor: official.name }))
        )
    );
console.log(mayors2);

Output: (Both approaches give the same result)

[
  { city: 'San Francisco', mayor: 'London Breed' },
  { city: 'Anaheim', mayor: 'Harry Sidhu' },
  { city: 'Houston', mayor: 'Sylvester Turner' }
]
[
  { city: 'San Francisco', mayor: 'London Breed' },
  { city: 'Anaheim', mayor: 'Harry Sidhu' },
  { city: 'Houston', mayor: 'Sylvester Turner' }
]
New contributor
pandubear is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to Code Review! Please edit your question so that the title describes the purpose of the code, rather than its mechanism. We really need to understand the motivational context to give good reviews. Thanks! \$\endgroup\$ Oct 14 at 6:54

Your Answer

pandubear is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.