Search code examples
hy

walk must return same type as input?


I'm unclear on how the outer function provided to walk should work.

The example in documentation referenced here: https://docs.hylang.org/en/stable/contrib/walk.html

Suggests the outer function can be first which would return the first element of the collection produced by mapping over with inner.

However when I try to aggregate the result in outer (eg sum or first) I get an error as per below - complaining that int is not iterable - looking at the source code I suspect this is because of (type form) in the macro definition:

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

Can anyone confirm, and advise if there is a way of having outer return a different type to the input form? i.e. can (walk inc sum [1 2 3 4 5]) be made to provide the sum of the list [2 3 4 5 6] as I'd expect?

=> (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
=> 

Solution

  • It's a bug. Say (sum (map inc [1 2 3 4 5])) instead.