gpt4 book ai didi

functional-programming - 在 nim 中传递函数

转载 作者:行者123 更新时间:2023-12-04 20:39:22 25 4
gpt4 key购买 nike

我在 Nim(0.10.2 版)中传递数学函数 (procs) 时遇到问题。

import math

var s1 = @[1.1, 1.2, 1.3, 1.4]
var s2 = map(s1, math.sqrt)

我收到错误
Error: 'sqrt' cannot be passed to a procvar

如果我为 sqrt 编写一个包装函数,它就可以正常工作。
proc fxn(x: float): float = math.sqrt(x)
var s2 = map(s1, fxn)

我使用平方根和 map 作为示例,但最终我会将 sqrt(和其他数学过程)传递给另一个过程。有没有办法在不编写包装函数的情况下做到这一点?

最佳答案

有计划通过启用 procvar 使其默认工作。默认情况下的编译指示,并为 C 导入的 procs 制作包装 procvar:https://github.com/nim-lang/Nim/issues/2172

关于functional-programming - 在 nim 中传递函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29496309/

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