【问题标题】:walk must return same type as input?walk 必须返回与输入相同的类型?
【发布时间】:2019-12-21 01:32:06
【问题描述】:

我不清楚提供给walkouter 函数应该如何工作。

此处引用的文档中的示例: https://docs.hylang.org/en/stable/contrib/walk.html

建议outer 函数可以是first,它将返回由inner 映射生成的集合的第一个元素。

但是,当我尝试在outer(例如sumfirst)中聚合结果时,我收到如下错误-抱怨int 不可迭代-查看源代码我怀疑这是是因为宏定义中有(type form)

((type form) (outer (HyExpression (map inner form))))

谁能确认并建议是否有办法让outer 为输入form 返回不同的类型?即可以让(walk inc sum [1 2 3 4 5]) 提供列表[2 3 4 5 6] 的总和吗?

=> (walk inc identity [1 2 3 4 5])
[2, 3, 4, 5, 6]
=> (walk inc accumulate [1 2 3 4 5])
[2, 5, 9, 14, 20]
=> (walk inc sum [1 2 3 4 5])
Traceback (most recent call last):
  File "stdin-75eb4a20707c49e8c921e986e7d6164d36b7e4b2", line 1, in <module>
    (walk inc sum [1 2 3 4 5])
  File "/home/phil/.local/lib/python3.6/site-packages/hy/contrib/walk.hy", line 22, in walk
    ((type form) (outer (HyExpression (map inner form))))]
TypeError: 'int' object is not iterable
=> (walk inc first [1 2 3 4 5])
Traceback (most recent call last):
  File "stdin-710dcc990bf071fe1a9a4c5501831c41867f1879", line 1, in <module>
    (walk inc first [1 2 3 4 5])
  File "/home/phil/.local/lib/python3.6/site-packages/hy/contrib/walk.hy", line 22, in walk
    ((type form) (outer (HyExpression (map inner form))))]
TypeError: 'int' object is not iterable
=> 

【问题讨论】:

    标签: hy


    【解决方案1】:

    It's a bug. 改为 (sum (map inc [1 2 3 4 5]))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-23
      • 2022-01-03
      • 1970-01-01
      • 1970-01-01
      • 2020-03-27
      • 1970-01-01
      • 1970-01-01
      • 2019-05-29
      相关资源
      最近更新 更多