在 Scala 中重复列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2491928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 02:00:53  来源:igfitidea点击:

Repeating a List in Scala

scala

提问by Ralph

I am a Scala noob. I have decided to write a spider solitaire solver as a first exercise to learn the language and functional programming in general.

我是 Scala 菜鸟。我决定编写一个蜘蛛纸牌求解器作为学习语言和函数式编程的第一个练习。

I would like to generate a randomly shuffled deck of cards containing 1, 2, or 4 suits. Here is what I came up with:

我想生成一副随机洗牌的牌,其中包含 1、2 或 4 套花色。这是我想出的:

val numberOfSuits = 1
(List("clubs", "diamonds", "hearts", "spades").take(numberOfSuits) * 4).take(4)

which should return

哪个应该返回

List("clubs", "clubs", "clubs", "clubs")
List("clubs", "diamonds", "clubs", "diamonds")
List("clubs", "diamonds", "hearts", "spades")

depending on the value of numberOfSuits, except there is no List "multiply" operation that I can find. Did I miss it? Is there a better way to generate the complete deck before shuffling?

取决于 numberOfSuits 的值,除了我找不到列表“乘法”操作。我错过了吗?有没有更好的方法在洗牌之前生成完整的牌组?

BTW, I plan on using an Enumeration for the suits, but it was easier to type my question with strings. I will take the List generated above and using a for comprehension, iterate over the suits and a similar List of card "ranks" to generate a complete deck.

顺便说一句,我计划为西装使用枚举,但用字符串输入我的问题更容易。我将使用上面生成的 List 并使用 a for comprehension,迭代套装和类似的卡片“等级”列表以生成完整的牌组。

采纳答案by Daniel C. Sobral

You should look up the scaladoc for the object List. It has all manners of interesting methods for creation of lists. For instance, the following does exactly what you were trying to:

您应该查找对象的 Scaladoc List。它有各种有趣的方法来创建列表。例如,以下内容正是您想要做的:

List.flatten(List.make(4, List("clubs", "diamonds", "hearts", "spades").take(numberOfSuits))).take(4)

A much nicer code, however, would be this (Scala 2.7):

然而,一个更好的代码是这样的(Scala 2.7):

val suits = List("clubs", "diamonds", "hearts", "spades")
List.tabulate(4, i => suits.apply(i % numberOfSuits))

On Scala 2.8 tabulateis curried, so the correct syntax would be:

在 Scala 2.8 上tabulate是柯里化的,所以正确的语法是:

List.tabulate(4)(i => suits.apply(i % numberOfSuits))

回答by retronym

Flatten a finite lists of lists:

展平一个有限的列表列表:

scala> List.fill(2)(List(1, 2, 3, 4)).flatten
res18: List[Int] = List(1, 2, 3, 4, 1, 2, 3, 4)

Flatten an infinite Stream of lists, take the first N elements:

展平一个无限的列表流,取前 N 个元素:

scala> Stream.continually(List(1, 2, 3, 4)).flatten.take(8).toList
res19: List[Int] = List(1, 2, 3, 4, 1, 2, 3, 4)

回答by Rex Kerr

You can expand a numeric sequence and flatMapinstead of multiplying.

您可以展开数字序列而flatMap不是乘法。

scala> (1 to 3).flatMap(_=>List(1,2,3,4).take(2)).take(4)
res1: Seq[Int] = List(1, 2, 1, 2)

This works in 2.7.x also.

这也适用于 2.7.x。



Edit: since you're less experienced with Scala, you may not yet have come across the pimp-my-library pattern. If you want to multiply your lists a lot, you can add a custom conversion class:

编辑:由于您对 Scala 的经验较少,您可能还没有遇到 pimp-my-library 模式。如果你想大量增加你的列表,你可以添加一个自定义转换类:

class MultipliableList[T](l: List[T]) {
  def *(n: Int) = (1 to n).flatMap(_=>l).toList
}
implicit def list2multipliable[T](l: List[T]) = new MultipliableList[T](l)

and now you can

现在你可以

scala> List(1,2,3)*4
res2: List[Int] = List(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3)

(Generally, to reuse such implicits, declare them in an object and then import MyObject._ to get the implicit conversion and corresponding class in scope.)

(通常,要重用此类隐式,请在对象中声明它们,然后导入 MyObject._ 以获取范围内的隐式转换和相应的类。)

回答by Mikhail Golubtsov

If you use catslibrary, you can make use of Semigroup's method combineN. It repeates a list Ntimes.

如果使用cats库,则可以使用Semigroup的方法combineN。它重复一个列表N次数。

import cats.implicits._
import cats.syntax.semigroup._

scala> List("clubs", "diamonds", "hearts", "spades").combineN(2)
res1: List[String] = List(clubs, diamonds, hearts, spades, clubs, diamonds, hearts, spades)