gpt4 book ai didi

scala - 有没有比这更好的方法来在列表中平方?

转载 作者:行者123 更新时间:2023-12-04 13:16:49 25 4
gpt4 key购买 nike

我正在尝试简化平方通话。

这是最好的方法吗?

(1 to 10).map(x => x*x)

最佳答案

在某处声明一次:

def sqr(x: Int) = x * x

然后像这样使用它:
(1 to 10).map(sqr)

关于scala - 有没有比这更好的方法来在列表中平方?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17686969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com