monads

help with reader monad

耗尽温柔 提交于 2019-11-30 17:38:35
I am new at haskell, I have to write a program context-aware,so I thought I can use the Reader Monad for keeping the context read from a file, I know how to read the file puting the content in a list of tuplessomething like [([Char],[Char])], but I do not know how to implement the Reader Monad for making the environment available to all the components of my program without using imperative style, In particular I do not know how to set and use the environment, as far as I understood I should give it as parameter to all the functions that need the environment with runReader function env, but I

How arbitrary is the “ap” implementation for monads?

三世轮回 提交于 2019-11-30 17:23:50
I am currently studying the bonds between monad and applicative functors. I see two implementation for ap: ap m1 m2 = do { f <- m1 ; x <- m2 ; return (f x) } and ap m1 m2 = do { x <- m2 ; f <- m1 ; return (f x) } The second one is different, yet, would it be a good implementation for <*> ? I got lost in the proof of pure (.) <*> u <*> v <*> w = u <*> (v <*> w) I try to get an intuition of "what part of the monad is the applicative functor"... There are at least three relevant aspects to this question. Given a Monad m instance, what is the specification of its necessary Applicative m superclass

Is this a valid monad transformer in Javascript?

大兔子大兔子 提交于 2019-11-30 15:44:13
In order to better understand monad transformers I implemented one. Since Javascript is dynamically typed I don't mimic type or data constructors but declare only plain old Javascript objects, which hold the corresponding static functions to form a specific monad / transformer. The underlying idea is to apply these methods to a value/values in a container type. Types and containers are separated so to speak. Array s can contain any number of elements. It is trivial to extend Array s so that they implement the monad interface. Array s can also represent the two variants of the maybe type. An

How to make your own for-comprehension compliant scala monad?

喜欢而已 提交于 2019-11-30 15:06:19
问题 I want to implement my own for-comprehension compatible monads and functors in Scala. Let's take two stupid monads as an example. One monad is a state monad that contains an "Int" that you can map or flatmap over. val maybe = IntMonad(5) maybe flatMap( a => 3 * ( a map ( () => 2 * a ) ) ) // returns IntMonad(30) Another monad takes does function composition like so... val func = FunctionMonad( () => println("foo") ) val fooBar = func map ( () => println("bar") ) fooBar() // foo // bar //

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

余生长醉 提交于 2019-11-30 14:48:21
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 computation expressions just like in F#. So after a bit of a struggle I came up with some code that successfully executes as below. using FluentAssertions; using System.Collections.Generic; namespace EnumerableViaAwait.Specs { [global::Microsoft.VisualStudio.TestTools.UnitTesting.TestClass] public class MyTestClass { public IEnumerable<int> Numbers() { return EnumeratorMonad.Build<int>(async

Why there needs to be a $ in calls like “runSomeMonad $ do …”?

房东的猫 提交于 2019-11-30 14:29:35
问题 Apparently the only possible interpretation of runSomeMonad do ... is runSomeMonad (do ...) . Why isn't the first variant allowed by the Haskell syntax? Is there some case where foo do bar could be actually ambiguous? 回答1: Note that you can observe this effect with not just do , but also let , if , \ , case , the extensions mdo and proc …and the dread unary - . I cannot think of a case in which this is ambiguous except for unary - . Here’s how the grammar is defined in the Haskell 2010

How to compose functions that return Option[List] in Scala?

此生再无相见时 提交于 2019-11-30 14:23:28
问题 Suppose I have two functions to get orders and order items: def getOrders(): Option[List[Int]] = ... def getOrderItems(orderId: Int): Option[List[Int]] = ... Note that both functions return Option[List] since each function may fail. Now I would like to get Option of List of all order items as follows: return Some[List] if both functions return Some and None if any of them returns None. I tried to compose these functions with for (see below) but it did not work. val allOrderItems = for {

What's the current status of restricted monads?

你离开我真会死。 提交于 2019-11-30 13:10:12
问题 Going back to at least the late 1990s there have been people wishing for the integration of restricted monads into Haskell in a friendly way. For example, without restricted monads you can't make an efficient monad out of Set , Map or probability distributions. Here's a SO question from a few years ago where someone else ran afoul of this problem. There are various workarounds that people have come up with, including: Creating a new type class for every possible restriction. Using Template

How to use bind with nested monads?

我的梦境 提交于 2019-11-30 13:01:02
I have two functions, one that tries to get a token from a webservice and may fail, and one that tries to use this token to get the username and may fail. getToken :: IO (Maybe Token) getUsername :: Token -> IO (Maybe String) I would like to take the result of getToken and feed it to getUsername . If there was only IO or Maybe , I could simply use bind, but since there are down nested monads, I can't. How can I write something equivalent to getToken >>= getUsername :: IO (Maybe String) ? More generally, what function has type m1 m2 a -> (a -> m1 m2 b) -> m1 m2 b ? Bonus question: how would I

Relax ordering constraints in monadic computation

南楼画角 提交于 2019-11-30 12:33:49
问题 here is some food for thought. When I write monadic code, the monad imposes ordering on the operations done. For example, If I write in the IO monad: do a <- doSomething b <- doSomethingElse return (a + b) I know doSomething will be executed before doSomethingElse . Now, consider the equivalent code in a language like C: return (doSomething() + doSomethingElse()); The semantics of C don't actually specify what order these two function calls will be evaluated, so the compiler is free to move