Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Random as instance of scalaz.Monad

Here's a simple example. Suppose I want to pick a random size for a range, and then pick a random index inside that range, and then return both the range and the index. The second computation of a random value clearly depends on the first—I need to know the size of the range in order to pick a value in the range.

This kind of thing is specifically what monadic binding is for—it allows you to write the following:

val rangeAndIndex: Rng[(Range, Int)] =
for {
  max <- Rng.positiveint
  index <- Rng.chooseint(0, max)
} yield (0 to max, index)

This wouldn't be possible if we didn't have a Monad instance for Rng.

Categories : Scala

Related to : Random as instance of scalaz.Monad
How to send a process to multiple sinks in scalaz-stream
You could also use observe and to to attach multiple Sinks to a Process: val p = io.stdInLines.observe(out1).to(out2) observe is like to but echoes what is passed into the sink. So io.stdInLines.observe(out1) still emits the strings that come from stdin (that means it is of type Process[Task, String]) but also sends them to the sink out1. As Eric pointed out, it is also possible to zip Sinks

Categories : Scala
Recursion in a monad
In this particular case, you can just unfold completely. Perhaps this helps: f 3 = { reduce f (and the subtraction) } do v <- getLine vs <- f 2 return $! v : vs = { reduce f (and the subtraction) } do v <- getLine vs <- do v' <- getLine vs' <- f 1 return $! v' : vs' return $! v : vs = { reduce f (and the subtraction

Categories : Haskell
Is Scala Either really a Monad
Yes, it really is - otherwise it would be in scalaz-outlaws. Either's bind is defined something like: trait Either[A, B] { def bind[C](f: B => Either[A, C]) = this match { case Right(b) => f(b) case Left(a) => Left(a) } } (in practice it's defined via a typeclass, but the above definition would work) I guess it's more proper to say that for a fixed A, the type ({type L[B]=

Categories : Scala
Is there a lazy Session IO Monad?
I don't think you can do exactly what you want, since what you describe exploits haskell's lazy evaluation to have the evaluation of a force the actions that compute b and c, and there's no way to seq on unspecified values. What I could do was hack together a monad transformer that delayed actions sequenced via >> so that they could be executed all together: data Session m a = Session { pe

Categories : Haskell
Short-circuit AND within IO Monad
The operation you want is simple to define explicitly. shortCircuitAnd :: Monad m => m Bool -> m Bool -> m Bool shortCircuitAnd x y = do r1 <- x -- perform effect of x if r1 then y -- perform effect of y and return result else return False -- do *not* evaluate or perform effect of y Of course, you can use this function infix as well, using backticks: x `shortCircu

Categories : Haskell
Recently Add
head :: tail pattern matching for strings
What is the easiest and most efficient way to make a min heap in Scala?
Removing mutable collections and making it look more scala style
Reverse list of n elements
scalac: Error: object CharRef in intelliJ 14
Scala - trouble with type inference in lambda expression
Exception on spark test
How can I emulate Haskell's typeclasses in Scala?
Slick: CRUD extension: How to encapsulate implicit mapping:BaseColumnType[T]
Can't find Traverse for sequencing Seq[ValidationNel[String, MyCaseClass]] => ValidationNel[String, Seq[MyCaseClass]]
Tail recursion: internal "loop" function or default values for accumulators
Scala - Add element:MyType to Array = option[MyType] expected
Scala, Composing Function with two values
Using dependent type to generate compile error
How to match all words in a sentence with scala combinators?
Parser Alternative Operator | Fails
ScalaTest assertion mismatch due to Physical Address
Scala implicit parameter and japanese smiley 'foldLeft'
Is it possible to user reduceByKey((x, y, z) => ...)?
How to implement security Authorization using scala and play?
SSO login using scala script
Sum elements based on a predicate
Keep track of completed Futures
API Observable with dynamic caching Remotely closed in gatling
Scala permutations using two lists
Is Scala Either really a Monad
Spark: Use of distinct
Identifying two type wildcards as identical
how to package spark scala application
© Copyright 2017 Publishing Limited. All rights reserved.