monads

Has this usage of async / await in C# been discovered before? [closed]

…衆ロ難τιáo~ 提交于 2019-12-18 16:58:47
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . After a previous question on stackoverflow regarding async / await it seemed to me that await was much more powerful and general than the marketing suggested. It seems to be a general method of building

State Monad, why not a tuple?

青春壹個敷衍的年華 提交于 2019-12-18 15:35:55
问题 I've just wrapped my head around monads (at least I'd like to think I have) and more specifically the state monad, which some people that are way smarter then me figured out, so I'm probably way of with this question. Anyway, the state monad is usually implemented with a M<'a> as something like this (F#): type State<'a, 'state> = State of ('state -> 'a * 'state) Now my question: Is there any reason why you couldn't use a tuple here? Other then the possible ambiguity between MonadA<'a, 'b> and

Haskell: monadic takeWhile?

邮差的信 提交于 2019-12-18 12:26:14
问题 I have some functions written in C that I call from Haskell. These functions return IO (CInt) . Sometimes I want to run all of the functions regardless of what any of them return, and this is easy. For sake of example code, this is the general idea of what's happening currently: Prelude> let f x = print x >> return x Prelude> mapM_ f [0..5] 0 1 2 3 4 5 Prelude> I get my desired side effects, and I don't care about the results. But now I need to stop execution immediately after the first item

How to convert this map/flatMap into a for comprehension in Scala?

ぃ、小莉子 提交于 2019-12-18 11:53:19
问题 How to convert this map/flatMap into a for comprehension, and please explain how it works, thanks. def compute2(maybeFoo: Option[Foo]): Option[Int] = maybeFoo.flatMap { foo => foo.bar.flatMap { bar => bar.baz.map { baz => baz.compute } } } 回答1: Your code can be translated into this: def compute2(maybeFoo: Option[Foo]): Option[Int] = for { foo <- maybeFoo bar <- foo.bar baz <- bar.baz } yield baz.compute Quotes from Programming in Scala, Second Edition: Generally, a for expression is of the

Why is the use of Maybe/Option not so pervasive in Clojure?

两盒软妹~` 提交于 2019-12-18 10:32:33
问题 Why does Clojure, despite such an emphasis on functional paradigm, not use the Maybe / Option monad to represent optional values? The use of Option is quite pervasive in Scala, a functional programming language I use regularly. 回答1: Clojure is not statically typed, so doesn't need the strict this/that/whatever type declarations that are necessary in haskell (and, I gather, Scala). If you want to return a string, you return a string; if you return nil instead, that's okay too. "Functional"

Why is there no IO transformer in Haskell?

蓝咒 提交于 2019-12-18 10:26:37
问题 Every other monad comes with a transformer version, and from what I know the idea of a transformer is a generic extension of monads. Following how the other transformers are build, IOT would be something like newtype IOT m a = IOT { runIOT :: m (IO a) } for which I could make up useful applications on the spot: IOT Maybe can either do an IO action or nothing, IOT [] can build a list that can later be sequence d. So why is there no IO transformer in Haskell? (Notes: I've seen this post on

Biapplicative and Bimonad?

末鹿安然 提交于 2019-12-18 10:23:00
问题 Haskell's Data.Bifunctor is basically: class Bifunctor f where bimap :: (a -> c) -> (b -> d) -> f a b -> f c d I could find a Biapply as well. My question is, why isn't there a complete bi-hierarchy (bierarchy?) like: class Bifunctor f => Biapplicative f where bipure :: a -> b -> f a b biap :: f (a -> b) (c -> d) -> f a c -> f b d class Biapplicative m => Bimonad m where bibind :: m a b -> (a -> b -> m c d) -> m c d bireturn :: a -> b -> m a b bireturn = bipure bilift :: Biapplicative f => (a

A simple example showing that IO doesn't satisfy the monad laws?

你离开我真会死。 提交于 2019-12-18 10:16:30
问题 I've seen mentioned that IO doesn't satisfy the monad laws, but I didn't find a simple example showing that. Anybody knows an example? Thanks. Edit: As ertes and n.m. pointed out, using seq is a bit illegal as it can make any monad fail the laws (combined with undefined ). Since undefined may be viewed as a non-terminating computation, it's perfectly fine to use it. So the revised question is: Anybody knows an example showing that IO fails to satisfy the monad laws, without using seq ? (Or

Why do we have map, fmap and liftM?

≯℡__Kan透↙ 提交于 2019-12-18 10:14:56
问题 map :: (a -> b) -> [a] -> [b] fmap :: Functor f => (a -> b) -> f a -> f b liftM :: Monad m => (a -> b) -> m a -> m b Why do we have three different functions that do essentially the same thing? 回答1: map exists to simplify operations on lists and for historical reasons (see What's the point of map in Haskell, when there is fmap?). You might ask why we need a separate map function. Why not just do away with the current list-only map function, and rename fmap to map instead? Well, that’s a good

scala Iterable#map vs. Iterable#flatMap

丶灬走出姿态 提交于 2019-12-18 10:08:23
问题 What is the difference between the map and flatMap functions of Iterable ? 回答1: Here is a pretty good explanation: http://www.codecommit.com/blog/scala/scala-collections-for-the-easily-bored-part-2 Using list as an example: Map's signature is: map [B](f : (A) => B) : List[B] and flatMap's is flatMap [B](f : (A) => Iterable[B]) : List[B] So flatMap takes a type [A] and returns an iterable type [B] and map takes a type [A] and returns a type [B] This will also give you an idea that flatmap will