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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:49:13+00:00 2026-05-11T18:49:13+00:00

The filter class of functions takes a condition (a -> Bool) and applies it

  • 0

The filter class of functions takes a condition (a -> Bool) and applies it when filtering.

What is the best way to use a filter on when you have multiple conditions?

Used the applicative function
liftA2 instead of liftM2 because I for some reason didn’t understand how liftM2 worked within pure code.

  • 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-05-11T18:49:13+00:00Added an answer on May 11, 2026 at 6:49 pm

    The liftM2 combinator can be used in the Reader monad to do this in a ‘more functional’ way:

    import Control.Monad
    import Control.Monad.Reader
    
    -- ....
    
    filter (liftM2 (&&) odd (> 100)) [1..200]
    

    Note that the imports are important; Control.Monad.Reader provides the Monad (e ->) instance that makes this all work.

    The reason this works is the reader monad is just (e ->) for some environment e. Thus, a boolean predicate is a 0-ary monadic function returning bool in an environment corresponding to its argument. We can then use liftM2 to distribute the environment over two such predicates.

    Or, in simpler terms, liftM2 will act a bit like this when the types work out:

    liftM2 f g h a = f (g a) (h a)
    

    You can also define a new combinator if you want to be able to chain these easily, and/or don’t want to mess with liftM2:

    (.&&.) :: (a -> Bool) -> (a -> Bool) -> (a -> Bool)
    (.&&.) f g a = (f a) && (g a)
    -- or, in points-free style:
    (.&&.) = liftM2 (&&)    
    
    filter (odd .&&. (> 5) .&&. (< 20)) [1..100]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a filter: class MyFilters { def filters = { before = {
Is there a way construct a predicate to filter by class type? I currently
I have this CSS. the property FILTER In class .crumb make my background and
What is the best way to filter URL input (the $_GET container) in Zend
Silverlight filter/class for cloud generation? Some HLSL or just C# class for generating clouds?
Sorry if this is a dumb question but im creating a filter class that
My filter mapping in web.xml is as follows: <filter> <filter-name>LoginCheckFilter</filter-name> <filter-class>com.tutorial.filter.LoginCheckFilter</filter-class> </filter> <filter-mapping> <filter-name>LoginCheckFilter</filter-name>
I have the following thats within a loop... echo <span class='srch-val'>.apply_filters( $value\n, $value).</span>; I
Consider the following class structure: class Filter { virtual void filter() = 0; virtual
Lets say I have this class (simplified): class Tag (...): children = models.ManyToManyField(null=True, symmetrical=False)

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.