io-monad

Scala Cats Effects - IO Async Shift - How Does it Work?

二次信任 提交于 2019-12-04 21:30:26
问题 Here is some Scala cats code using the IO Monad: import java.util.concurrent.{ExecutorService, Executors} import cats.effect.IO import scala.concurrent.{ExecutionContext, ExecutionContextExecutor} import scala.util.control.NonFatal object Program extends App { type CallbackType = (Either[Throwable, Unit]) => Unit // IO.async[Unit] is like a Future that returns Unit on completion. // Unlike a regular Future, it doesn't start to run until unsafeRunSync is called. def forkAsync(toRun: () => Unit

Is it possible to do the IO monad from Haskell in Clojure?

孤人 提交于 2019-12-04 09:26:44
问题 I've had a look at the algo.monads and fluokitten documentation. I've also read through monad blog entries by Jim Duey, Konrad Hinsen and Leonardo Borges. The closest I can find is Konrad Hinsen's library Monadic IO streams - but this doesn't appear to 'implement the monad interface' (for want of a better phrasing) This is example using ST in Haskell oneST :: ST s Int -- note that this works correctly for any s oneST = do var <- newSTRef 0 modifySTRef var (+1) readSTRef var one :: Int one =

What's going on in this type signature? (Vector.Mutable modifiers in Haskell)

纵饮孤独 提交于 2019-12-04 02:12:08
Mutable vectors in Haskell have three element-level mutators: read :: PrimMonad m => MVector (PrimState m) a -> Int -> m a write :: PrimMonad m => MVector (PrimState m) a -> Int -> a -> m () swap :: PrimMonad m => MVector (PrimState m) a -> Int -> Int -> m () Now I can use these fine -- import Data.Vector import Data.Vector.Mutable import Control.Monad.ST import Control.Monad.Primitive incrAt :: Vector Double -> Int -> Vector Double incrAt vec i = runST $ do mvec <- thaw vec oldval <- read mvec i write mvec i (oldval + 1) freeze mvec But what is going on here? What is a PrimMonad ? And is

Scala Cats Effects - IO Async Shift - How Does it Work?

倾然丶 夕夏残阳落幕 提交于 2019-12-03 12:42:35
Here is some Scala cats code using the IO Monad : import java.util.concurrent.{ExecutorService, Executors} import cats.effect.IO import scala.concurrent.{ExecutionContext, ExecutionContextExecutor} import scala.util.control.NonFatal object Program extends App { type CallbackType = (Either[Throwable, Unit]) => Unit // IO.async[Unit] is like a Future that returns Unit on completion. // Unlike a regular Future, it doesn't start to run until unsafeRunSync is called. def forkAsync(toRun: () => Unit)(executor: ExecutorService): IO[Unit] = IO.async[Unit] { callback: CallbackType => // "callback" is a

Is it possible to do the IO monad from Haskell in Clojure?

孤人 提交于 2019-12-03 03:01:17
I've had a look at the algo.monads and fluokitten documentation. I've also read through monad blog entries by Jim Duey , Konrad Hinsen and Leonardo Borges . The closest I can find is Konrad Hinsen's library Monadic IO streams - but this doesn't appear to 'implement the monad interface' (for want of a better phrasing) This is example using ST in Haskell oneST :: ST s Int -- note that this works correctly for any s oneST = do var <- newSTRef 0 modifySTRef var (+1) readSTRef var one :: Int one = runST oneST My question is: Is it possible to do the IO Monad from Haskell in Clojure? Could you

A mystery involving putStrLn

最后都变了- 提交于 2019-12-02 10:13:05
问题 Why does the piece of code below produce the error parse error on input ‘putStrLn’ ? main = do line <- fmap reverse getLine putStrLn $ "You said " ++ line ++ " backwards!" putStrLn $ "Yes, you said " ++ line ++ " backwards!" <interactive>:11:4: error: parse error on input ‘putStrLn’ Also, why does the following piece of code produce the error parse error on input ‘let’ ? main = do line <- getLine let line' = reverse line putStrLn $ "You said " ++ line' ++ " backwards!" putStrLn $ "Yes, you

A mystery involving putStrLn

落爺英雄遲暮 提交于 2019-12-02 07:35:11
Why does the piece of code below produce the error parse error on input ‘putStrLn’ ? main = do line <- fmap reverse getLine putStrLn $ "You said " ++ line ++ " backwards!" putStrLn $ "Yes, you said " ++ line ++ " backwards!" <interactive>:11:4: error: parse error on input ‘putStrLn’ Also, why does the following piece of code produce the error parse error on input ‘let’ ? main = do line <- getLine let line' = reverse line putStrLn $ "You said " ++ line' ++ " backwards!" putStrLn $ "Yes, you said " ++ line' ++ " backwards!" <interactive>:31:4: error: parse error on input ‘let’ Both snippets have

Get value from IO rather than the computation itself

巧了我就是萌 提交于 2019-12-02 06:29:56
问题 Being quite new to Haskell, I'm currently trying to improve my skills by writing an interpreter for a simple imperative toy language. One of the expressions in this language is input , which reads a single integer from standard input. However, when I assign the value of this expression to a variable and then use this variable later, it seems ot me that I actually stored the computation of reading a value rather the read value itself. This means that e.g. the statements x = input; y = x + x;