monads

How are Scala Futures chained together with flatMap?

眉间皱痕 提交于 2019-12-03 06:36:42
I'm working on using Futures for the first time in Scala and am working through an example of using the flatMap combinator; I've been following this discussion: http://docs.scala-lang.org/overviews/core/futures.html Specifically, this example: val usdQuote = future { connection.getCurrentValue(USD) } val chfQuote = future { connection.getCurrentValue(CHF) } val purchase = for { usd <- usdQuote chf <- chfQuote if isProfitable(usd, chf) } yield connection.buy(amount, chf) purchase onSuccess { case _ => println("Purchased " + amount + " CHF") } is translated to this: val purchase = usdQuote

What is the difference between a monad and a closure?

拟墨画扇 提交于 2019-12-03 06:29:19
i am kinda confused reading the definition between the two. Can they actually intersect in terms of definition? or am i completely lost? Thanks. Closures, as the word tends to be used, are just functions (or blocks of code, if you like) that you can treat like a piece of data and pass to other functions, etc. (the "closed" bit is that wherever you eventually call it, it behaves just as it would if you called it where it was originally defined). A monad is (roughly) more like a context in which functions can be chained together sequentially, and controls how data is passed from one function to

(How) can I make this monadic bind tail-recursive?

余生长醉 提交于 2019-12-03 06:21:45
I have this monad called Desync - [<AutoOpen>] module DesyncModule = /// The Desync monad. Allows the user to define in a sequential style an operation that spans /// across a bounded number of events. Span is bounded because I've yet to figure out how to /// make Desync implementation tail-recursive (see note about unbounded recursion in bind). And /// frankly, I'm not sure if there is a tail-recursive implementation of it... type [<NoComparison; NoEquality>] Desync<'e, 's, 'a> = Desync of ('s -> 's * Either<'e -> Desync<'e, 's, 'a>, 'a>) /// Monadic return for the Desync monad. let internal

Valid usage of Optional type in Java 8

烈酒焚心 提交于 2019-12-03 06:17:10
问题 Is this a valid (intended) usage of Optional type in Java 8? String process(String s) { return Optional.ofNullable(s).orElseGet(this::getDefault); } 回答1: 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

Is a collection with flatMap a monad?

有些话、适合烂在心里 提交于 2019-12-03 06:04:17
Scala has the trait Iterable[A] that defines def flatMap[B](f: (A) ⇒ GenTraversableOnce[B]): Iterable[B] That certainly looks like the bind function on a monad, and the documentation hints that it is a monad, but there are two objections, one minor and one major: the minor one: the return-type of the function passed in is this GenTraversableOnce . I think this is just a convenience that can be overlooked when judging monad-ness. the major one: the "value" of the monad is the list of all the values it contains, but the function is given the values one at a time. Do these problems break the

How to write tail-recursive functions when working inside monads

好久不见. 提交于 2019-12-03 05:58:30
问题 In general I have problems figuring out how to write tailrecursive functions when working 'inside' monads. Here is a quick example: This is from a small example application that I am writing to better understand FP in Scala. First of all the user is prompted to enter a Team consisting of 7 players. This function recursively reads the input: import cats.effect.{ExitCode, IO, IOApp} import cats.implicits._ case class Player (name: String) case class Team (players: List[Player]) /** * Reads a

Why can't there be an instance of MonadFix for the continuation monad?

有些话、适合烂在心里 提交于 2019-12-03 05:55:28
问题 How can we prove that the continuation monad has no valid instance of MonadFix? 回答1: Well actually, it's not that there can't be a MonadFix instance, just that the library's type is a bit too constrained. If you define ContT over all possible r s, then not only does MonadFix become possible, but all instances up to Monad require nothing of the underlying functor : newtype ContT m a = ContT { runContT :: forall r. (a -> m r) -> m r } instance Functor (ContT m) where fmap f (ContT k) = ContT (

Are continuations monads?

馋奶兔 提交于 2019-12-03 05:55:08
问题 Can continuations be said to be monads? Are they a subset of monads or are they simply a way of implementing monads? Edit: Or maybe I got it wrong and monads is a more abstract concept than continuations ? (So I'm really comparing apples to oranges here) 回答1: Briefly, since the 'bind' of a monad takes an effective continuation (a lambda of the 'rest of the computation') as an argument, monads are continuations in that sense. On the flip side, continuation-passing style can be effectively

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

孤人 提交于 2019-12-03 05:44:09
问题 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

Threading extra state through a parser in Scala

一世执手 提交于 2019-12-03 05:40:27
问题 I'll give you the tl;dr up front I'm trying to use the state monad transformer in Scalaz 7 to thread extra state through a parser, and I'm having trouble doing anything useful without writing a lot of t m a -> t m b versions of m a -> m b methods. An example parsing problem Suppose I have a string containing nested parentheses with digits inside them: val input = "((617)((0)(32)))" I also have a stream of fresh variable names (characters, in this case): val names = Stream('a' to 'z': _*) I