for-comprehension

New desugaring behavior in Scala 2.10.1

喜夏-厌秋 提交于 2019-12-03 05:30:57
Suppose I have this monadic class: case class Foo[A](xs: List[A]) { def map[B](f: A => B) = Foo(xs map f) def flatMap[B](f: A => Foo[B]) = Foo(xs flatMap f.andThen(_.xs)) def withFilter(p: A => Boolean) = { println("Filtering!") Foo(xs filter p) } } The following is from a 2.10.0 REPL session: scala> for { (a, b) <- Foo(List(1 -> "x")) } yield a res0: Foo[Int] = Foo(List(1)) And here's the same thing in 2.10.1: scala> for { (a, b) <- Foo(List(1 -> "x")) } yield a Filtering! res0: Foo[Int] = Foo(List(1)) This is completely unexpected (to me), and leads to particularly confusing errors in cases

Scala Future with filter in for comprehension

橙三吉。 提交于 2019-12-02 22:12:52
In the example below I get the exception java.util.NoSuchElementException: Future.filter predicate is not satisfied I want to have the result Future( Test2 ) when the check if( i == 2 ) fails. How do I handle filter/if within a for comprehension that deals with composing futures? Below is a simplified example that works in the Scala REPL. Code: import scala.concurrent.Future import scala.util.{ Try, Success, Failure } import scala.concurrent.ExecutionContext.Implicits.global val f1 = Future( 1 ) val f2 = for { i <- f1 if( i == 2 ) } yield "Test1" f2.recover{ case _ => "Test2" } f2.value tgr In

Pattern match on value of Either inside a for comprehension?

江枫思渺然 提交于 2019-12-01 17:40:34
I have a for comprehension like this: for { (value1: String, value2: String, value3: String) <- getConfigs(args) // more stuff using those values } getConfigs returns an Either[Throwable, (Seq[String], String, String)] and when I try to compile I get this error: value withFilter is not a member of Either[Throwable,(Seq[String], String, String)] How can I use this method (that returns an Either ) in the for comprehension? Like this: for { tuple <- getConfigs() } println(tuple) Joking aside, I think that is an interesting question but it is misnamed a bit. The problem (see above) is not that for

Create collection of cartesian product of two (and more) lists with Java Lambda

末鹿安然 提交于 2019-12-01 14:55:35
I'm able to easily achieve this in Scala with something like: def permute(xs: List[Int], ys: List[Int]) = { for {x <- xs; y <- ys} yield (x,y) } So if I give it {1, 2}, {3, 4} I return {1, 3}, {1, 4}, {2, 3}, {2, 4} I was hoping to be able to translate this to java 8 using streams. I'm having a bit of difficulty and I'd like to be able to extend this out farther as I'd like to be able to generate many permuted test samples from more than two lists. Will it inevitably be a nested mess even using streams or am I not applying myself enough? Some additional answers were found after realizing that

Can I use for-comprehenion / yield to create a map in Scala?

一笑奈何 提交于 2019-11-30 11:21:49
Can I "yield" into a Map? I've tried val rndTrans = for (s1 <- 0 to nStates; s2 <- 0 to nStates if rnd.nextDouble() < trans_probability) yield (s1 -> s2); (and with , instead of -> ) but I get the error TestCaseGenerator.scala:42: error: type mismatch; found : Seq.Projection[(Int, Int)] required: Map[State,State] new LTS(rndTrans, rndLabeling) I can see why, but I can't see how to solve this :-/ scala> (for(i <- 0 to 10; j <- 0 to 10) yield (i -> j)) toMap res1: scala.collection.immutable.Map[Int,Int] = Map((0,10), (5,10), (10,10), (1,10), (6,10), (9,10), (2,10), (7,10), (3,10), (8,10), (4,10)

Clojure For Comprehension example

让人想犯罪 __ 提交于 2019-11-30 10:23:49
I am using docjure and it needs a column map for its select-columns function. I would like to grab all my columns without having to specify it manually. How do I generate the following as a lazy infinite vector sequence [:A :B :C :D :E ... :AA :AB :AC .... :ZZ ... :XFD]? Your question boils down to: " How do I convert a number to a base-26 string with the alphabet A-Z? ". Here's one way to do that - probably not the most concise way, but making it more elegant is left as an exercise for the reader :). Assume that numbers 0-25 map to 'A'-'Z', 26 maps to 'AA', etcetera. First we define a

How to convert this map/flatMap into a for comprehension in Scala?

蓝咒 提交于 2019-11-30 04:49:07
How to convert this map/flatMap into a for comprehension, and please explain how it works, thanks. def compute2(maybeFoo: Option[Foo]): Option[Int] = maybeFoo.flatMap { foo => foo.bar.flatMap { bar => bar.baz.map { baz => baz.compute } } } Your code can be translated into this: def compute2(maybeFoo: Option[Foo]): Option[Int] = for { foo <- maybeFoo bar <- foo.bar baz <- bar.baz } yield baz.compute Quotes from Programming in Scala, Second Edition : Generally, a for expression is of the form: for ( seq ) yield expr Here, seq is a sequence of generators, definitions, and filters, with semi

Using for-comprehension, Try and sequences in Scala

那年仲夏 提交于 2019-11-30 01:28:33
问题 Let's say you've got a bunch of methods: def foo() : Try[Seq[String]] def bar(s:String) : Try[String] and you want to make a for-comprhension: for { list <- foo item <- list result <- bar(item) } yield result of course this won't compile since Seq cannot be used with Try in this context. Anyone has a nice solution how to write this clean without breaking it into separate two for's? I've came across this syntax problem for the thirds time and thought that it's about time to ask about this. 回答1

How can I do 'if..else' inside a for-comprehension?

依然范特西╮ 提交于 2019-11-29 21:04:09
I am asking a very basic question which confused me recently. I want to write a Scala For expression to do something like the following: for (i <- expr1) { if (i.method) { for (j <- i) { if (j.method) { doSomething() } else { doSomethingElseA() } } } else { doSomethingElseB() } } The problem is that, in the multiple generators For expression, I don't know where can I put each for expression body. for {i <- expr1 if(i.method) // where can I write the else logic ? j <- i if (j.method) } doSomething() How can I rewrite the code in Scala Style? Daniel C. Sobral The first code you wrote is

Clojure For Comprehension example

。_饼干妹妹 提交于 2019-11-29 15:09:02
问题 I am using docjure and it needs a column map for its select-columns function. I would like to grab all my columns without having to specify it manually. How do I generate the following as a lazy infinite vector sequence [:A :B :C :D :E ... :AA :AB :AC .... :ZZ ... :XFD]? 回答1: Your question boils down to: " How do I convert a number to a base-26 string with the alphabet A-Z? ". Here's one way to do that - probably not the most concise way, but making it more elegant is left as an exercise for