for-comprehension

Pattern match on value of Either inside a for comprehension?

[亡魂溺海] 提交于 2020-01-21 11:04:09
问题 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? 回答1: Like this: for { tuple <- getConfigs() } println(tuple) Joking

Threading `Try`s through for-comprehension

十年热恋 提交于 2020-01-03 16:45:54
问题 Triggered by another question (which has been subsequently edited away though), I wanted to try out how easy it would be to chain calls to Scala 2.10's Try construct (cf. this presentation), using for-comprehensions. The idea is to have a list of tokens and match them against a sequence of patterns, then return the first error or the successfully matched pattern. I arrived at the following pretty awkward version, and I wonder if this can be made simpler and nicer: import util.Try trait Token

Threading `Try`s through for-comprehension

两盒软妹~` 提交于 2020-01-03 16:44:16
问题 Triggered by another question (which has been subsequently edited away though), I wanted to try out how easy it would be to chain calls to Scala 2.10's Try construct (cf. this presentation), using for-comprehensions. The idea is to have a list of tokens and match them against a sequence of patterns, then return the first error or the successfully matched pattern. I arrived at the following pretty awkward version, and I wonder if this can be made simpler and nicer: import util.Try trait Token

Getting the desugared part of a Scala for/comprehension expression?

安稳与你 提交于 2019-12-27 11:12:43
问题 Does anyone know how to get the (Scala part only) desugared translation of a for/comprehension expression before it actually tries to compile in the REPL (or compiler)? The only thing I've found so far is the compiler "-print" flag but that gives you the full Scala translation… 回答1: As I already said in the other topic, scalac -print prints out scala code, not java. It translates all scala keywords that are not directly compatible with java to normal scala code. It is not possible to let the

Getting the desugared part of a Scala for/comprehension expression?

帅比萌擦擦* 提交于 2019-12-27 11:12:32
问题 Does anyone know how to get the (Scala part only) desugared translation of a for/comprehension expression before it actually tries to compile in the REPL (or compiler)? The only thing I've found so far is the compiler "-print" flag but that gives you the full Scala translation… 回答1: As I already said in the other topic, scalac -print prints out scala code, not java. It translates all scala keywords that are not directly compatible with java to normal scala code. It is not possible to let the

Mock for comprehension in scala

ε祈祈猫儿з 提交于 2019-12-25 02:22:26
问题 I have this piece of code for (element <- table.find; Right(info) = exceptionManager(mapper.getInfoFromDbObject(element))) yield info and I would like to unit test it. I want to mock table.find in order to return a sequence of element that I want. I have tried mocking hasNext() and next() methods of Iterator interface but it seems it is not working. Which is the method to mock a for comprehension? 回答1: Each for comprehension is translated to map , flatMap , filter method calls. So you should

Scala for comprehension efficiency?

两盒软妹~` 提交于 2019-12-23 17:33:31
问题 In the book "Programming In Scala", chapter 23, the author give an example like: case class Book(title: String, authors: String*) val books: List[Book] = // list of books, omitted here // find all authors who have published at least two books for (b1 <- books; b2 <- books if b1 != b2; a1 <- b1.authors; a2 <- b2.authors if a1 == a2) yield a1 The author said, this will translated into: books flatMap (b1 => books filter (b2 => b1 != b2) flatMap (b2 => b1.authors flatMap (a1 => b2.authors filter

Does for comprehension have something like “flatYield”?

一笑奈何 提交于 2019-12-23 14:55:21
问题 I have some code like //all data have different types val data1Future = loadData1(params) val data2Future = loadData2(params) val data3Future = loadData3(params) def saveResult(rez): Future[_] = ??? data1Future.flatMap { data1 => data2Future.flatMap { data2 => data3Future.flatMap { data3 => //do some computation //several rows and several vals val rez = ??? saveResult(rez) } } } But it is a litle bit ugly :) Unfortunatelly, I can't use for comprehension since I need something like "flatYield"

When are scala's for-comprehensions lazy?

梦想与她 提交于 2019-12-20 18:03:14
问题 In Python, I can do something like this: lazy = ((i,j) for i in range(0,10000) for j in range(0,10000)) sum((1 for i in lazy)) It will take a while, but the memory use is constant. The same construct in scala: (for(i<-0 to 10000; j<-i+1 to 10000) yield (i,j)).count((a:(Int,Int)) => true) After a while, I get a java.lang.OutOfMemoryError , even though it should be evaluated lazily. 回答1: Nothing's inherently lazy about Scala's for-comprehension; it's syntactic sugar* which won't change the fact

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

我的未来我决定 提交于 2019-12-19 10:45:36
问题 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