[prev in list] [next in list] [prev in thread] [next in thread]
List: haskell-cafe
Subject: Re: [Haskell-cafe] [extension]syntactic sugar for maps
From: Eric Rasmussen <ericrasmussen () gmail ! com>
Date: 2013-03-28 4:22:55
Message-ID: CAMysB06zazxwfcXDXHX2TRkpjEqYefB5tJYzfexByo9KLmwGOw () mail ! gmail ! com
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
I agree that fromList or pattern matching at the function or case level are
readable. We probably don't need new sugar. For what it's worth, in scala
you can use "->" to construct tuples, so you'll sometimes see maps created
like this:
Map(1 -> "one", 2 -> "two", 3 -> "foo")
You can always do something similar in haskell (keeping in mind that "->"
is reserved):
import qualified Data.Map as Map
(-->) = (,)
makeMap = Map.fromList
myMap = makeMap [ 1 --> "one"
, 2 --> "two"
, 3 --> "foo"
]
Of course, it's not idiomatic and won't be immediately obvious to readers
that you are constructing tuples. However, if you find it easier to read
and need to write a lot of map literals in your code, it may be worth
coming up with a couple of aliases similar to those.
On Wed, Mar 27, 2013 at 1:16 PM, Eli Frey <eli.lee.frey@gmail.com> wrote:
> > Sorry, I forgot to explain (probably because I'm too used to it). I am
> referring to a syntax for easy creation of maps. Something equivalent to
> lists:
> >
> > to build a list: [ 1, 2, 3]
> > to build a map; { 1, "one", 2, "two", 3, "three"}
> >
> > Without it I am always forced to use fromList.
>
> This looks like something to use records for, or in any case something
> where association list performance is not an issue.
>
> If you just want to store some configuration-like structure and pass it
> around, a record is great for this. You might find where in other
> languages you would simply leave a key "null", in Haskell you can just fill
> it with a Nothing.
>
> Maps (hash or binary-tree) really pay off when they are filled dynamically
> with massive numbers of associations. I find when I am ending up in this
> scenario, I am generating my map programatically, not writing it as a
> literal.
>
> Sometimes people even write maps simply as functions and not even as a
> data-structure.
>
> > myMap char = case char of
> > 'a' -> 1
> > 'b' -> 2
> > 'c' -> 3
>
> Perhaps you could describe a situation you are in where you are wanting
> this, and we could see if there is something you can do already that is
> satisfying and solves your problem.
>
>
>
> On Wed, Mar 27, 2013 at 12:59 PM, Eli Frey <eli.lee.frey@gmail.com> wrote:
>
>> > http://hackage.haskell.org/trac/ghc/wiki/OverloadedLists comes to mind.
>>
>> This assumes you can turn ANY list into a thing. Maps only make sense to
>> be constructed from association list. If I've got a [Char], how do I make
>> a map form it?
>>
>>
>> On Wed, Mar 27, 2013 at 12:56 PM, Nicolas Trangez <nicolas@incubaid.com>wrote:
>>
>>> On Wed, 2013-03-27 at 21:30 +0200, Răzvan Rotaru wrote:
>>> > I am terribly missing some syntactic sugar for maps (associative data
>>> > structures) in Haskell. I find myself using them more than any other
>>> > data
>>> > structure, and I think there is no big deal in adding some sugar for
>>> > this
>>> > to the language. I could not find out whether such an extension is
>>> > beeing
>>> > discussed. If not, I would like to propose and extension. Any help and
>>> > suggestions are very welcome here. Thanks.
>>>
>>> http://hackage.haskell.org/trac/ghc/wiki/OverloadedLists comes to mind.
>>>
>>> Nicolas
>>>
>>>
>>> _______________________________________________
>>> Haskell-Cafe mailing list
>>> Haskell-Cafe@haskell.org
>>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>>
>>
>>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
[Attachment #5 (text/html)]
I agree that fromList or pattern matching at the function or case level are readable. \
We probably don't need new sugar. For what it's worth, in scala you can use \
"->" to construct tuples, so you'll sometimes see maps created like \
this:<br> <span style="font-family:courier new,monospace"><br>Map(1 -> \
"one", 2 -> "two", 3 -> "foo")<br><br><span \
style="font-family:arial,helvetica,sans-serif">You can always do something similar in \
haskell (keeping in mind that "->" is reserved):</span><br> <br>import \
qualified Data.Map as Map<br><br>(-->) = (,)<br><br>makeMap = \
Map.fromList<br><br>myMap = makeMap [ 1 --> "one"<br> \
, 2 --> "two"<br> , 3 --> \
"foo"<br> ]</span><br><br>Of course, it's not idiomatic and won't \
be immediately obvious to readers that you are constructing tuples. However, if you \
find it easier to read and need to write a lot of map literals in your code, it may \
be worth coming up with a couple of aliases similar to those.<br> <br><br><div \
class="gmail_quote">On Wed, Mar 27, 2013 at 1:16 PM, Eli Frey <span dir="ltr"><<a \
href="mailto:eli.lee.frey@gmail.com" \
target="_blank">eli.lee.frey@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 class="im"><div><div>> Sorry, I \
forgot to explain (probably because I'm too used to it). I am referring to a \
syntax for easy creation of maps. Something equivalent to \
lists:<br>><br></div>> to build a list: [ 1, 2, 3]<br>
</div><div>> to build a map; { 1, "one", 2, "two", 3, \
"three"}<br>><br></div><div>> Without it I am always forced to use \
fromList.<span><font color="#888888"><br><br></font></span></div>
</div><div><span><font color="#888888">This looks like something to use records for, \
or in any case something where association list performance is not an \
issue.<br><br>If you just want to store some configuration-like structure and pass it \
around, a record is great for this. You might find where in other languages you \
would simply leave a key "null", in Haskell you can just fill it with a \
Nothing.<br>
<br>Maps (hash or binary-tree) really pay off when they are filled dynamically with \
massive numbers of associations. I find when I am ending up in this scenario, I am \
generating my map programatically, not writing it as a literal.<br>
<br>Sometimes people even write maps simply as functions and not even as a \
data-structure.<br><br>> myMap char = case char of<br>> 'a' \
-> 1<br>> 'b' -> 2<br>> 'c' -> 3<br>
<br>Perhaps you could describe a situation you are in where you are wanting this, and \
we could see if there is something you can do already that is satisfying and solves \
your problem.<br></font></span></div><div><span><font color="#888888"><br>
</font></span></div></div><div class="gmail_extra"><br><br><div \
class="gmail_quote"><div class="im">On Wed, Mar 27, 2013 at 12:59 PM, Eli Frey <span \
dir="ltr"><<a href="mailto:eli.lee.frey@gmail.com" \
target="_blank">eli.lee.frey@gmail.com</a>></span> wrote:<br>
</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex"><div dir="ltr"><div class="im"><div>> <a \
href="http://hackage.haskell.org/trac/ghc/wiki/OverloadedLists" \
target="_blank">http://hackage.haskell.org/trac/ghc/wiki/OverloadedLists</a> comes to \
mind.<br>
<br></div></div>This assumes you can turn ANY list into a thing. Maps only make \
sense to be constructed from association list. If I've got a [Char], how do I \
make a map form it?<br> </div><div><div class="h5"><div><div><div \
class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Mar 27, 2013 at 12:56 \
PM, Nicolas Trangez <span dir="ltr"><<a href="mailto:nicolas@incubaid.com" \
target="_blank">nicolas@incubaid.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex"><div>On Wed, 2013-03-27 at 21:30 +0200, Răzvan Rotaru \
wrote:<br> > I am terribly missing some syntactic sugar for maps (associative \
data<br> > structures) in Haskell. I find myself using them more than any \
other<br> > data<br>
> structure, and I think there is no big deal in adding some sugar for<br>
> this<br>
> to the language. I could not find out whether such an extension is<br>
> beeing<br>
> discussed. If not, I would like to propose and extension. Any help and<br>
> suggestions are very welcome here. Thanks.<br>
<br>
</div><a href="http://hackage.haskell.org/trac/ghc/wiki/OverloadedLists" \
target="_blank">http://hackage.haskell.org/trac/ghc/wiki/OverloadedLists</a> comes to \
mind.<br> <span><font color="#888888"><br>
Nicolas<br>
</font></span><div><div><br>
<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" \
target="_blank">Haskell-Cafe@haskell.org</a><br> <a \
href="http://www.haskell.org/mailman/listinfo/haskell-cafe" \
target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br> \
</div></div></blockquote></div><br></div> \
</div></div></div></div></blockquote></div><br></div> \
<br>_______________________________________________<br> Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" \
target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br> \
<br></blockquote></div><br>
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic