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 8099347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:22:53+00:00 2026-06-05T22:22:53+00:00

I would like to reduce the following boiler plate code, BUT without using lenses

  • 0

I would like to reduce the following boiler plate code, BUT without using lenses (Data.Label). How could I best do this?

data Qcfg = Qcfg { qthresh :: Int, tdelay :: Rational, cwpsq :: TPSQ, cwmap :: TMap, cwchan :: TChan String }

getQthresh = do
   c <- ask
   return (qthresh c)

getDelay = do
   c <- ask
   return (tdelay c)

getTMap = do
   c <- ask
   return (cwmap c)

getTPsq = do
   c <- ask
   return (cwpsq c)

getTChan = do
    c <- ask
    return (cwchan c)
  • 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-05T22:22:56+00:00Added an answer on June 5, 2026 at 10:22 pm

    These are just cases of fmap. These are all equivalent:

    getQthresh = qthresh <$> ask
    
    getQthresh'' = fmap qthresh ask
    
    getQthresh''' = liftM qthresh ask
    
    getQthresh' = do
       c <- ask
       return (qthresh c)
    

    The Data.Functor / Control.Applicative version with <$> is the one you want; there is no boilerplate in it at all if you think about it. And you are indeed wasting space writing functions for each accessor; you just a new way to apply the accessors which fmap/<$> gives you. If you are forever writing <$> ask you can define

     get field = field <$> ask
    

    Maybe that’s what you were looking for now that I think of it. Then

    get qthresh
    

    will be the same as your

    getQthresh
    

    and similarly for the other fields. Of course you could define this get (note that State goes with a different one) in your monadic way:

    get field = do
       c <- ask
       return (field c)
    

    For the specific case of Reader there is asks f which is fmap f ask, and likewise gets for State, but I was taking the question to be about ‘lifting’ accessors into a functor or monad, since it seemed it wasnt plain that do {x <- action; return (f x)} is just f <$> action

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

Sidebar

Related Questions

I would like to reduce the vertical size of a JButton. The following code
I would like to reduce the following code. The below code works, however it
Anyone know how I would be able to reduce my following lines of code?
I would like to do the following (just an example, the real code has
From Python ProblemSet I would like to test the following functions but it seems
I would like to create some kind of structure to reduce the number of
Would like to know the c# code to actually retrieve the IP type: Static
first post here at this great website! I would like to reduce the amount
I have the following boilerplate that I do quite often, and would like to
Instead of having code like the following: if ( $Boss1 ==1) { $Boss1_holder =

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.