【问题标题】:passing functions in nim在 nim 中传递函数
【发布时间】:2015-04-07 16:18:32
【问题描述】:

我在 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)

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

【问题讨论】:

    标签: functional-programming parameter-passing nim-lang


    【解决方案1】:

    有计划通过默认启用 procvar pragma 并为 C 导入的 procs 制作包装 procvar 来实现此功能:https://github.com/nim-lang/Nim/issues/2172

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-03
      • 2018-06-15
      • 1970-01-01
      • 2010-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多