首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >返回用户定义的类类型Scala的列表

返回用户定义的类类型Scala的列表
EN

Stack Overflow用户
提问于 2018-06-02 07:38:18
回答 1查看 74关注 0票数 1

我正在尝试返回以下函数中未满的所有列的列表。"isColumnFull“函数将检查列表是否已满。GameState是list的列表。我不确定哪里出了错。你能帮帮忙吗?

代码语言:javascript
复制
type GameState = List[List[String]]

case class ColumnNum(index: Int)

val count = 0 //not sure this is needed
def allViableColumns(game: GameState): List[ColumnNum] = 
for((xs, count) <- game.zipWithIndex) yield {if(!isColumnFull(xs))List(count+1)} 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-02 07:42:31

如果您想要列的索引:

代码语言:javascript
复制
type GameState = List[List[String]]
case class ColumnNum(index: Int)
def allViableColumns(game: GameState): List[ColumnNum] = 
  for((xs, i) <- game.zipWithIndex; if !isColumnFull(xs)) yield ColumnNum(i + 1)

如果您想要列,只需:

代码语言:javascript
复制
def allViableColumns(game: GameState): List[List[String]] = 
  game filterNot isColumnFull

如果您决定使用第一个版本,请考虑将(i + 1)更改为i:基于一的索引通常没有很好的理由。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50652163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档