Page 1 of 1

macro for (dolist)-like function

Posted: Sat Oct 01, 2005 11:22 am
by Dmi
I want to write something like:

Code: Select all

(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

Code: Select all

(let (sym dofile-current-record-value) (some-code))
and to call it in dofile internal loop.
Is this the best choice?

Posted: Sat Oct 01, 2005 2:10 pm
by Lutz
That works. You also could use the 'expand' function to redefine symbols in expressions:

Code: Select all

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

Code: Select all

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

Posted: Sat Oct 01, 2005 3:23 pm
by Dmi
Thanks! Cool!

As I understand, the way with (expand) will break dynamic scoping?
But I don't use it now (probably yet... ;-)

Posted: Sat Oct 01, 2005 3:47 pm
by Lutz
'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.

Code: Select all

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

(expand '(x (y z)) 'x 'y 'z) => (1 (2 3))
Its like a multilevel replace/expansion

Lutz

Posted: Sat Oct 01, 2005 4:04 pm
by Dmi
Of course. :-)

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