lenses

83 views
Skip to first unread message

Tim Pigden

unread,
Jun 19, 2015, 7:29:09 AM6/19/15
to type...@googlegroups.com
Hi
I'm already using quite a lot of the generic features from shapeless.
Now I need lenses. But there are 2 different implementations in the typelevel stack - shapeless and monocle (not to mention scalaz).
Is anyone in a position to offer advice on the differences between them? In particular are there any strong reasons to favour monocle.

It should be noted I have a fairly large number of case classes in my data model and that some of them contain 20+ fields - I don't want to have to manually create lenses for all fields of all case classes if possible.

Tim

Miles Sabin

unread,
Jun 19, 2015, 8:58:10 AM6/19/15
to Tim Pigden, type...@googlegroups.com
My take on this is that Monocle is the superior lens implementation
both in terms of generality and in terms of performance. shapeless has
a slight edge in terms of ease of use and boilerplate reduction.

I plan to do some more work on lenses for shapeless 3.0 which will
hopefully take them in an interesting direction and make them
competitive with Kmett-style lenses, but for now I would say that if
lenses are important to you you should use Monocle. OTOH if they're
incidental and you're already using shapeless then there's no
compelling reason not to use shapeless's.

I think Julien is on this list ... I think he'd probably agree with
the above ...

Cheers,


Miles

--
Miles Sabin
tel: +44 7813 944 528
skype: milessabin
gtalk: mi...@milessabin.com
http://milessabin.com/blog
http://twitter.com/milessabin

Tim Pigden

unread,
Jun 19, 2015, 9:09:38 AM6/19/15
to Miles Sabin, type...@googlegroups.com
Thanks
Been experimenting for the last hour or so with shapeless and it does look as if compilation speed is a problem. for example this
object Sites {

object CustomerSiteLenses {
val location = lens[CustomerSite].location
//val timeWindows = lens[CustomerSite].timeWindows
val siteType = lens[CustomerSite].siteType
val fixedDuration = lens[CustomerSite].fixedDuration
val loadRates = lens[CustomerSite].loadRates
val unloadRates = lens[CustomerSite].unloadRates
//val address = lens[CustomerSite].address
val isArrivalConstraint = lens[CustomerSite].isArrivalConstraint
val earlyFlexibility = lens[CustomerSite].earlyFlexibility
val lateFlexibility = lens[CustomerSite].lateFlexibility
val flexibleCost = lens[CustomerSite].flexibleCost
//val siteFacilities = lens[CustomerSite].siteFacilities
//val requiredVehicleFacilities = lens[CustomerSite].requiredVehicleFacilities
//val allowedVehicleTypes = lens[CustomerSite].allowedVehicleTypes
//val excludedVehicleTypes = lens[CustomerSite].excludedVehicleTypes

//val _extension = lens[CustomerSite]._extension

}

}
took some 77 seconds to compile. 
Will try monocle but keep my eye open for 3.0
--
Tim Pigden
Optrak Distribution Software Limited
+44 (0)1992 517100
http://www.linkedin.com/in/timpigden
http://optrak.com
Optrak Distribution Software Ltd is a limited company registered in England and Wales.
Company Registration No. 2327613 Registered Offices: Suite 6,The Maltings, Hoe Lane, Ware, SG12 9LR England 
This email and any attachments to it may be confidential and are intended solely for the use of the individual to whom it is addressed. Any views or opinions expressed are solely those of the author and do not necessarily represent those of Optrak Distribution Software Ltd. If you are not the intended recipient of this email, you must neither take any action based upon its contents, nor copy or show it to anyone. Please contact the sender if you believe you have received this email in error.

Miles Sabin

unread,
Jun 19, 2015, 9:25:47 AM6/19/15
to Tim Pigden, type...@googlegroups.com
On Fri, Jun 19, 2015 at 2:09 PM, Tim Pigden <tim.p...@optrak.com> wrote:
> Thanks
> Been experimenting for the last hour or so with shapeless and it does look
> as if compilation speed is a problem. for example this
>
> object Sites {
>
> object CustomerSiteLenses {
> val location = lens[CustomerSite].location
> //val timeWindows = lens[CustomerSite].timeWindows
> val siteType = lens[CustomerSite].siteType
> val fixedDuration = lens[CustomerSite].fixedDuration
> val loadRates = lens[CustomerSite].loadRates
> val unloadRates = lens[CustomerSite].unloadRates
> //val address = lens[CustomerSite].address
> val isArrivalConstraint = lens[CustomerSite].isArrivalConstraint
> val earlyFlexibility = lens[CustomerSite].earlyFlexibility
> val lateFlexibility = lens[CustomerSite].lateFlexibility
> val flexibleCost = lens[CustomerSite].flexibleCost
> //val siteFacilities = lens[CustomerSite].siteFacilities
> //val requiredVehicleFacilities =
> lens[CustomerSite].requiredVehicleFacilities
> //val allowedVehicleTypes = lens[CustomerSite].allowedVehicleTypes
> //val excludedVehicleTypes = lens[CustomerSite].excludedVehicleTypes
>
> //val _extension = lens[CustomerSite]._extension
>
> }
>
> }
>
> took some 77 seconds to compile.

What shapeless version? If coproducts are involved 2.2.2 should improve things.

Tim Pigden

unread,
Jun 19, 2015, 9:56:16 AM6/19/15
to Miles Sabin, type...@googlegroups.com
Hi
It is 2.2.2 ( just went back and deleted all the older shapeless in my ivy cache to make sure)
Even if I reduce the above to the single uncommented value isArrivalConstraint - which is a boolean, it still takes about 6 seconds (newish i7)
Tim

Miles Sabin

unread,
Jun 19, 2015, 11:44:02 AM6/19/15
to Tim Pigden, type...@googlegroups.com
On Fri, Jun 19, 2015 at 2:56 PM, Tim Pigden <tim.p...@optrak.com> wrote:
> It is 2.2.2 ( just went back and deleted all the older shapeless in my ivy
> cache to make sure)
> Even if I reduce the above to the single uncommented value
> isArrivalConstraint - which is a boolean, it still takes about 6 seconds
> (newish i7)

It'll be the number of fields in CustomerSite which is the issue ... how many?

If you can boil down something along the lines that Sam did for
Coproducts and post it here,

https://github.com/milessabin/shapeless/issues/381

that would be very helpful.

杨博

unread,
Dec 28, 2016, 3:40:32 AM12/28/16
to Typelevel Users & Development List
I would suggest https://github.com/adamw/quicklens, which seems easier than other implementations.

在 2015年6月19日星期五 UTC+8下午7:29:09,Tim Pigden写道:

Matthew Pocock

unread,
Jan 3, 2017, 5:25:55 AM1/3/17
to 杨博, Typelevel Users & Development List
Sorry for resurrecting a zombie thread, but I sketched this out the other day:


It is a little macro that takes a trait and generates case classes for each operation. It would be fairly trivial to do essentially the same thing for the fields of a case class (generating a per-field command object that represents a query for that field), and then auto-generate the implicits from these case classes to the corresponding lens. While lenses don't have a single inhabitant, the case classes representing each field let you select exactly one.

Matthew

--
You received this message because you are subscribed to the Google Groups "Typelevel Users & Development List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to typelevel+unsubscribe@googlegroups.com.
To post to this group, send email to type...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/typelevel/3a14ab93-8010-44cd-a608-d0db63b8d6a0%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Dr Matthew Pocock
Turing ate my hamster LTD

Integrative Bioinformatics Group, School of Computing Science, Newcastle University

skype: matthew.pocock
tel: (0191) 2566550
Reply all
Reply to author
Forward
0 new messages