monads

New desugaring behavior in Scala 2.10.1

喜夏-厌秋 提交于 2019-12-03 05:30:57
Suppose I have this monadic class: case class Foo[A](xs: List[A]) { def map[B](f: A => B) = Foo(xs map f) def flatMap[B](f: A => Foo[B]) = Foo(xs flatMap f.andThen(_.xs)) def withFilter(p: A => Boolean) = { println("Filtering!") Foo(xs filter p) } } The following is from a 2.10.0 REPL session: scala> for { (a, b) <- Foo(List(1 -> "x")) } yield a res0: Foo[Int] = Foo(List(1)) And here's the same thing in 2.10.1: scala> for { (a, b) <- Foo(List(1 -> "x")) } yield a Filtering! res0: Foo[Int] = Foo(List(1)) This is completely unexpected (to me), and leads to particularly confusing errors in cases

What is Crockford's law?

元气小坏坏 提交于 2019-12-03 05:26:56
Someone referenced "Crockford's law" recently with respect to monads. Google shows very little in the way of results. Anyone know what it is? Assuming "Crockford's Law" is The Curse that he mentions early in the video, he's referring to this common occurrence (described much more eloquently here ): person X doesn't understand monads person X works long and hard, and groks monads person X experiences amazing feeling of enlightenment, wonders why others are not similarly enlightened person X gives horrible, incomplete, inaccurate, oversimplified, and confusing explanation of monads to others

Do monad transformers, generally speaking, arise out of adjunctions?

强颜欢笑 提交于 2019-12-03 05:14:29
In Adjoint functors determine monad transformers, but where's lift? , Simon C has shown us the construction... newtype Three u f m a = Three { getThree :: u (m (f a)) } ... which, as the answers there discuss, can be given an instance Adjunction f u => MonadTrans (Three u f) ( adjunctions provides it as AdjointT ). Any Hask/Hask adjunction thus leads to a monad transformer; in particular, StateT s arises in this manner from the currying adjunction between (,) s and (->) s . My follow-up question is: does this construction generalise to other monad transformers? Is there a way to derive, say,

Exception or Either monad in C#

非 Y 不嫁゛ 提交于 2019-12-03 05:11:48
问题 I am trying to grok get a preliminary understanding of monads. I have a data layer call whose result I would like to return monadically either as a result eg no of rows updated/dataset etc, or an exception. I figure I need to use the Exception monad which I could see as a special case of the Either monad I've looked round at various samples - tonnes of Maybe samples, and I am not quite sure how or if to generalise this to become an Either monad - but I can't find any which are not in haskell

Do monad transformers apply to getting JSON from services?

痞子三分冷 提交于 2019-12-03 05:00:25
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 methods are returning Promise[Option[Whatever]] . That is, a value of type Whatever that will be,

Is Python's “with” monadic?

帅比萌擦擦* 提交于 2019-12-03 04:57:51
问题 Like many a foolhardy pioneer before me, I'm endeavoring to cross the trackless wasteland that is Understanding Monads. I'm still staggering through, but I can't help noticing a certain monad-like quality about Python's with statement. Consider this fragment: with open(input_filename, 'r') as f: for line in f: process(line) Consider the open() call as the "unit" and the block itself as the "bind". The actual monad isn't exposed (uh, unless f is the monad), but the pattern is there. Isn't it?

Scala IO monad: what's the point?

一世执手 提交于 2019-12-03 04:52:33
问题 I watched a video recently on how you could come up with the IO monad, the talk was in scala. I am actually wondering what the point of having functions return IO[A] out of them. The lambda expressions wrapped in the IO object are what the mutations are and at some point higher up the change they have to be observed, I mean executed, so that something happens. Are you not just pushing the problem higher up the tree somewhere else? The only benefit I can see is that it allows for lazy

Translate from monad to applicative

≡放荡痞女 提交于 2019-12-03 04:52:27
OK, so I know what the Applicative type class contains, and why that's useful. But I can't quite wrap my brain around how you'd use it in a non-trivial example. Consider, for example, the following fairly simple Parsec parser: integer :: Parser Integer integer = do many1 space ds <- many1 digit return $ read ds Now how the heck would you write that without using the Monad instance for Parser ? Lots of people claim that this can be done and is a good idea, but I can't figure out how exactly. integer :: Parser Integer integer = read <$> (many1 space *> many1 digit) Or integer = const read <$>

Can you define `Comonads` based on `Monads`?

一个人想着一个人 提交于 2019-12-03 04:46:34
问题 Okay, so let's say you have the type newtype Dual f a = Dual {dual :: forall r. f(a -> r)->r} As it turns out, when f is a Comonad, Dual f is a Monad (fun exercise). Does it work the other way around? You can define fmap ab (Dual da) = Dual $ \fb -> da $ fmap (. ab) fb and extract (Dual da) = da $ return id , but I do not know how to define duplicate or extend . Is this even possible? If not, what the proof there is not (is there a particular Monad m for which you can prove Dual m is not a

What is so special about Monads?

六眼飞鱼酱① 提交于 2019-12-03 04:39:02
问题 A monad is a mathematical structure which is heavily used in (pure) functional programming, basically Haskell. However, there are many other mathematical structures available, like for example applicative functors, strong monads, or monoids. Some have more specific, some are more generic. Yet, monads are much more popular. Why is that? One explanation I came up with, is that they are a sweet spot between genericity and specificity. This means monads capture enough assumptions about the data