[Haskell-beginners] Syntactic sugar to insert into a Map?
martin.drautzburg at web.de
Sun Jul 21 16:23:56 CEST 2013
-----BEGIN PGP SIGNED MESSAGE-----
Am 07/21/2013 03:08 PM, schrieb Mateusz Kowalczyk:
> 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
> 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
> *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
> *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.
I just want to insert several key/value pairs, something like
insert key1 val1
insert key2 val2
I know I can achieve this with dollars and by reversing the order,
such that Map.empty is at the end. But I don't think it is pretty.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
-----END PGP SIGNATURE-----
More information about the Beginners