monads

Is the `Monad ((,) w)` instance anywhere standard?

不羁岁月 提交于 2019-12-03 23:24:00
问题 I use the pair spelling of Writer all the time, but I always have to instantiate myself: instance (Monoid w) => Monad ((,) w) where return x = (mempty, x) ~(w,x) >>= f = let (w', y) = f x in (w `mappend` w', y) Does this live anywhere in the standard libraries? 回答1: As camccann mentioned in the comment, Control.Monad.Instances defines only the Functor instance. Control.Monad.Applicative defines the Applicative instance. The transformers package, and thus also version 2 and above of the mtl

What is an explicit example of a monad without a monad transformer? [duplicate]

两盒软妹~` 提交于 2019-12-03 23:19:26
This question already has answers here : Closed last year . Is there a monad that doesn't have a corresponding monad transformer (except IO)? (4 answers) Monad transformers are known for all standard monads (Reader, Writer, State, Cont, List, etc.), but each of these monad transformers works in a slightly different way. There is no general method or formula for constructing a monad transformer given a definition of a type constructor with a monad instance. So, it is not assured that a monad data type designed according to some arbitrary business requirements will have a monad transformer. Is

Unlike a Functor, a Monad can change shape?

梦想与她 提交于 2019-12-03 22:36:26
I've always enjoyed the following intuitive explanation of a monad's power relative to a functor: a monad can change shape; a functor cannot. For example: length $ fmap f [1,2,3] always equals 3 . With a monad, however, length $ [1,2,3] >>= g will often not equal 3 . For example, if g is defined as: g :: (Num a) => a -> [a] g x = if x==2 then [] else [x] then [1,2,3] >>= g is equal to [1,3] . The thing that troubles me slightly, is the type signature of g . It seems impossible to define a function which changes the shape of the input, with a generic monadic type such as: h :: (Monad m, Num a)

Scala equivalent of Haskell's do-notation (yet again)

梦想与她 提交于 2019-12-03 22:25:53
I know that Haskell's do x <- [1, 2, 3] y <- [7, 8, 9] let z = (x + y) return z can be expressed in Scala as for { x <- List(1, 2, 3) y <- List(7, 8, 9) z = x + y } yield z But, especially with monads, Haskell often has statements inside the do block that don't correspond to either <- or = . For example, here's some code from Pandoc that uses Parsec to parse something from a string. -- | Parse contents of 'str' using 'parser' and return result. parseFromString :: GenParser tok st a -> [tok] -> GenParser tok st a parseFromString parser str = do oldPos <- getPosition oldInput <- getInput

Why does Haskell not have an I Monad (for input only, unlike the IO monad)?

好久不见. 提交于 2019-12-03 22:24:47
Conceptually, it seems that a computation that performs output is very different from one that performs input only. The latter is, in one sense, much purer. I, for one, would like to have a way to separate the input only parts of my programme from the ones that might actually write something out. So, why is there no input only Monad? Any reason why it wouldn't work to have an I monad (and an O Monad, which could be combined into the IO Monad)? Edit : I mostly meant input as reading files, not interacting with the user. This is also my use case, where I can assume that input files do not change

RxJava - fetch every item on the list

元气小坏坏 提交于 2019-12-03 18:28:33
问题 I have a method that returns an Observable<ArrayList<Long>> , which are ids of some Items. I'd like to go through this list and download every Item using another method that returns Observable<Item> . How would I do this using RxJava operators? 回答1: Here's a small self contained example public class Example { public static class Item { int id; } public static void main(String[] args) { getIds() .flatMapIterable(ids -> ids) // Converts your list of ids into an Observable which emits every item

Is Future in Scala a monad?

瘦欲@ 提交于 2019-12-03 17:53:47
问题 Why and how specifically is a Scala Future not a Monad; and would someone please compare it to something that is a Monad, like an Option? The reason I'm asking is Daniel Westheide's The Neophyte's Guide to Scala Part 8: Welcome to the Future where I asked whether or not a Scala Future was a Monad, and the author responded that it wasn't, which threw off base. I came here to ask for a clarification. 回答1: A summary first Futures can be considered monads if you never construct them with

Propagation of State Monad

孤街浪徒 提交于 2019-12-03 17:13:44
I have the following function for walking around "edges" of the "graph" of my game world. It alters the state of the world--specifically, the player's location. I need to report a message alerting the player of their change in location as well. So I could either return a tuple of (message, newWorld), or I could use a State monad. (Right? I'm new to this stuff.) Here's my attempt at the monad approach: walk dir = do world <- get let attempt = filter (\e -> edgeDirection e == dir) $ edges edges = (worldEdges world) M.! playerLoc playerLoc = playerLocation $ worldPlayer world case attempt of [] -

How can I use parMap with a monadic function?

随声附和 提交于 2019-12-03 16:50:08
问题 I have a monadic function getRate: getRate :: String -> IO Double I'd like to map this function over a list of String's. Normally, I would just do: mapM getRate ["foo", "bar"] but since each call to getRate makes network calls, I'd like to parallelize the map so that each rate is fetched in a separate thread (or at least spread out among queues). I'm thinking of something like parMapM getRate ["foo", "bar"] but there is no parMapM function and parMap doesn't work with monadic functions. What

Do monad transformers apply to getting JSON from services?

让人想犯罪 __ 提交于 2019-12-03 16:29:49
问题 I have a Play! 2 for Scala application that needs to retrieve some data in JSON format from an external service. The Play! framework allows to make HTTP requests asynchronously by wrapping the response in a Promise. Promise is a monad that wraps a value that will be available in the future. This is fine, but in my case what I get from the web service is a JSON string. I have to parse it and the parsing might fail. So I have to wrap whatever I get into an Option . The result is that many of my