monads

help with reader monad

本秂侑毒 提交于 2019-11-30 01:34:48
问题 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

Haskell: actual IO monad implementation, in different language?

我是研究僧i 提交于 2019-11-30 01:15:54
How is IO monad actually implemented?in sense of, what would be the actual implementation of the main function? How would I call haskell function (IO) from another language and do I in that case need to maintain IO my self? Does main pulls IO actions (Lazily) as references and then call them? Or it is interpreter job, when it found actions in its way it can call them? Or maybe something else? Is there good IO monad implementation in different language which can help to deeply understand what happening in main function? Edit: Such hGetContents confuses me a lot, and makes me unsure how IO is

How arbitrary is the “ap” implementation for monads?

笑着哭i 提交于 2019-11-30 01:00:09
问题 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"... 回答1: There are at least three relevant aspects to

How to handle `Reader` monad and `Try`?

旧时模样 提交于 2019-11-29 23:19:15
I'm reading this great article about dependency injection in scala with Reader monad . The original example is working well, but I did a little bit change on the return types of the UserRepository.get/find . It was User , but I changed it to Try[User] . Then the code won't be compiled, I had tries many times, but still without lucky. import scala.util.Try import scalaz.Reader case class User(email: String, supervisorId: Int, firstName: String, lastName: String) trait UserRepository { def get(id: Int): Try[User] def find(username: String): Try[User] } trait Users { def getUser(id: Int) = Reader

Monad instance for binary tree

懵懂的女人 提交于 2019-11-29 23:00:51
I built binary tree with: data Tree a = Empty | Node a (Tree a) (Tree a) deriving (Eq, Ord, Read, Show) How can i make Monad type class instance for this tree? And can i make it on not? i try: instance Monad Tree where return x = Node x Empty Empty Empty >>= f = Empty (Node x Empty Empty) >>= f = f x But i can't make (>>=) for Node x left right. Thank you. Edward KMETT There is no (good) monad for the type you just described, exactly. It would require rebalancing the tree and merging together the intermediate trees that are generated by the bind, and you can't rebalance based on any

Can a `ST`-like monad be executed purely (without the `ST` library)?

放肆的年华 提交于 2019-11-29 22:55:52
This post is literate Haskell. Just put in a file like "pad.lhs" and ghci will be able to run it. > {-# LANGUAGE GADTs, Rank2Types #-} > import Control.Monad > import Control.Monad.ST > import Data.STRef Okay, so I was able to figure how to represent the ST monad in pure code. First we start with our reference type. Its specific value is not really important. The most important thing is that PT s a should not be isomorphic to any other type forall s . (In particular, it should be isomorphic to neither () nor Void .) > newtype PTRef s a = Ref {unref :: s a} -- This is defined liked this to make

Monads in C# — why Bind implementations require passed function to return a monad?

荒凉一梦 提交于 2019-11-29 22:27:55
Most examples of monads I saw in C# are written somewhat like that: public static Identity<B> Bind<A, B>(this Identity<A> a, Func<A, Identity<B>> func) { return func(a.Value); } For example, see http://mikehadlow.blogspot.com/2011/01/monads-in-c-3-creating-our-first-monad.html . The question is, what is the point of requiring func to return an Identity<B> ? If I use the following definition: public interface IValue<A> { public IValue<B> Bind<B>(Func<A, B> func) } then I can actually use same func for for Lazy<T> , Task<T> , Maybe<T> etc without actually depending on actual type implementing

Monoid vs MonadPlus [duplicate]

↘锁芯ラ 提交于 2019-11-29 22:22:13
This question already has an answer here: Why MonadPlus and not Monad + Monoid? 3 answers I am very new to both Monads and Monoids and recently also learned about MonadPlus . From what I see, Monoid and MonadPlus both provide a type with a associative binary operation and an identity. (I'd call this a semigroup in mathematical parlance.) So what is the difference between Monoid and MonadPlus ? A semigroup is a structure equipped with an associative binary operation. A monoid is a semigroup with an identity element for the binary operation. Monads and semigroups Every monad has to adhere to the

RxJava - fetch every item on the list

两盒软妹~` 提交于 2019-11-29 22:10:47
I have a method that returns an Observable<ArrayList<Long>> , which are ids of some Items. I'd like to go through this list and download every Item using another method that returns Observable<Item> . How would I do this using RxJava operators? Miguel Lavigne Here's a small self contained example public class Example { public static class Item { int id; } public static void main(String[] args) { getIds() .flatMapIterable(ids -> ids) // Converts your list of ids into an Observable which emits every item in the list .flatMap(Example::getItemObservable) // Calls the method which returns a new

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

让人想犯罪 __ 提交于 2019-11-29 22:10:09
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. 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" does not correspond exactly to "strict compile-time typing". They are orthogonal concepts, and Clojure chooses