macro for (dolist)-like function

Started by Dmi, October 01, 2005, 04:22:09 AM

Previous topic - Next topic

Dmi

I want to write something like:
(dofile (sym filespec opts) (some-code))
to simplify processing of large files with regular substructures (for example - csv or ldif) in record-by-record basis - without reading-in whole file.

where

 sym is symbol name like exactly such one in (dolist) - not predefined and existing locally to dofile contents.

 filespec can be value or expression that results in str-filename or int-fd.

 opts are optional arguments like record delimiter other than n, or buffer size for (read-buf) etc.



My trouble is about a way to handle sym in macro function. Only way I found is to compose lambda like
(let (sym dofile-current-record-value) (some-code))
and to call it in dofile internal loop.

Is this the best choice?
WBR, Dmi

Lutz

#1
That works. You also could use the 'expand' function to redefine symbols in expressions:





newLISP v.8.6.0 on OSX, execute 'newlisp -h' for more info.

> (set 'smbl 'idx)
idx

> (eval (expand '(dolist (smbl '(a b c d e)) (println smbl)) 'smbl))
a
b
c
d
e
e
>

Let me show this step by step

> (set 'prog '(dolist (smbl '(a b c d e)) (println smbl)))
(dolist (smbl '(a b c d e))
 (println smbl))

> (expand prog 'smbl)
(dolist (idx '(a b c d e))
 (println idx))

> (eval (expand prog 'smbl))
a
b
c
d
e
e
>

Lutz

Dmi

#2
Thanks!  Cool!



As I understand, the way with (expand) will break dynamic scoping?

But I don't use it now (probably yet... ;-)
WBR, Dmi

Lutz

#3
'expand' does not relate to dynamic scoping in any way. It is just replacing a symbol in an expression with the content of that symbol's evaluation.



(set 'x 1 'y 2 'z 3)

(expand '(x (y z)) 'x 'y 'z) => (1 (2 3))


Its like a multilevel replace/expansion



Lutz

Dmi

#4
Of course. :-)



But, hmm... I found another cool ways to apply (expand) to my task, than I've seen before!

Thanks again :-)
WBR, Dmi