Convert (a & # 8594; IO b) to IO (a & # 8594; b)

I have several data types in the context of IO, for example:

a :: IO String
b :: IO FilePath
c :: String -> IO String

I want to combine them into one data object, for example:

data Configdata = Configdata String FilePath (String -> String)

Therefore, I do not need to get each value for my own from the I / O context, but just from IO Configdata.

The critical point where I have no solution I can convert String -> IO Stringto IO (String -> String). Hoogle does not give me any features that can do this.

I'm not sure if this is possible, even impossible, since the input of the function is possibly endless.

Does anyone have a solution or explanation why this is not possible? I know that using a list instead of a function is an option, but I would rather use a function if possible.

+4
1

, . :

import Acme.Missiles

boo :: String -> IO String
boo "cute" = return "Who a nice kitty?"
boo "evil" = launchMissiles >> return "HTML tags lea͠ki̧n͘g fr̶ǫm ̡yo​͟ur eye͢s̸ ̛l̕ik͏e liq​uid pain"

, IO (String -> String), IO , String -> String. IOW, , .

, . , , , IO , .

import qualified Data.Map as Map

puh :: IO (String -> String)
puh = fmap ((Map.!) . Map.fromList) . forM ["cute"] $ \q -> do
       res <- boo q
       return (q, res)

, .

+10

Source: https://habr.com/ru/post/1651667/


All Articles