[prev in list] [next in list] [prev in thread] [next in thread] 

List:       haskell-cafe
Subject:    Re: [Haskell-cafe] When using Cereal, what is the right way to write `get` for multi-constructor typ
From:       Rahul Muttineni <rahulmutt () gmail ! com>
Date:       2016-04-27 3:44:17
Message-ID: CANij+eRQQj-e73X5fKhS1rv63S8-=eN88imVv4U0LfR8Lr5_ZQ () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Lennart Augustsson gave a nice example of how to define enums without
having to maintain the forward as well as reverse transformations here [1].
Applying that to your problem:

> instance Enum STH where
>    fromEnum = fromJust . flip lookup table
>    toEnum = fromJust . flip lookup (map swap table)
> table = [(A, 1), (B, 66), (C, 111)]

And then you can use the code I gave in the previous reply to generate the
Serialize instance. I suppose this is better looking, but you may want to
check the efficiency since it requires a lookup (but it's a super tiny
table so it might not be all that bad). You can generate similar code with
Template Haskell, eliding the lookup.

[1]
http://stackoverflow.com/questions/6000511/better-way-to-define-an-enum-in-haskell

On Tue, Apr 26, 2016 at 1:40 PM, Magicloud Magiclouds <
magicloud.magiclouds@gmail.com> wrote:

> OK. So my understanding is there is no better (good-looking) code. Thank
> you all.
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>
>
-- 
Rahul Muttineni

[Attachment #5 (text/html)]

<div dir="ltr">Lennart Augustsson gave a nice example of how to define enums without \
having to maintain the forward as well as reverse transformations here [1]. Applying \
that to your problem:<div><br></div><div>&gt; instance Enum STH \
where<br></div><div><div><div>&gt;      fromEnum = fromJust . flip lookup \
table</div><div>&gt;      toEnum = fromJust . flip lookup (map swap \
table)</div><div>&gt; table = [(A, 1), (B, 66), (C, \
111)]</div></div><div><br></div><div>And then you can use the code I gave in the \
previous reply to generate the Serialize instance. I suppose this is better looking, \
but you may want to check the efficiency since it requires a lookup (but it&#39;s a \
super tiny table so it might not be all that bad). You can generate similar code with \
Template Haskell, eliding the lookup.  </div><div><br></div><div>[1]  <a \
href="http://stackoverflow.com/questions/6000511/better-way-to-define-an-enum-in-haske \
ll">http://stackoverflow.com/questions/6000511/better-way-to-define-an-enum-in-haskell</a></div><div><div \
class="gmail_extra"><br><div class="gmail_quote">On Tue, Apr 26, 2016 at 1:40 PM, \
Magicloud Magiclouds <span dir="ltr">&lt;<a \
href="mailto:magicloud.magiclouds@gmail.com" \
target="_blank">magicloud.magiclouds@gmail.com</a>&gt;</span> wrote:<br><blockquote \
class="gmail_quote" style="margin:0px 0px 0px \
0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div \
dir="ltr">OK. So my understanding is there is no better (good-looking) code. Thank \
you all.<br></div>_______________________________________________<br> Haskell-Cafe \
mailing list<br> <a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
 <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" \
rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
 <br></blockquote></div><div><br></div>-- <br><div class="gmail_signature">Rahul \
Muttineni</div> </div></div></div></div>



_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://mail.haskell.org/cgi-bin/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