for-comprehension

Using Eithers with Scala “for” syntax

有些话、适合烂在心里 提交于 2019-11-29 09:02:52
As I understand it, Scala "for" syntax is extremely similar to Haskell's monadic "do" syntax. In Scala, "for" syntax is often used for List s and Option s. I'd like to use it with Either s, but the necessary methods are not present in the default imports. for { foo <- Right(1) bar <- Left("nope") } yield (foo + bar) // expected result: Left("nope") // instead I get "error: value flatMap is not a member..." Is this functionality available through some import? There is a slight hitch: for { foo <- Right(1) if foo > 3 } yield foo // expected result: Left(???) For a List, it would be List() . For

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

若如初见. 提交于 2019-11-28 18:01:34
问题 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) }

Future[Option] in Scala for-comprehensions

心已入冬 提交于 2019-11-28 04:07:49
I have two functions which return Futures. I'm trying to feed a modified result from first function into the other using a for-yield comprehension. This approach works: val schoolFuture = for { ud <- userStore.getUserDetails(user.userId) sid = ud.right.toOption.flatMap(_.schoolId) s <- schoolStore.getSchool(sid.get) if sid.isDefined } yield s However I'm not happy with having the "if" in there, it seems that I should be able to use a map instead. But when I try with a map: val schoolFuture: Future[Option[School]] = for { ud <- userStore.getUserDetails(user.userId) sid = ud.right.toOption

Using Eithers with Scala “for” syntax

人走茶凉 提交于 2019-11-28 02:24:35
问题 As I understand it, Scala "for" syntax is extremely similar to Haskell's monadic "do" syntax. In Scala, "for" syntax is often used for List s and Option s. I'd like to use it with Either s, but the necessary methods are not present in the default imports. for { foo <- Right(1) bar <- Left("nope") } yield (foo + bar) // expected result: Left("nope") // instead I get "error: value flatMap is not a member..." Is this functionality available through some import? There is a slight hitch: for { foo

What is the idiomatic way to pattern match sequence comprehensions?

十年热恋 提交于 2019-11-27 14:37:22
问题 val x = for(i <- 1 to 3) yield i x match { case 1 :: rest => ... // compile error } constructor cannot be instantiated to expected type; found : collection.immutable.::[B] required: scala.collection.immutable.IndexedSeq[Int] This is the same problem as MatchError when match receives an IndexedSeq but not a LinearSeq. The question is, how to do it right? Adding .toList everywhere doesn't seem right. And creating an own extractor which handles every Seq (as described in the answer of the other

Future[Option] in Scala for-comprehensions

China☆狼群 提交于 2019-11-27 05:19:33
问题 I have two functions which return Futures. I'm trying to feed a modified result from first function into the other using a for-yield comprehension. This approach works: val schoolFuture = for { ud <- userStore.getUserDetails(user.userId) sid = ud.right.toOption.flatMap(_.schoolId) s <- schoolStore.getSchool(sid.get) if sid.isDefined } yield s However I'm not happy with having the "if" in there, it seems that I should be able to use a map instead. But when I try with a map: val schoolFuture:

Cartesian product of two lists

為{幸葍}努か 提交于 2019-11-27 04:25:06
Given a map where a digit is associated to several characters scala> val conversion = Map("0" -> List("A", "B"), "1" -> List("C", "D")) conversion: scala.collection.immutable.Map[java.lang.String,List[java.lang.String]] = Map(0 -> List(A, B), 1 -> List(C, D)) I want to generate all possible character sequences based on a sequence of digits. Examples: "00" -> List("AA", "AB", "BA", "BB") "01" -> List("AC", "AD", "BC", "BD") I can do this with for comprehensions scala> val number = "011" number: java.lang.String = 011 Create a sequence of possible characters per index scala> val values = number

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

佐手、 提交于 2019-11-26 15:57:20
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… 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 compiler translate only parts afaik. But basically a for-comprehension is always translated the same way. A

Type Mismatch on Scala For Comprehension

廉价感情. 提交于 2019-11-26 12:54:39
Why does this construction cause a Type Mismatch error in Scala? for (first <- Some(1); second <- List(1,2,3)) yield (first,second) <console>:6: error: type mismatch; found : List[(Int, Int)] required: Option[?] for (first <- Some(1); second <- List(1,2,3)) yield (first,second) If I switch the Some with the List it compiles fine: for (first <- List(1,2,3); second <- Some(1)) yield (first,second) res41: List[(Int, Int)] = List((1,1), (2,1), (3,1)) This also works fine: for (first <- Some(1); second <- Some(2)) yield (first,second) For comprehensions are converted into calls to the map or

Cartesian product of two lists

戏子无情 提交于 2019-11-26 12:44:20
问题 Given a map where a digit is associated to several characters scala> val conversion = Map(\"0\" -> List(\"A\", \"B\"), \"1\" -> List(\"C\", \"D\")) conversion: scala.collection.immutable.Map[java.lang.String,List[java.lang.String]] = Map(0 -> List(A, B), 1 -> List(C, D)) I want to generate all possible character sequences based on a sequence of digits. Examples: \"00\" -> List(\"AA\", \"AB\", \"BA\", \"BB\") \"01\" -> List(\"AC\", \"AD\", \"BC\", \"BD\") I can do this with for comprehensions