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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:24:23+00:00 2026-05-29T13:24:23+00:00

I have a function which signature is String -> String -> IO (Maybe String)

  • 0

I have a function which signature is String -> String -> IO (Maybe String)
Now, I use this function to build values for a dictionary and I end up with: [(String,IO (Maybe String))]

I have to analyze the values in the dictionary and based on the result return the appropriate key. I was hoping to just use filter on the Map to step through it but I can’t think of a way to extract that IO action on the fly. So how do I map/filter through the dictionary running the IO action and based on the result of the IO action’s value return appropriate key of the dictionary?? Is there an easy way of doing it or I just got myself in a mess?

Thanks.

  • 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-29T13:24:24+00:00Added an answer on May 29, 2026 at 1:24 pm

    Perhaps the solution is to use sequence with something like

    sequence . map (\(a,mb) -> (mb >>= \b -> return (a,b)))
    

    then you can simply use liftM to apply your filter to the resulting IO [(String,Maybe String)].

    liftM is in Control.Monad. Alternatively, in do notation

    myFilter :: (String,(Maybe String)) -> Bool) 
                -> [(String,IO (Maybe String))] 
                -> IO [(String,(Maybe String))]
    myFilter f ml =
       do
           l <- sequence . map (\(a,mb) -> (mb >>= \b -> return (a,b))) $ ml
           return $ filter f ml
    

    Perhaps some refactoring is in order. Often when working with monads you want to use mapM instead of map.

    There is also a filterM function in Control.Monad. It might be what you need.


    Edit: it was pointed out in comments that

    sequence . map (\(a,mb) -> (mb >>= \b -> return (a,b))) $ ml
    

    is equivalent to

    mapM (\(a,mb) -> fmap ((,) a) mb) ml
    

    so

    myFiter' f = (liftM $ filter f) .  mapM (\(a,mb) -> fmap ((,) a) mb)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I have a function which signature is std::string f(double x, double param1, double
In AS3 you have a function on a string with this signature: function replace(pattern:*,
I have a function which parses one string into two strings. In C# I
i have a function which retrieves values from a webservice , then loops through
I have a function which accepts a string parameter such as: var1=val1 var2=val2 var3='a
I have a function which returns a one-sided intersection of values between two input
I have a function which calculates distance from database records. This function is called
I have a custom EL function defined as follows: <function> ... <function-signature> java.lang.String getAsText(com.test.Outerclass.InnerEnum)
I have a function which searches an STL container then returns the iterator when
I have an function which decodes the encoded base64 data in binary data but

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.