我可以在 Scala 中使用以下方法轻松实现这一点:
def permute(xs: List[Int], ys: List[Int]) = {
for {x <- xs; y <- ys} yield (x,y)
}
Run Code Online (Sandbox Code Playgroud)
所以如果我给它 {1, 2}, {3, 4} 我返回 {1, 3}, {1, 4}, {2, 3}, {2, 4}
我希望能够使用流将其转换为 java 8。
我遇到了一些困难,我希望能够将其扩展得更远,因为我希望能够从两个以上的列表中生成许多排列的测试样本。
即使使用流,它是否也不可避免地会成为嵌套的混乱,还是我不够应用自己?
在意识到我正在寻找笛卡尔积后发现了一些额外的答案:
import cats.data.ReaderT
import cats.instances.either._
trait Service1
trait Service2
case class Cats(name:String)
type FailFast[A] = Either[List[String], A]
type Env = (Service1, Service2, Cats)
type ReaderEnvFF[A] = ReaderT[FailFast, Env, A]
def toReaderEnvFF[A](input:A):ReaderEnvFF[A] =
ReaderT((_:Env) => Right(input))
def c:ReaderEnvFF[Cats] =
for {
cats <- toReaderEnvFF((_:Env)._3)
} yield cats // This line is 26
Run Code Online (Sandbox Code Playgroud)
错误:
错误:(26,11)类型不匹配;找到:T1.this.Env => com.savdev.Cats(展开为)(((com.savdev.Service1,com.savdev.Service2,com.savdev.Cats)))=> com.savdev.Cats需要的猫:com .savdev.Cats}产生猫
您能解释一下,为什么猫不是com.savdev.Cats吗?以及为什么在错误中说它被扩展为具有return方法的功能[Cats],而不是botFailFast[Cats]
我尝试应用与此处完全相同的逻辑:
trait Service1 { def s1f = Option(10) }
trait Service2 {
type ReaderS1[A] = ReaderT[Option,Service1,A]
import cats.syntax.applicative._ …Run Code Online (Sandbox Code Playgroud) 我有 2 个期货(db 表上的 2 个操作),我希望在保存修改之前检查两个期货是否已成功完成。
现在,我在第一个(作为依赖)中开始第二个未来,但我知道这不是最好的选择。我知道我可以使用for-comprehension 并行执行两个期货,但即使一个失败,另一个也会被执行(尚未测试)
firstFuture.dropColumn(tableName) match {
case Success(_) => secondFuture.deleteEntity(entity)
case Failure(e) => throw new Exception(e.getMessage)
}
// the first future alters a table, drops a column
// the second future deletes a row from another table
Run Code Online (Sandbox Code Playgroud)
在这种情况下,如果第一个 future 成功执行,第二个可能会失败。我想恢复第一个未来的更新。我听说过 SQL 事务,似乎是这样的,但是如何呢?
val futuresResult = for {
first <- firstFuture.dropColumn(tableName)
second <- secondFuture.deleteEntity(entity)
} yield (first, second)
Run Code Online (Sandbox Code Playgroud)
一个for-comprehension是在我的情况好多了,因为我没有这两个期货之间的依赖关系,可以并行执行,但这不解决我的问题,结果可能是(成功,成功)或(失败,成功)的例子。
scala future concurrent-processing for-comprehension playframework
我在Scala编程第23.5节中读到,map,flatMap和filter操作总是可以转换为for-comprehensions,反之亦然.
我们给出了以下等价物:
def map[A, B](xs: List[A], f: A => B): List[B] =
for (x <- xs) yield f(x)
Run Code Online (Sandbox Code Playgroud)
我有一系列地图操作计算的值:
val r = (1 to 100).map{ i => (1 to 100).map{i % _ == 0} }
.map{ _.foldLeft(false)(_^_) }
.map{ case true => "open"; case _ => "closed" }
Run Code Online (Sandbox Code Playgroud)
我想知道这对于理解是什么样的.我该如何翻译?
(如果它有用,用这个词来说就是:
我想有一种标准的方法来转换地图操作,其中的实际功能的细节并不重要.我可能是错的.)
def combinations(occurrences: List[(Char,Int)]): List[List[(Char,Int)]] = occurrences match {
case Nil => Nil
case x :: xs => for(z <- combinations(xs); y <- occ(x)) yield (y :: z)
}
def occ(e: (Char, Int)): List[(Char, Int)] = (for(i <- 0 to e._2) yield (e._1, i)).toList
Run Code Online (Sandbox Code Playgroud)
嗨,
我在上面的代码片段中找不到任何缺陷,但它仍然给我List()任何输入.
我有四种类型A,B,C和D,将初始值x的类型的Future[Option[A]]和三个功能:f1: A => Option[B],f2: B => Future[Option[C]]和f3: C => D.
如何for从一开始就编写一个理解x结果,这个结果Future[Option[D]]将是三个函数的"组合" 类型的值?
我有一个Future[Future[(String,String)]],我想将它转换为Future[(String,String)]使用for comprehension.
我试图了解for与Scala相关的理解Maps.我有以下代码,我的意图是打破键值对,对值执行某些操作并返回修改后的值Map.我使用正确的功能还是应该使用其他功能?
val kvpair = Map("a" -> 1, "b" -> 2, "c" -> 3)
def multiplyValues(map: Map[Char, Int]) = {
for {
char <- map._1
value <- map._2 * 2
} yield (char, value )
}
Run Code Online (Sandbox Code Playgroud) 请查看以下代码段:
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
object Main extends App {
ids.foreach { l => println(l.mkString(", ")) }
for(l <- ids) println(l.mkString(", "))
def ids = Future(List(1, 2, 3, 4))
}
Run Code Online (Sandbox Code Playgroud)
方法ids返回一个Future[List[Int]],我想打印返回的值List:
ids.map { l => println(l.mkString(", ")) } // prints nothing
for(l <- ids) println(l.mkString(", ")) // prints nothing
Run Code Online (Sandbox Code Playgroud)
问题是上面的任何语句都没有打印List返回的内容ids.我错过了什么吗?
ls = ['abc', 56, 49, 63, 66, 80]
for i in ls:
if(isinstance(i, int) or isinstance(i, float)):
for i in range(len(ls)):
ls[i] = str(ls[i])
Run Code Online (Sandbox Code Playgroud)
我可以知道如何创建上面代码的列表理解吗?
我正在尝试以下但不工作
if (s for s in ls isinstance(s, int) or isinstance(s, float)):
for i in range(len(ls)):
ls[i] = str(ls[i])
Run Code Online (Sandbox Code Playgroud) scala ×9
future ×3
collections ×1
java ×1
lambda ×1
list ×1
python ×1
python-3.x ×1
reader-monad ×1
scala-cats ×1