[Haskell-beginners] Syntactic sugar to insert into a Map?

Mateusz Kowalczyk fuuzetsu at fuuzetsu.co.uk
Sun Jul 21 15:08:13 CEST 2013


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 21/07/13 13:36, martin wrote:
> Hello all,
> 
> What is the best way to enter several (key, value) pairs into a
> Map, such that the code "looks nice", kind of like the do-notation
> with monads.
> 
> I suppose this question is not really specific to a Map. It is
> about chaining functions with a type of a1 ->...-> an -> b -> b.
> 
> Is there any syntactic sugar available? 
> 
> _______________________________________________ Beginners mailing
> list Beginners at haskell.org 
> http://www.haskell.org/mailman/listinfo/beginners
> 
There's no sugar but that doesn't mean you have to have something that
doesn't look nice! Hackage tells me it's an instance of monoid.

*Main Data.Monoid Data.Map> singleton 1 2
fromList [(1,2)]
*Main Data.Monoid Data.Map> singleton 1 2 <> singleton 2 3
fromList [(1,2),(2,3)]
*Main Data.Monoid Data.Map> singleton 1 2 <> singleton 2 3 <>
singleton 4 3
fromList [(1,2),(2,3),(4,3)]

There's of course fromList
*Main Data.Monoid Data.Map> fromList [(1, 2), (3, 4)] <> fromList [(7,
2), (8, 4)]
fromList [(1,2),(3,4),(7,2),(8,4)]

*Main Data.Monoid Data.Map> fromList $ zip [1 .. 3] ['a' .. 'z']
fromList [(1,'a'),(2,'b'),(3,'c')]

I don't think that fromList looks nice enough, especially considering
that you can split it across many lines etc. It'd be easier to help if
you could be more specific about what you're looking for.

As per your chaining question, no, there's no general way to do this
for arbitrary `n' that I'm aware of (that doesn't involve some real
hackery which doesn't seem like what you're looking for)

- -- 
Mateusz K.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.20 (GNU/Linux)

iQIcBAEBAgAGBQJR6909AAoJEM1mucMq2pqX4AcP/AoCGqV5TO+SWQWlSYCtD21L
BbXMJY2ruwdfcuRm8o8IR3iuStVjErjtrqmeNschpbCyqWpwECliabpCCslQygpg
NpeTDhMW7rJSdj+7lPAnBtB/YFNYxsFi9RpZgJftUDPZ843urZArn/zmH5vDtLvH
iG9wpo/TXXOV6obZ04/HQeXAoPqlHQFOfG8cgmSBtYsCU/tyxI5tH4aw1K2QgsyI
fP7IK948wxY09mZQph8NESjOfsvmBa+3VJfFPz7x4+MbsShfYrf2ffA7RtAb+g4E
KTEjoMBSl++AG/B1b6ecGPdeCughjiAAKOPwKqwCyZpdNe3T21E74N3iCOuOHOk5
Pd/uYd/FSVx+M5lWuaHE54UE12yRMYh+MQ1L7QIoOU0CTvMOJ8u3iPa0oA/dBdeW
CCYzxszqXw6uCpZ/9/eZ01xne5YioCFnXZlIegijOhMKkzsvBCuL8WUlrIedd+3k
xWkg54fojqDvebGoaE3v19bswoBiJ7IZs4jScooxyEJU4/7kRBSZksilnpC6MkIz
+pq2owqUmNEXfAozG5vmmH2SZcOmmt1d7EV/c/IyiyAZZh+Zre2nhu2w20E0RWvE
sWBdefCxemdMSvXr7XfCUPsU21uUACKVgIaTg8PcddGG21wMM78qgDIc/F7RZIqx
rBylBbTtAjG0DbuvLLDi
=Aab2
-----END PGP SIGNATURE-----




More information about the Beginners mailing list