Python's collections.defaultdict(list) in Haskell?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Python's collections.defaultdict(list) in Haskell?

Dan Stromberg

I'm spending a little time here and there to learn some Haskell.  I'm coming from a chiefly Python/C/bash background.

I want to build a Data.Map where the keys are strings, and the values are lists of strings.

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: https://docs.python.org/3/library/collections.html#collections.defaultdict

Is there an equivalent in Haskell?

Thanks!

--
Dan Stromberg

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Python's collections.defaultdict(list) in Haskell?

Sylvain HENRY
import qualified Data.Map as Map

-- if your keys are unique
let xs = [("Item0", ["a","b","c"]), ("Item1", ["x","y"]), ("Item2", ["abc","def"])]
Map.fromList xs

-- if you want to combine values for keys that are equal
let xs = [("Item0", ["a","b","c"]), ("Item1", ["x","y"]), ("Item0", ["abc","def"])]
Map.fromListWith (++) xs

--
Sylvain


2015-11-10 3:07 GMT+01:00 Dan Stromberg <[hidden email]>:

I'm spending a little time here and there to learn some Haskell.  I'm coming from a chiefly Python/C/bash background.

I want to build a Data.Map where the keys are strings, and the values are lists of strings.

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: https://docs.python.org/3/library/collections.html#collections.defaultdict

Is there an equivalent in Haskell?

Thanks!

--
Dan Stromberg

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Python's collections.defaultdict(list) in Haskell?

Dan Stromberg

These are some good leads.

I'll be adding values one at a time, and yes, my keys aren't necessarily unique.

Is there a way of cons'ing on the single values one at a time, that will avoid the slowness of ++ ?

Thanks.

On Mon, Nov 9, 2015 at 7:45 PM, Sylvain Henry <[hidden email]> wrote:
import qualified Data.Map as Map

-- if your keys are unique
let xs = [("Item0", ["a","b","c"]), ("Item1", ["x","y"]), ("Item2", ["abc","def"])]
Map.fromList xs

-- if you want to combine values for keys that are equal
let xs = [("Item0", ["a","b","c"]), ("Item1", ["x","y"]), ("Item0", ["abc","def"])]
Map.fromListWith (++) xs

--
Sylvain


2015-11-10 3:07 GMT+01:00 Dan Stromberg <[hidden email]>:

I'm spending a little time here and there to learn some Haskell.  I'm coming from a chiefly Python/C/bash background.

I want to build a Data.Map where the keys are strings, and the values are lists of strings.

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: https://docs.python.org/3/library/collections.html#collections.defaultdict

Is there an equivalent in Haskell?

Thanks!

--
Dan Stromberg

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




--
Dan Stromberg

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Python's collections.defaultdict(list) in Haskell?

Sylvain HENRY
You can use insertWith: http://hackage.haskell.org/package/containers-0.5.6.3/docs/Data-Map-Strict.html#g:6

E.g.:
let xs = [("Item0", ["a","b","c"]), ("Item1", ["x","y"])]
let m = Map.fromList xs
Map.insertWith (++) "Item0" ["d"] m

"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:
Ord k => (b -> a -> a) -> k -> b -> Map k a -> Map k a
which would allow you to write Map.insertWith (:) "Item0" "d" m

Maybe you should report it.

--
Sylvain

2015-11-10 6:23 GMT+01:00 Dan Stromberg <[hidden email]>:

These are some good leads.

I'll be adding values one at a time, and yes, my keys aren't necessarily unique.

Is there a way of cons'ing on the single values one at a time, that will avoid the slowness of ++ ?

Thanks.

On Mon, Nov 9, 2015 at 7:45 PM, Sylvain Henry <[hidden email]> wrote:
import qualified Data.Map as Map

-- if your keys are unique
let xs = [("Item0", ["a","b","c"]), ("Item1", ["x","y"]), ("Item2", ["abc","def"])]
Map.fromList xs

-- if you want to combine values for keys that are equal
let xs = [("Item0", ["a","b","c"]), ("Item1", ["x","y"]), ("Item0", ["abc","def"])]
Map.fromListWith (++) xs

--
Sylvain


2015-11-10 3:07 GMT+01:00 Dan Stromberg <[hidden email]>:

I'm spending a little time here and there to learn some Haskell.  I'm coming from a chiefly Python/C/bash background.

I want to build a Data.Map where the keys are strings, and the values are lists of strings.

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: https://docs.python.org/3/library/collections.html#collections.defaultdict

Is there an equivalent in Haskell?

Thanks!

--
Dan Stromberg

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




--
Dan Stromberg

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Python's collections.defaultdict(list) in Haskell?

Sylvain HENRY
Oh forget the last part of my email: if the key doesn't exist, insertWith has to insert the new value, hence its type...

--
Sylvain

2015-11-10 14:24 GMT+01:00 Sylvain Henry <[hidden email]>:
You can use insertWith: http://hackage.haskell.org/package/containers-0.5.6.3/docs/Data-Map-Strict.html#g:6

E.g.:
let xs = [("Item0", ["a","b","c"]), ("Item1", ["x","y"])]
let m = Map.fromList xs
Map.insertWith (++) "Item0" ["d"] m

"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:
Ord k => (b -> a -> a) -> k -> b -> Map k a -> Map k a
which would allow you to write Map.insertWith (:) "Item0" "d" m

Maybe you should report it.

--
Sylvain

2015-11-10 6:23 GMT+01:00 Dan Stromberg <[hidden email]>:

These are some good leads.

I'll be adding values one at a time, and yes, my keys aren't necessarily unique.

Is there a way of cons'ing on the single values one at a time, that will avoid the slowness of ++ ?

Thanks.

On Mon, Nov 9, 2015 at 7:45 PM, Sylvain Henry <[hidden email]> wrote:
import qualified Data.Map as Map

-- if your keys are unique
let xs = [("Item0", ["a","b","c"]), ("Item1", ["x","y"]), ("Item2", ["abc","def"])]
Map.fromList xs

-- if you want to combine values for keys that are equal
let xs = [("Item0", ["a","b","c"]), ("Item1", ["x","y"]), ("Item0", ["abc","def"])]
Map.fromListWith (++) xs

--
Sylvain


2015-11-10 3:07 GMT+01:00 Dan Stromberg <[hidden email]>:

I'm spending a little time here and there to learn some Haskell.  I'm coming from a chiefly Python/C/bash background.

I want to build a Data.Map where the keys are strings, and the values are lists of strings.

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: https://docs.python.org/3/library/collections.html#collections.defaultdict

Is there an equivalent in Haskell?

Thanks!

--
Dan Stromberg

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




--
Dan Stromberg

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners