monads

Scala: how to understand the flatMap method of Try?

☆樱花仙子☆ 提交于 2019-12-05 16:16:46
问题 The flatMap method of the Success is implemented like this: def flatMap[U](f: T => Try[U]): Try[U] = try f(value) catch { case NonFatal(e) => Failure(e) } I kinda understand what this method is doing, it helps us to avoid writing a lot of catching code. But in what sense is it similar to the regular flatMap? A regular flatMap takes a sequence of sequences, and put all the elements into one big "flat" sequence. But the flatMap method of Try is not really flattening anything. So, how to

Scotty Using MongoDB

随声附和 提交于 2019-12-05 15:25:10
I'm relatively new to Haskell, and this is my first time working with monad transformers. I'd really appreciate some help. runQuery :: Pipe -> Query -> ActionM (Either Failure [Document]) runQuery pipe query = access pipe master "nutrition" (find query >>= rest) main = do pipe <- runIOE $ connect $ host "127.0.0.1" scotty 3000 $ do post "/" $ do b <- body let user :: Either String User = eitherDecode b case user of Left err -> text . pack $ "Could not decode the user:" ++ err ++ ":\n" ++ (show b) Right u -> do let query::Query = (select ["i_name" =: ["$in" =: map (unpack . name) (foods u)]]

Direct translation of Haskell monad into Scala

风流意气都作罢 提交于 2019-12-05 14:39:58
Trying to learn how to program monads in Scala, got some troubles Given the quick code sample import Control.Monad newtype LJ a = LJ { session :: a } instance Monad LJ where return s = LJ s (>>=) m f = f ( session m ) instance Functor LJ where fmap f m = LJ . f $ session m type SimpleLJ = LJ String auth :: String -> String -> SimpleLJ auth = undefined readFeed :: String -> SimpleLJ readFeed = undefined closeFeed :: String -> SimpleLJ closeFeed = undefined proceed = auth "123" "456" >>= readFeed >>= closeFeed how do I write the same thing in Scala (not scalaz)? As far as I learned, it's enough

Make an arbitrary class in Scala as a monad instance

非 Y 不嫁゛ 提交于 2019-12-05 14:38:18
问题 In order to make anything operable in monad context, if using Haskell - I just add implementation of class Monad for given type anywhere. So I don't touch a source of the data type definition at all. Like (something artificial) data Z a = MyZLeft a | MyZRight a swap (MyZLeft x) = MyZRight x swap (MyZRight x) = MyZLeft x instance Monad Z where return a = MyZRight a (>>=) x f = case x of MyZLeft s -> swap (f s) MyZRight s -> swap (f s) so I'm not touching definition of Z, but make it as a monad

scala Either.RightProjection confusion (for comprehension de-sugaring)

北战南征 提交于 2019-12-05 14:27:56
问题 I can use an = in a scala for-comprehension (as specified in section 6.19 of the SLS) as follows: Option Suppose I have some function String => Option[Int] : scala> def intOpt(s: String) = try { Some(s.toInt) } catch { case _ => None } intOpt: (s: String)Option[Int] Then I can use it thus scala> for { | str <- Option("1") | i <- intOpt(str) | val j = i + 10 //Note use of = in generator | } | yield j res18: Option[Int] = Some(11) It was my understanding that this was essentially equivalent to:

Applicative is to monad what X is to comonad

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 14:23:55
问题 Can we solve this equation for X ? Applicative is to monad what X is to comonad 回答1: After giving it some thought, I think this is actually a backward question. One might think that ComonadApply is to Comonad what Applicative is to Monad , but that is not the case. But to see this, let us use PureScript's typeclass hierarchy: class Functor f where fmap :: (a -> b) -> f a -> f b class Functor f => Apply f where apply :: f (a -> b) -> f a -> f b -- (<*>) class Apply f => Applicative f where

Is this a monad?

一曲冷凌霜 提交于 2019-12-05 13:35:01
问题 I'm trying to understand the concept of monads and I want to know if this code is an implementation of this concept (in JavaScript). I have function M which return new object that have set method which create wrapper method var foo = M().set('getX', function() { return this.x; }).set('setX', function(x) { this.x = x; }).set('addX', function(x) { this.x += x; }); And then I can chain method of foo foo.setX(10).addX(20).addX(30).getX() will return 60 and the same if I have object with methods

Applicative instance for MaybeT m assumes Monad m

时间秒杀一切 提交于 2019-12-05 13:15:44
问题 I've been using the Haxl monad (described here: http://www.reddit.com/r/haskell/comments/1le4y5/the_haxl_project_at_facebook_slides_from_my_talk), which has the interesting feature that <*> for its Applicative instance isn't the same as ap from Control.Monad. This is a key feature that allows it to do concurrent computations without blocking. For example, if hf and ha are long computations, then let hf :: Haxl (a -> b) = ... ha :: Haxl a = ... in do f <- hf a <- ha return (f a) will do them

Implementing an MFunctor instance for RVarT

跟風遠走 提交于 2019-12-05 13:02:01
Is it possible to implement an MFunctor instance for RVarT ? So far I've come up with the following: {-# LANGUAGE RankNTypes #-} import Data.RVar -- from rvar import Control.Monad.Trans.Class (lift) -- from transformers hoistRVarT :: Monad m => (forall t. n t -> m t) -> RVarT n a -> RVarT m a hoistRVarT f rv = sampleRVarTWith (lift . f) rv However this can not be used as a definition of hoist for MFunctor , due to the Monad constraint on m incurred by lift . The problem is, that I couldn't find another way to lift the the resulting monad into RVarT without lift . But I think conceptually it

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

丶灬走出姿态 提交于 2019-12-05 12:46:49
问题 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] -