<div dir="ltr"><div>Oh forget the last part of my email: if the key doesn't exist, insertWith has to insert the new value, hence its type...<br><br>--<br></div>Sylvain<br></div><div class="gmail_extra"><br><div class="gmail_quote">2015-11-10 14:24 GMT+01:00 Sylvain Henry <span dir="ltr"><<a href="mailto:hsyl20@gmail.com" target="_blank">hsyl20@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div>You can use insertWith: <a href="http://hackage.haskell.org/package/containers-0.5.6.3/docs/Data-Map-Strict.html#g:6" target="_blank">http://hackage.haskell.org/package/containers-0.5.6.3/docs/Data-Map-Strict.html#g:6</a><br><br>E.g.:<br>let xs = [("Item0", ["a","b","c"]), ("Item1", ["x","y"])]<br>let m = Map.fromList xs<br>Map.insertWith (++) "Item0" ["d"] m<br><br></div>"d" is cons'ed from the left, so it shouldn't be too slow. I don't know why insertWith has this type instead of:<br>Ord k => (b -> a -> a) -> k -> b -> Map k a -> Map k a<br></div><div>which would allow you to write Map.insertWith (:) "Item0" "d" m<br></div><br>Maybe you should report it.<br><br>--<br></div>Sylvain<br></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">2015-11-10 6:23 GMT+01:00 Dan Stromberg <span dir="ltr"><<a href="mailto:strombrg@gmail.com" target="_blank">strombrg@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><br></div>These are some good leads.<br><br></div>I'll be adding values one at a time, and yes, my keys aren't necessarily unique.<br><br></div>Is there a way of cons'ing on the single values one at a time, that will avoid the slowness of ++ ?<br><br></div>Thanks.<br><div><div><div><div><div class="gmail_extra"><div><div><br><div class="gmail_quote">On Mon, Nov 9, 2015 at 7:45 PM, Sylvain Henry <span dir="ltr"><<a href="mailto:hsyl20@gmail.com" target="_blank">hsyl20@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div>import qualified Data.Map as Map<br><br></div><div>-- if your keys are unique<br></div><div>let xs = [("Item0", ["a","b","c"]), ("Item1", ["x","y"]), ("Item2", ["abc","def"])]<br></div>Map.fromList xs<br><br></div>-- if you want to combine values for keys that are equal<br>let xs = [("Item0", ["a","b","c"]), ("Item1", ["x","y"]), ("Item0", ["abc","def"])]<br></div>Map.fromListWith (++) xs<br><br>--<br></div>Sylvain<br><div><div><div><br></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div>2015-11-10 3:07 GMT+01:00 Dan Stromberg <span dir="ltr"><<a href="mailto:strombrg@gmail.com" target="_blank">strombrg@gmail.com</a>></span>:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><div dir="ltr"><div><br></div>I'm spending a little time here and there to learn some Haskell. I'm coming from a chiefly Python/C/bash background.<div><br></div><div>I want to build a Data.Map where the keys are strings, and the values are lists of strings.</div><div><br></div><div>In Python, collections.defaultdict(list) makes this pretty straightforward. It gives a hash table ("dict") that has values that default to an empty list, since list() produces an empty list. More info here: <a href="https://docs.python.org/3/library/collections.html#collections.defaultdict" target="_blank">https://docs.python.org/3/library/collections.html#collections.defaultdict</a></div><div><br></div><div>Is there an equivalent in Haskell?</div><div><br></div><div>Thanks!</div><span><font color="#888888"><div><div><br></div>-- <br><div>Dan Stromberg</div>
</div></font></span></div>
<br></div></div>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br><br clear="all"><br></div></div><span><font color="#888888">-- <br><div>Dan Stromberg</div>
</font></span></div></div></div></div></div></div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>
</div></div></blockquote></div><br></div>