返回用户定义的类类型Scala的列表 [英] Return a list of user defined class type Scala

查看:95
本文介绍了返回用户定义的类类型Scala的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to return a list of all the columns which are not full in below function. "isColumnFull" function will check if the list is full or not. GameState is a list of list. I am not sure where is the mistake. Could you please help?

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)} 

推荐答案

如果您想要列的索引:

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)

如果您要使用列,则只需:

If you want the columns, it's just:

def allViableColumns(game: GameState): List[List[String]] = 
  game filterNot isColumnFull

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

Should you decide to use the first version, consider changing (i + 1) to i: there are usually no good reasons for one-based indexing.

这篇关于返回用户定义的类类型Scala的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆