[Haskell-cafe] Using lenses

Mike Ledger eleventynine
Thu Oct 3 09:01:35 UTC 2013


Hi,

In a game I made recently, I had to load OBJ formatted models into an
OpenGL-friendly format. To do that, I'd parse the .obj, into a simple ADT,
and build the model into a vector. Here's where lens comes in: we want to
build separate vectors for the vertices, normals, UVs and faces indices.
lens made that very easy, in conjunction with the state monad. Unlike
normal record fields, you can actually pass around lenses freely.

data ModelBuilder = ModelBuilder { _mbVertices, _mbNormals, _mbUvs ::
[Vector Float], ... }
makeFields ''ModelBuilder

addTo label a = label %= (++ Vector.fromList a)

which would be used like:

case objCommand of
    V  x y z -> addTo vertices [x,y,z]
    VN x y z -> addTo normals [x,y,z]
    VT x y z -> addTo uvs [x,y,z]
    ...

I hope to actually release the game that uses this soon -- it's one of the
very few 'complete' 3D games written in Haskell -- when I have the time,
which will probably be around november.

- Mike



On Thu, Oct 3, 2013 at 6:07 PM, Simon Peyton-Jones <simonpj at microsoft.com>wrote:

>  (I sent this to ?libraries? but Kim-Ee suggested adding Caf?, where so
> many smart people hang out.)****
>
> ** **
>
> Friends****
>
> ** **
>
> Some of you will know that I?ve promised to give a talk about Edward?s
> lens library <http://hackage.haskell.org/package/lens> at the Haskell
> Exchange <http://skillsmatter.com/event/scala/haskell-exchange> in London
> next Wednesday (9th).  I did this to give everyone (including me) a break
> from GHC hackery, and also to force me to learn about this lens voodoo that
> everyone is twittering about.  Edward generously gave me quite a bit of
> one-to-one attention last week (my hair is still standing on end), but this
> message is to ask your help too.****
>
>
> *Specifically, I?d like to give some compelling use-cases*.   If you are
> using the lens library yourself, could you spare a few minutes to tell me
> how you are using it?  I expect to cover Lens and Traversal but not Prism.
> ****
>
> ** **
>
> The use-case everyone starts with is nested records, but I?d like to go
> beyond that.  The next levels seem to be:****
>
> **?         **Lenses as views of data that isn?t ?really there? e.g.
> regarding a record with rectangular coordinates as having polar coordinates
> too.****
>
> **?         **Lenses and Traversals that focus on elements of finite maps
> (Control.Lens.At)****
>
> ** **
>
> What else? I?m sure you are using them in all sorts of cool ways that I
> would never think of, and I?d love to know.****
>
> ** **
>
> Please don?t tell me anything secret!  To give everyone the benefit I may
> just concatenate all the replies and send to you all, so please say if you
> don?t want me to do that with yours. ****
>
> ** **
>
> And don?t burn too many cycles on this...I don?t want to waste your time,
> and I can always get back to you if I can?t understand what you say.
> Sooner is better than later...Weds is coming.****
>
> ** **
>
> Simon ?Edward?s prophet? PJ****
>
> ** **
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20131003/bc6a29c4/attachment.htm>



More information about the Haskell-Cafe mailing list