/CHANGES/walk-each

http://github.com/alimoeeny/arc · #! · 28 lines · 26 code · 2 blank · 0 comment · 0 complexity · 0e0f3f04aff04d135f854a8eea6f3612 MD5 · raw file

  1. In vanilla arc, 'each is a macro that expands to code that determines how to
  2. traverse a data structure. This is redundant, and violates the principle of
  3. never using a macro where a function will do; a higher-order fn that took a data
  4. structure and a traversal fn could do the same and would be more generic.
  5. Moreover, such a function could be extended via redefinition to allow new data
  6. structures to be traversed. However, even given such a function, 'each would
  7. still be useful as a convenience macro, to avoid unnecessary use of 'fn.
  8. This hack refactors 'each into a higher-order fn 'walk, which takes an object to
  9. traverse and a function to apply to each part of it, and a simplified 'each
  10. macro that just wraps 'walk. Example:
  11. arc> (walk '(1 2 3) prn)
  12. 1
  13. 2
  14. 3
  15. nil
  16. arc> (let old walk
  17. (def walk (x f)
  18. (old (if (and (isa x 'int) (>= x 0)) (range 1 x) x)
  19. f)))
  20. *** redefining walk
  21. #<procedure: walk>
  22. arc> (walk 3 prn)
  23. 1
  24. 2
  25. 3
  26. nil