Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7994035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:01:45+00:00 2026-06-04T14:01:45+00:00

Lenses don’t seem to have any disadvantages while having significant advantages over standard Haskell:

  • 0

Lenses don’t seem to have any disadvantages while having significant advantages over standard Haskell: Is there any reason I shouldn’t use lenses wherever possible? Are there performance considerations? Additionally, does template Haskell have any significant overhead?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:01:46+00:00Added an answer on June 4, 2026 at 2:01 pm

    Lenses form an alternative to using direct closures over data constructors. Lenses therefore have approximately the same caveats as using functions and data constructors directly.

    Some of the cons because of this:

    • Every time you modify a lens, you might potentially cause a lot of objects to be (re)created. For example, if you have this data structure:

      A { B { C { bla = "foo" } } }
      

      …and the lens of type Lens A String, you will create a new A, B and C every time you “modify” that lens. This is nothing unusual in Haskell (creating lots of objects), but the object creation is hidden behind the lens, making it hard to spot as a potential performance sink.

    • A lens could also create inefficiencies due to the “mapping function” being used. For example, if you make a lens that modifies the 26th element in a list, it might cause a lot of slowdowns due to the lookup time.

    And the pros:

    • Lenses, in combination with normal records, can be used beautifully with the state monads (see data-lens-fd for an example), and this makes it possible to avoid recreating a lot of objects most of the time, due to extensive data sharing. See for example the focus function, and a similar pattern of using withSomething functions in the Snap Web framework.
    • Lenses obviously don’t actually modify any memory in-place, so they are very useful when you need to reason about state in the context of concurrency. Lenses would therefore be very useful when dealing with graphs of various kinds.

    Lenses are not always isomorphic to closures over data constructors, however. Here are some of the differences (taking data-lens as the implementation here):

    • Most lens implementations use some form of data type to store the “accessor” and “mutator” as a pair. For data-lens, it’s the Store comonad. This means that every time you create a lens, there’ll be a very small extra overhead due to that data structure being created.
    • Because lenses depend on values via some unknown mapping, it might become harder to reason about garbage collection, and you might get (logical) memory leaks because you forgot that you are using a very generic lens that depends on some large chunk of memory. Take, for example, a lens that accesses an element in some large vector, which is composed with another lens that thus hides the first lens, making it difficult to see that the composed lens still depends on the large amount of memory.

    Template Haskell code runs at compile time, and does not affect the runtime performance of lenses whatsoever.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a database with 50 or so lenses and their attributes. I
I'm having a bit of trouble with this one! I have definitely looked in
My company has built a machine that measures wedge and roundness of lenses by
I would like to reduce the following boiler plate code, BUT without using lenses
I have an application in which I am trying to implement ManyToMany relation between
I have the demonstrator code below that fails to compile with the error: Occurs
I have an OpenGL example I wrote years ago demonstrating the lense effect using
is there a good way to gather the mac addresses of machines on a
I have jsTree loading data from a JSON page and it displays correctly. I
I have a pull queue being serviced by a backend and when the queue

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.