PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

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