Page 1 of 1

Name for a macro?

Posted: Fri Feb 01, 2008 12:52 am
by Cyril
I have always felt that appending an element to the right of the list or string is a very important operation. What do I like in newlisp, it is its ability to pass -1 as a third argument to 'push'. But writing -1 explicitly is boring, and arguments order seems unnatural. 'push @lst, $elt;' from Perl is much better, but 'push' is already defined in other way in newlisp. Of course I can define my own macro in a trivial way, but I stumbled at a name for it. I am not a native English speaker, so I don't know which verb looks natural to the most of you here. My current solution is 'tack':

Code: Select all

(define-macro (tack _a _b)
  (push (eval _b) (eval _a) -1))

(tack filename ".ext")
Does anyone have a better idea?

Posted: Fri Feb 01, 2008 1:50 am
by Jeff
shove

Posted: Fri Feb 01, 2008 6:33 am
by HPW
Whats wrong with:

(append filename ".ext")

Posted: Fri Feb 01, 2008 1:28 pm
by Cyril
HPW wrote:Whats wrong with:

(append filename ".ext")
Non-destructiveness. I want to modify filename.

Posted: Fri Feb 01, 2008 1:37 pm
by Jeff
concat

Posted: Fri Feb 01, 2008 1:52 pm
by Cyril
Jeff wrote:concat
'concat' and 'append' look like pure functions for me. Seeing such a word in the code, I assume that result is yield and arguments leaved intact. On the other hand, 'push', 'tack' and 'shove' look like actions. More then, like brutal and destructive actions. I readily believe that they are breaking their arguments. But there are my deep-in-mind associations, and I am not a native English speaker. Maybe other people feel it in other way. And yes, I opine that good mnemonics matters.

Posted: Fri Feb 01, 2008 1:52 pm
by Lutz

Code: Select all

> (set 'var "filename")
"filename"
> (write-buffer var ".ext")
4
> var
"filename.ext"
> 
remember 'write-buffer' and 'write-line' do append to a string when given a string instead of a file handle ;-)

Lutz

Posted: Fri Feb 01, 2008 2:03 pm
by Cyril
Lutz wrote:remember 'write-buffer' and 'write-line' do append to a string when given a string instead of a file handle ;-)
Oh, this works! I'll remember this for the rest of my life, I promise! ;-)

Posted: Fri Feb 01, 2008 2:07 pm
by Jeff
Lutz,

Is there a way to set stdout inside an application so that all print and println output is written to a buffer?

Posted: Fri Feb 01, 2008 2:36 pm
by Lutz
There is the 'device' function, but it only works with files:

Code: Select all

> (device (open "buffer" "w"))
3
> (println "hello world")
"hello world"
> (close (device))
true
> !cat buffer
hello world
> 
Lutz

Posted: Fri Feb 01, 2008 2:50 pm
by Jeff
I know. That would be too slow. I was hoping for something more along the lines of using device to assign stdin to a string-buffer so that print would act more like write-buffer.

Posted: Fri Feb 01, 2008 4:48 pm
by m35
Abstracting streams is probably my favorite use of OOP. I suppose the lispy way of doing it is to pass a function that performs the write?

I haven't used FOOP at all yet. I wonder how it would look using that.

Posted: Fri Feb 01, 2008 5:36 pm
by Cyril
m35 wrote:I suppose the lispy way of doing it is to pass a function that performs the write?
I have no clue how other people do it, but my experimental 'sxml2xml' function accepts an output function as argument. It is called like '(sxml2xml print pattern 4)', you can also pass '(curry net-send soc)' or '(curry write-buffer buf)' instead or 'print'.

Posted: Sat Feb 02, 2008 10:53 pm
by Fanda
Jeff wrote:concat
Actually, I would suggest something similar. I like how other LISPs use '!' at the end of their functions to imply destructive behaviour. It makes it very clear. See for example dotlisp:
http://dotlisp.sourceforge.net/dotlisp.htm#Lists

Fanda