Usage of lenses

3 views
Skip to first unread message

Kamil Ciemniewski

unread,
Dec 22, 2011, 6:24:54 AM12/22/11
to sn...@snapframework.com
Hi all,

I work at MongoDB backend for Auth but I'm pretty confused with lenses API.. 

I've got such an init function:

initMongoDBAuthManager
:: AuthSettings
-- ^ Authentication settings for your app
-> Lens b (Snaplet SessionManager)
-- ^ Lens into a 'SessionManager' auth snaplet will use
-> Lens b (Snaplet MongoDBSnaplet)
-- ^ Lens into a 'MongoDBSnaplet'
-> SnapletInit b (AuthManager b)
initMongoDBAuthManager = ...

Now, how do I get Snaplet MongoDBSnaplet out of
Lens b (Snaplet MongoDBSnaplet) ??


Regards
--
http://digitalinventor.pl
Kamil Ciemniewski

Leon Smith

unread,
Dec 23, 2011, 5:19:07 PM12/23/11
to Kamil Ciemniewski, sn...@snapframework.com
Use:

getL :: Lens b (Snaplet MongoDBSnaplet) -> b -> Snaplet MongoDBSnaplet

The intuition is that "Lens record field" represents a getter and
setter for a field inside
of some record. "getL" gets the getter, and "setL" gets the setter.

When writing a snaplet, you don't know what this record looks like,
so this is why the record is
a polymorphic variable ("b", in this case.) And when writing a
snaplet, you can't know what the
record looks like, as it can change when you
add/remove/reconfigure/modify other snaplets that
make up your app. So from type of your function:

initMongoDBAuthManager
:: AuthSettings
-- ^ Authentication settings for your app
-> Lens b (Snaplet SessionManager)
-- ^ Lens into a 'SessionManager' auth snaplet will use
-> Lens b (Snaplet MongoDBSnaplet)
-- ^ Lens into a 'MongoDBSnaplet'
-> SnapletInit b (AuthManager b)

So when you are inside "initMongoDBAuthManager", the only things you
know about record "b" is that
it has a field of type (Snaplet SessionManager) and another field of
type (Snaplet MongoDBSnaplet), nothing
more and nothing less. Instead of referring to the fields via some
.dotname, you are passed the field names instead.

Best,
Leon

Reply all
Reply to author
Forward
0 new messages