monads

Un-optioning an optioned Option

99封情书 提交于 2019-12-02 20:04:01
Say I have a val s: Option[Option[String]] . It can thus have the following values: Some(Some("foo")) Some(None) None I want to reduce it so that the first becomes Some("foo") while the two others become None . Obviously there are many ways to accomplish this, but I'm looking for a simple, perhaps built-in, less-than-one-liner. It's a shame that flatten doesn't exist. It should. Flatten does exist now. As before, s getOrElse None (in addition to the other answers) will also do the same thing. You could use scalaz join to do this, as this is one of the monadic operations: doubleOpt.join Here it

Valid usage of Optional type in Java 8

微笑、不失礼 提交于 2019-12-02 19:42:15
Is this a valid (intended) usage of Optional type in Java 8? String process(String s) { return Optional.ofNullable(s).orElseGet(this::getDefault); } Stuart Marks I'll take another swing at this. Is this a valid usage? Yes, in the narrow sense that it compiles and produces the results that you're expecting. Is this intended usage? No. Now, sometimes things find usefulness beyond what they were originally for, and if this works out, great. But for Optional , we have found that usually things don't work out very well. Brian Goetz and I discussed some of the issues with Optional in our JavaOne

Confusion over the State Monad code on “Learn you a Haskell”

故事扮演 提交于 2019-12-02 19:04:40
I am trying to get a grasp on Haskell using the online book Learn you a Haskell for great Good . I have, to my knowledge, been able to understand Monads so far until I hit the chapter introducing the State Monad . However, the code presented and claimed to be the Monad implementation of the State type (I have not been able to locate it in Hoogle) seems too much for me to handle. To begin with, I do not understand the logic behind it i.e why it should work and how the author considered this technique.( maybe relevant articles or white-papers can be suggested?) At line 4, it is suggested that

Exception or Either monad in C#

旧城冷巷雨未停 提交于 2019-12-02 18:28:32
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 - and, unfortunately, I most certainly don't grok haskell! I was wondering if anyone could point me to

Scala IO monad: what's the point?

喜夏-厌秋 提交于 2019-12-02 18:08:32
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 evaluation, in the sense that if you do not call the unsafePerformIO operation no side effects occur. Also I

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

◇◆丶佛笑我妖孽 提交于 2019-12-02 17:59:12
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 comonad)? Some observations: Dual IO a is essentially Void (and Const Void is a valid Comonad ). Dual m a

Haskell and State

我是研究僧i 提交于 2019-12-02 17:40:42
Haskell is a pure functional programming language. My question is: What are the advantages and disadvantages of using Haskell to solve problems involving lots of state, for example GUI programming or game programming? Also a secondary question: what methods are there to handle state in a functional way? Thanks in advance. I'm going to answer your second question first. There are actually many ways to handle mutable state in Haskell (and other FP languages). First of all, Haskell does support mutable state in IO, through IORef and mvar constructs. Using these will feel very familiar to

Is Python's “with” monadic?

旧城冷巷雨未停 提交于 2019-12-02 17:27:57
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? Or am I just mistaking all of FP for monadry? Or is it just 3 in the morning and anything seems

How to inject multi dependencies when I use “Reader monad” for dependency injection?

我的梦境 提交于 2019-12-02 17:17:59
I'm trying to use Reader monad for dependency injection, but have problems when the methods requires different dependencies: class PageFetcher { def fetch(url: String) = Reader((dep1: Dep1) => Try { ... }) } class ImageExtractor { def extractImages(html: String) = Reader((deps: (Dep2, Dep3)) => { ... }) } object MyImageFinder { def find(url: String) = Reader((deps: (PageFetcher, ImageExtractor)) => { val (pageFetcher, imageExtractor) = deps for { htmlTry <- pageFetcher.fetch(url) html <- htmlTry images <- imageExtractor.extractImages(html) } yield images }) } // I add these 3 useless

How do you make a generic memoize function in Haskell?

三世轮回 提交于 2019-12-02 17:13:10
I've seen the other post about this , but is there a clean way of doing this in Haskell? As a 2nd part, can it also be done without making the function monadic? This largely follows http://www.haskell.org/haskellwiki/Memoization . You want a function of type (a -> b). If it doesn't call itself, then you can just write a simple wrapper that caches the return values. The best way to store this mapping depends on what properties of a you can exploit. Ordering is pretty much a minimum. With integers you can construct an infinite lazy list or tree holding the values. type Cacher a b = (a -> b) -> a