scala - 如何在包装到头部时找到跳过某些值的项目的索引
问题描述
我有一个布尔列表
val stacks = List(True, True, False, True, False)
我需要一个带索引的函数,并返回下一个不为假的索引,在达到长度后返回 0。
def nextInvolvedAfter(after: Int): Int = ???
例如:
nextInvolvedAfter(0) // 1
nextInvolvedAfter(1) // 3
nextInvolvedAfter(2) // 3
nextInvolvedAfter(3) // 0
我正在考虑迭代这样的列表:
stacks.drop(after + 1) ++ stacks indexWhere(_)
解决方案
这似乎有效:
(stacks.zipWithIndex.drop(after + 1) ++ stacks.zipWithIndex).find(_._1).get._2
推荐阅读
- c# - WPF/C#:如何绑定字典
> 在数据网格中? - java - 如何防止 Visual Studio 代码中的状态栏颜色在运行任何 Java 文件时变为橙色
- html - Github-托管个人页面-子页面的超链接
- javascript - 类型错误:dart.global.firebase.firestore 不是函数
- vb.net - VB.NET 使用 ReadProcessMemory API
- python - groupby 然后创建一个计数器
- formatting - 如何正确包装涉及键绑定的 Elisp 文档字符串?
- apache-spark - 从 Apache Spark 加载时以科学格式存储在 Hive 表中的小数
- android - Firebase 用户:isEmailVerified 返回 false
- python - 为什么气流没有运行最近的任务