monads

Conjuring JQuery Deferred with monadic incantations

我们两清 提交于 2019-12-01 04:12:31
问题 Inspired by this (excellent) discussion of using Promises in javascript, I'm trying to work out how I could use Deferred to chain together async and non-async functions, to avoid paying the callback tax when using my 'Global storage' code. I've got a few questions related to this, but I'll ask them together here, because the context is the same. One thing I can't work out is how I can make a deferred out of something that isn't asynchronous - that is, how do I take a value, wrap it in a

Unable to compile Writer Monad example from “Learn you a Haskell”

[亡魂溺海] 提交于 2019-12-01 03:38:08
The following code, which is verbatim from LYAH , doesn't compile. Code and compile-time error are included below. On the LYAH page, the code is ~15% down the page, yay emacs browser :) Any ideas why? Am I overlooking something totally obvious? (Despite the similarity in post-titles, I think my question is different from this one .) Here's the code (in a file which I named testcopy.hs ) import Control.Monad.Writer logNumber :: Int -> Writer [String] Int logNumber x = Writer (x, ["Got number: " ++ show x]) multWithLog :: Writer [String] Int multWithLog = do a <- logNumber 3 b <- logNumber 5

Side effects in Scala

狂风中的少年 提交于 2019-12-01 02:37:06
I am learning Scala right in these days. I have a slight familiarity with Haskell, although I cannot claim to know it well. Parenthetical remark for those who are not familiar with Haskell One trait that I like in Haskell is that not only functions are first-class citizens, but side effects (let me call them actions) are. An action that, when executed, will endow you with a value of type a , belongs to a specific type IO a . You can pass these actions around pretty much like any other value, and combine them in interesting ways. In fact, combining the side effects is the only way in Haskell to

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

℡╲_俬逩灬. 提交于 2019-12-01 02:17:40
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? 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 package, define a wrapper function writer :: (a, w) -> Writer w a . But I have not found the Monad instance

Is there a standard option workflow in F#?

时光总嘲笑我的痴心妄想 提交于 2019-12-01 02:08:39
Is there an option (maybe) wokflow (monad) in the standrd F# library? I've found a dozen of hand-made implementations ( 1 , 2 ) of this workflow, but I don't really want to introduce non-standard and not very trusted code into my project. And all imaginable queries to google and msdn gave me no clue where to find it. There's no Maybe monad in the standard F# library. You may want to look at FSharpx , a F# extension written by highly-qualified members of F# community, which has quite a number of useful monads. kvb There's no standard computation builder for options, but if you don't need things

Monad more powerful than Applicative?

独自空忆成欢 提交于 2019-12-01 02:02:18
I looked at past discussion but could not see why any of the answers are actually correct. Applicative <*> :: f (a -> b) -> f a -> f b Monad (>>=) :: m a -> (a -> m b) -> m b So if I get it right, the claim is that >>= cannot be written by only assuming the existence of <*> Well, let's assume I have <*> . And I want to create >>= . So I have f a . I have f (a -> b) . Now when you look at it, f (a -> b) can be written as (a -> b) (if something is a function of x, y , z - then it's also a function of x, y). So from the existence of <*> we get (a -> b) -> f a -> f b which again can be written as

Unable to compile Writer Monad example from “Learn you a Haskell”

眉间皱痕 提交于 2019-11-30 23:23:39
问题 The following code, which is verbatim from LYAH, doesn't compile. Code and compile-time error are included below. On the LYAH page, the code is ~15% down the page, yay emacs browser :) Any ideas why? Am I overlooking something totally obvious? (Despite the similarity in post-titles, I think my question is different from this one.) Here's the code (in a file which I named testcopy.hs ) import Control.Monad.Writer logNumber :: Int -> Writer [String] Int logNumber x = Writer (x, ["Got number: "

What advantages does scala.util.Try have over try..catch?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 20:11:19
Searching online for the answer gives two prominent posts ( Codacy's and Daniel Westheide's ), and both give the same answer as Scala's official documentation for Try : An important property of Try shown in the above example is its ability to pipeline, or chain, operations, catching exceptions along the way. The example referenced above is: import scala.io.StdIn import scala.util.{Try, Success, Failure} def divide: Try[Int] = { val dividend = Try(StdIn.readLine("Enter an Int that you'd like to divide:\n").toInt) val divisor = Try(StdIn.readLine("Enter an Int that you'd like to divide by:\n")

How to modify using a monadic function with lenses?

自作多情 提交于 2019-11-30 17:52:47
I needed a lens function that works like over , but with monadic operations: overM :: (Monad m) => Lens s t a b -> (a -> m b) -> (s -> m t) While this function is easy to define (it's actually just an identity modulo WrappedMonad ), I wonder are such functions defined somewhere in lens ? {-# LANGUAGE RankNTypes #-} import Control.Applicative import Control.Lens overF :: (Functor f) => Lens s t a b -> (a -> f b) -> (s -> f t) overF l = l overM :: (Monad m) => Lens s t a b -> (a -> m b) -> (s -> m t) overM l = (unwrapMonad .) . l . (WrapMonad .) in Control.Lens.Traversal: traverseOf :: Over p f

MonadPlus definition for Haskell IO

馋奶兔 提交于 2019-11-30 17:42:15
I was just writing a quick bit of code, and I wanted to use the guard function in the IO Monad. However, there is no definition of MonadPlus for IO which means that we cannot use guard in IO land. I have seen an example of using the MabyeT transformer to use guard in the Maybe Monad and then lifting all of the IO actions but I do not really want to do that if I do not have to. Some example of what I want might be: handleFlags :: [Flag] -> IO () handleFlags flags = do when (Help `elem` flags) (putStrLn "Usage: program_name options...") guard (Help `elem` flags) ... do stuff ... return () I was