[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&#39;t need new sugar. For what it&#39;s worth, in scala you can use \
&quot;-&gt;&quot; to construct tuples, so you&#39;ll sometimes see maps created like \
this:<br> <span style="font-family:courier new,monospace"><br>Map(1 -&gt; \
&quot;one&quot;, 2 -&gt; &quot;two&quot;, 3 -&gt; &quot;foo&quot;)<br><br><span \
style="font-family:arial,helvetica,sans-serif">You can always do something similar in \
haskell (keeping in mind that &quot;-&gt;&quot; is reserved):</span><br> <br>import \
qualified Data.Map as Map<br><br>(--&gt;) = (,)<br><br>makeMap = \
Map.fromList<br><br>myMap = makeMap [ 1 --&gt; &quot;one&quot;<br>                    \
, 2 --&gt; &quot;two&quot;<br>                               , 3 --&gt; \
&quot;foo&quot;<br>  ]</span><br><br>Of course, it&#39;s not idiomatic and won&#39;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">&lt;<a \
href="mailto:eli.lee.frey@gmail.com" \
target="_blank">eli.lee.frey@gmail.com</a>&gt;</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>&gt; Sorry, I \
forgot to explain (probably because I&#39;m too used to  it). I am referring to a \
syntax for easy creation of maps. Something equivalent to \
lists:<br>&gt;<br></div>&gt; to build a list: [ 1, 2, 3]<br>

</div><div>&gt; to build a map; { 1, &quot;one&quot;, 2, &quot;two&quot;, 3, \
&quot;three&quot;}<br>&gt;<br></div><div>&gt; 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 &quot;null&quot;, 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>&gt; myMap char = case char of<br>&gt;         &#39;a&#39; \
-&gt; 1<br>&gt;         &#39;b&#39; -&gt; 2<br>&gt;         &#39;c&#39; -&gt; 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">&lt;<a href="mailto:eli.lee.frey@gmail.com" \
target="_blank">eli.lee.frey@gmail.com</a>&gt;</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>&gt; <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&#39;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">&lt;<a href="mailto:nicolas@incubaid.com" \
target="_blank">nicolas@incubaid.com</a>&gt;</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> &gt; I am terribly missing some syntactic sugar for maps (associative \
data<br> &gt; structures) in Haskell. I find myself using them more than any \
other<br> &gt; data<br>
&gt; structure, and I think there is no big deal in adding some sugar for<br>
&gt; this<br>
&gt; to the language. I could not find out whether such an extension is<br>
&gt; beeing<br>
&gt; discussed. If not, I would like to propose and extension. Any help and<br>
&gt; 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