state-monad

State monad and strategy pattern

元气小坏坏 提交于 2019-12-04 05:54:39
I am redesigning a library and I am not happy with the current design pattern. This question concerns the use of the strategy pattern in conjunction with a State monad I have a Filter . All it does, in its basic implementation, is to take a some datafeed of type 'd and update itself, generating a new updated copy of itself. [<AbstractClass>] type Filter<'d, 'F> (state: 'F) = member val StateVariable = state with get abstract member Update: 'd -> Filter<'d, 'F> I have then a ISignalGenerator , that takes a filter, environmental data and process it to generate a Signal of type 'S . type

Scala and State Monad

巧了我就是萌 提交于 2019-12-04 03:12:06
I have been trying to understand the State Monad. Not so much how it is used, though that is not always easy to find, either. But every discussion I find of the State Monad has basically the same information and there is always something I don't understand. Take this post, for example. In it the author has the following: case class State[S, A](run: S => (A, S)) { ... def flatMap[B](f: A => State[S, B]): State[S, B] = State(s => { val (a, t) = run(s) f(a) run t }) ... } I can see that the types line up correctly. However, I don't understand the second run at all. Perhaps I am looking at the

Haskell Data Type With References

谁说我不能喝 提交于 2019-12-03 13:56:19
I'm implementing Ukkonen's algorithm, which requires that all leaves of a tree contain a reference to the same integer, and I'm doing it in Haskell to learn more about the language. However, I'm having a hard time writing out a data type that does this. -- Node has children, indexes of info on the edge -- to it, and an optional suffix link. -- Leaf has a beginning index of the info, but the -- end index is always an incrementing variable index. data STree = Node [STree] (Int, Int) (Maybe STree) | Leaf (Int, ??? ) How can I put the reference in the Leaf type declaration? 来源: https:/

Why must we use state monad instead of passing state directly?

对着背影说爱祢 提交于 2019-12-03 13:29:25
问题 Can someone show a simple example where state monad can be better than passing state directly? bar1 (Foo x) = Foo (x + 1) vs bar2 :: State Foo Foo bar2 = do modify (\(Foo x) -> Foo (x + 1)) get 回答1: State passing is often tedious, error-prone, and hinders refactoring. For example, try labeling a binary tree or rose tree in postorder: data RoseTree a = Node a [RoseTree a] deriving (Show) postLabel :: RoseTree a -> RoseTree Int postLabel = fst . go 0 where go i (Node _ ts) = (Node i' ts', i' +

How to put mutable Vector into State Monad

倾然丶 夕夏残阳落幕 提交于 2019-12-03 13:00:03
问题 I wrote small program in haskell to count all ocurences of Int values in Tree using State Monad with Vector: import Data.Vector import Control.Monad.State import Control.Monad.Identity data Tree a = Null | Node (Tree a) a (Tree a) deriving Show main :: IO () main = do print $ runTraverse (Node Null 5 Null) type MyMon a = StateT (Vector Int) Identity a runTraverse :: Tree Int -> ((),Vector Int) runTraverse t = runIdentity (runStateT (traverse t) (Data.Vector.replicate 7 0)) traverse :: Tree

Combining StateT and State monads

杀马特。学长 韩版系。学妹 提交于 2019-12-03 10:19:39
Lets say I have a function f :: State [Int] Int and a function: g :: StateT [Int] IO Int I want to use f in g and pass the state between them. Is there a library function for StateT (return . runState f) ? Or in general, given a monad transformer with a corresponding monad, is there a library function for it? In even more general, what you're trying to do is apply a transformation to an inner layer of a transformer stack. For two arbitrary monads, the type signature might look something like this: fmapMT :: (MonadTrans t, Monad m1, Monad m2) => (m1 a -> m2 a) -> t m1 a -> t m2 a Basically a

Why does this simple use of the State monad cause a stack overflow?

自作多情 提交于 2019-12-03 08:50:43
问题 I was playing around with the State monad, and I don't know what's causing the stack overflow in this simple piece of code. import Control.Monad.State.Lazy tick :: State Int Int tick = do n <- get put $! (n+1) return n million :: Int million = snd $ runState (mapM_ (const tick) [1..1000000]) 0 main = print million Note I would just like to know what's causing the problem in this piece of code, the task itself is not important per se. 回答1: The problem is that Control.Monad.State.Lazy's (>>=)

Is it possible to implement `(Applicative m) => Applicative (StateT s m)`?

自闭症网瘾萝莉.ら 提交于 2019-12-03 06:01:12
I'm currently working on Data.Fresh and Control.Monad.Trans.Fresh , which resp. define an interface for generating fresh variables, and a monad transformer which implements this interface. I initially thought it would be possible to implement the Applicative instance for my FreshT v m with the only requirement that Applicative m exists. However, I got stuck and it seemed like I need to require Monad m . Not trusting my Haskell-fu, I then turned to the transformers package, and was surprised by what I found in Control.Monad.Trans.State.Lazy and .Strict : instance (Functor m, Monad m) =>

Updating a Big State Fast in Haskell

隐身守侯 提交于 2019-12-03 05:52:50
For my vector graphics library in Haskell I must carry around a rather big state: line stroke parameters, colors, clip path etc. I know two ways of doing this. Quoting a comment from Haskell-cafe : "I would suggest you either use a reader monad with mutable state, or a state monad with immutable state". Here is my problem: updating a big immutable state is a performance kill. Using lots of STRefs is like writing C in Haskell: it's verbose and ugly. Here is the immutable state: data GfxState = GfxState { lineWidth :: Double, lineCap :: Int, color :: Color, clip :: Path, ... } setLineWidth ::

Confusion over the State Monad code on “Learn you a Haskell”

孤人 提交于 2019-12-03 05:44:09
问题 I am trying to get a grasp on Haskell using the online book Learn you a Haskell for great Good. I have, to my knowledge, been able to understand Monads so far until I hit the chapter introducing the State Monad. However, the code presented and claimed to be the Monad implementation of the State type (I have not been able to locate it in Hoogle) seems too much for me to handle. To begin with, I do not understand the logic behind it i.e why it should work and how the author considered this