4 ms·
In Haskell, these direct equivalents aren’t bad at all: cityToState = Map.fromList [ (city, state) | (state, cities) <- Map.toList citiesByStat
by evincarofautumn 8y ago
In Haskell, these direct equivalents aren’t bad at all:
cityToState = Map.fromList
[ (city, state)
| (state, cities) <- Map.toList citiesByState
, city <- cities
]
cityToState = Map.fromList $ do
(state, cities) <- Map.toList citiesByState
city <- cities
pure (city, state)
For multiple “nested loops”, I generally prefer do-notation over both list comprehensions and combinators such as concatMap, unless the structure is simple enough that the combinator version is much shorter.
- klodolph 8y agoAnd for those following who don’t know Haskell, both of these are basically sugar for concatMap. (>>=) = flip concatMap