2017-02-23 5 views
10

मुझे wreq पैकेज का सामना करना पड़ा है। मैं लाइन द्वारा निरंतरता रेखा का मूल्यांकन करना चाहता हूं, लेकिन मुझे इसके लिए कोई रास्ता नहीं मिल रहा है।मैं सीपीएस फ़ंक्शन के साथ आरईपीएल का उपयोग कैसे कर सकता हूं?

import Network.Wreq.Session as S 
withSession $ \sess -> do 
    res <- S.getWith opts sess "http://stackoverflow.com/questions" 
    -- print res 
    -- .. other things 

उपरोक्त स्निपेट में मैं ghci में print res का मूल्यांकन कैसे कर सकता हूं? दूसरे शब्दों में, क्या मुझे ghci में Session टाइप मिल सकता है?

+0

'withSession' हैक automatize के लिए एक GHCi सत्र

> sess <- newEmptyMVar :: IO (MVar Session) > stop <- newEmptyMVar :: IO (MVar()) > forkIO $ withSession $ \s -> putMVar sess s >> takeMVar stop > s <- takeMVar sess > -- use s here as if you were inside withSession > let s =() -- recommended > putMVar stop() > -- we are now "outside" withSession, don't try to access s here! 

एक छोटा सा पुस्तकालय में इस का उपयोग फ़ंक्शन ठीक कार्य है जो एक सत्र प्रदान करता है। 'सत्र' के साथ पारित समारोह के शरीर के भीतर 'sess' पैरामीटर में हेरफेर करके आप सत्र प्राप्त करते हैं। दूसरे शब्दों में, यदि आपके पास 'main = withession ..' है तो कार्रवाई को चलाने के लिए ghci प्रॉम्प्ट पर बस 'main' (या': main') टाइप करें। अब तक "लाइन द्वारा निरंतरता लाइन का मूल्यांकन करें", आपको उस तर्क को स्वयं लागू करना होगा। – user2407038

+0

वाह, अद्भुत सवाल! सीपीएस-शैली पुस्तकालय कार्यों के लिए जीएचसीआई प्रतिलिपि दोबारा दर्ज करने में सक्षम होना वास्तव में अच्छा होगा। – chi

उत्तर

11

अद्भुत सवाल।

मुझे कोई भी तरीका नहीं है जो जीएचसीआई आरईपीएल में फिर से प्रवेश कर सकता है, ताकि हम इसका उपयोग सीपीएस कार्यों में कर सकें। शायद दूसरे कुछ सुझाव दे सकते हैं।

हालांकि, मैं एक हैक का सुझाव दे सकता हूं। असल में, कोई भी इस मामले में आईओ मोनैड पर आधारित होने पर सीपीएस को अंदर लाने के लिए समवर्तीता का फायदा उठा सकता है।

यहाँ हैक है:

data CPSControl b = CPSControl (MVar()) (MVar b) 

startDebugCps :: ((a -> IO()) -> IO b) -> IO (a, CPSControl b) 
startDebugCps cps = do 
    cpsVal <- newEmptyMVar 
    retVal <- newEmptyMVar 
    stop <- newEmptyMVar 
    _ <- forkIO $ do 
     x <- cps $ \c -> putMVar cpsVal c >> takeMVar stop 
     putMVar retVal x 
    s <- takeMVar cpsVal 
    return (s, CPSControl stop retVal) 

stopDebugCps :: CPSControl b -> IO b 
stopDebugCps (CPSControl stop retVal) = do 
    putMVar stop() 
    takeMVar retVal 

testCps :: (String -> IO()) -> IO String 
testCps act = do 
    putStrLn "testCps: begin" 
    act "here's some string!" 
    putStrLn "testCps: end" 
    return "some return value" 

एक त्वरित परीक्षण:

> (x, ctrl) <- startDebugCps testCps 
testCps: begin 
> x 
"here's some string!" 
> stopDebugCps ctrl 
testCps: end 
"some return value" 
संबंधित मुद्दे

 संबंधित मुद्दे