macro for (dolist)-like function

For the Compleat Fan
Locked
Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

macro for (dolist)-like function

Post 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?
WBR, Dmi

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

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
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

Of course. :-)

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

Locked