for-comprehension

Scala solution to nQueen using for-comprehension

天涯浪子 提交于 2021-02-10 09:21:19
问题 I have some difficulty in understanding the Scala solution to the n Queens problem, below is the implementation assuming isSafe is defined correctly def queens(n: Int): Set[List[Int]] = { def placeQueens(k: Int): Set[List[Int]] = k match { case 0 => Set(List()) case _ => for { queens <- placeQueens(k - 1) col <- 0 until n if isSafe(col, queens ) }yield k :: queens } placeQueens(n) } The for comprehension, as I have seen, theoretically should return a buffered collection, and I see here it

for comprehension in scala with recursive call

淺唱寂寞╮ 提交于 2021-01-29 03:12:34
问题 I am working on the last project for the coursera course "functional programming in scala". I need to implement a function called combinations that takes a list of character occurrences and output all possible subsets of character occurrences. As an example, the subsets of the occurrence list List(('a', 2), ('b', 2)) are: List( List(), List(('a', 1)), List(('a', 2)), List(('b', 1)), List(('a', 1), ('b', 1)), List(('a', 2), ('b', 1)), List(('b', 2)), List(('a', 1), ('b', 2)), List(('a', 2), (

EitherT with multiple return types

我的未来我决定 提交于 2021-01-28 02:05:19
问题 I am trying to compose futures with for-comprehension and EitherT, but I am having trouble due the return types. Please can someone explain why this does not compile and how can I make it compile changing the for-comprehension? import scala.concurrent.Future import scala.concurrent.ExecutionContext.Implicits.global import cats.data.EitherT import cats.implicits._ object CatsApp extends App { case class L1(a: String) case class L2(a: String) case class L3(a: String) case class R1(num: Int)

Scala: ExecutionContext for future for-comprehension

微笑、不失礼 提交于 2020-07-05 07:42:08
问题 When I make a future , or apply methods like onSuccess and map , I can specify ExecutionContext for them. For example, val f = future { // code } executionContext f.map(someFunction)(executionContext) f onSuccess { // code } executionContext However, if I use a for-comprehension of future, how can I specify ExecutionContext for the yield part? for { f <- future1 g <- future2 } yield { // code to be executed after future1 onSuccess and future2 onSuccess // What ExecutionContext runs this code?

Scala: ExecutionContext for future for-comprehension

天大地大妈咪最大 提交于 2020-07-05 07:40:11
问题 When I make a future , or apply methods like onSuccess and map , I can specify ExecutionContext for them. For example, val f = future { // code } executionContext f.map(someFunction)(executionContext) f onSuccess { // code } executionContext However, if I use a for-comprehension of future, how can I specify ExecutionContext for the yield part? for { f <- future1 g <- future2 } yield { // code to be executed after future1 onSuccess and future2 onSuccess // What ExecutionContext runs this code?

for-comprehension yield raises type mismatch compiler error

喜夏-厌秋 提交于 2020-05-17 03:44:17
问题 I want to extract from Iterable[Try[Int]] a list of all valid values ( Iterable[Int] ) val test = List( Try(8), Try(throw new RuntimeException("foo")), Try(42), Try(throw new RuntimeException("bar")) ) The following is the way to print all valid values from test : for { n <- test p <- n } println(p) // Output // 8 // 42 However, when I tried to save the valid values to list I have received an error: val nums: Seq[Int] = for { n <- list p <- n // Type mismatch. Required: IterableOnce[Int],

for-comprehension yield raises type mismatch compiler error

柔情痞子 提交于 2020-05-17 03:42:08
问题 I want to extract from Iterable[Try[Int]] a list of all valid values ( Iterable[Int] ) val test = List( Try(8), Try(throw new RuntimeException("foo")), Try(42), Try(throw new RuntimeException("bar")) ) The following is the way to print all valid values from test : for { n <- test p <- n } println(p) // Output // 8 // 42 However, when I tried to save the valid values to list I have received an error: val nums: Seq[Int] = for { n <- list p <- n // Type mismatch. Required: IterableOnce[Int],

Can monad with broken associativity law yield incorrect result in for-comprehension?

*爱你&永不变心* 提交于 2020-03-18 15:47:42
问题 Here is a Monad instance for ListT (copied from montrivo) case class ListT[M[_], A](value: M[List[A]]) implicit def listTMonad[M[_]: Monad] = new Monad[ListT[M, *]] { override def flatMap[A, B](fa: ListT[M, A])(f: A => ListT[M, B]): ListT[M, B] = ListT( Monad[M].flatMap[List[A], List[B]](fa.value)( list => Traverse[List].flatTraverse[M, A, B](list)(a => f(a).value) ) ) override def pure[A](a: A): ListT[M, A] = ListT(Monad[M].pure(List(a))) override def tailRecM[A, B](a: A)(f: A => ListT[M,

Can monad with broken associativity law yield incorrect result in for-comprehension?

核能气质少年 提交于 2020-03-18 15:47:07
问题 Here is a Monad instance for ListT (copied from montrivo) case class ListT[M[_], A](value: M[List[A]]) implicit def listTMonad[M[_]: Monad] = new Monad[ListT[M, *]] { override def flatMap[A, B](fa: ListT[M, A])(f: A => ListT[M, B]): ListT[M, B] = ListT( Monad[M].flatMap[List[A], List[B]](fa.value)( list => Traverse[List].flatTraverse[M, A, B](list)(a => f(a).value) ) ) override def pure[A](a: A): ListT[M, A] = ListT(Monad[M].pure(List(a))) override def tailRecM[A, B](a: A)(f: A => ListT[M,

Pattern match on value of Either inside a for comprehension?

荒凉一梦 提交于 2020-01-21 11:06:43
问题 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