Note that there are some explanatory texts on larger screens.

plurals
  1. POTo Use or Not to Use Data.Map
    text
    copied!<p>I'm currently working on a Haskell API. The latter provides some functions that currently take a <em>list of lists</em> as input, i.e. <code>[(String,[(String, Double)])]</code>.</p> <p>For visualization purposes, here's a sample of the <em>list of lists</em> mentioned above: </p> <pre><code>[ ("A", [ ("I1", 1), ("I2", 2), ] ), ("B", [ ("I1", 3), ] ) ] </code></pre> <p>I've defined some <em>private</em> helper functions. One helper function will search for specific entries in this list (<code>Data.List.find</code> = <code>O(n)</code>); another one will perform intersections; and another function will transform the list presented above to the following one:</p> <pre><code>[ ("I1", [ ("A", 1), ("B", 3), ] ), ("I2", [ ("A", 2), ] ) ] </code></pre> <p>The function that performs the transformation uses <code>Data.Map</code>, since it offers some functions that simplify that process a lot, like <code>Data.Map.unionWith</code> and <code>Data.Map.insertWith</code>. Well, since the transformation function had to call <code>Data.Map.fromList</code> and <code>Data.Map.toList</code>, I thought it would be nice to have a <em>map of maps</em> instead of a <em>list of lists</em> from the beginning. And so I changed my sample input to match the <em>map of maps</em> requirement. </p> <p>Again, for visualization purposes, here's the list from above as a <em>map of maps</em>: </p> <pre><code>Map.fromList [ ("A", Map.fromList [ ("I1", 1), ("I2", 2), ] ), ("B", Map.fromList [ ("I1", 3), ] ) ] </code></pre> <p>Thanks to this step my code lost a few lines, and thanks to <code>Data.Map.lookup</code>, finding a desired now only takes <code>O(log n)</code> time.</p> <p>Nonetheless, I'm currently asking myself if this really is a good solution? Is a <em>map of maps</em> the way to go? Or should the transformation function work with <code>Data.Map.fromList</code> and <code>Data.Map.toList</code>, and let the rest work with <em>list of lists</em>? Or better yet, is there a data structure that is more suitable for this kind of work?</p> <p>I'm really looking forward to your replies.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload