Scala中最优雅的重复循环

Scala中最优雅的重复循环

本文介绍了Scala中最优雅的重复循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找与之等效的东西

I'm looking for an equivalent of:

for(_ <- 1 to n)
  some.code()

那将是最短和最优雅的. Scala中没有类似的东西吗?

that would be shortest and most elegant. Isn't there in Scala anything similar to this?

rep(n)
  some.code()

这毕竟是最常见的结构之一.

This is one of the most common constructs after all.

PS

我知道实现rep很容易,但是我正在寻找预定义的东西.

I know it's easy to implement rep, but I'm looking for something predefined.

推荐答案

1 to n foreach { _ => some.code() }

这篇关于Scala中最优雅的重复循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 18:04