Page 1 of 1

Which utilities do you use?

Posted: Wed Dec 10, 2014 8:39 am
by porky11
There are some macros, read-macros and functions written by myself, I use in many programs I write.
I think, every lisper has some files containing utilities.
I am interested, which utilities are the most useful you wrote.

Let me write some examples, I wrote:

A reader macro. I use it, instead of comment, if I want to remove excactly one element or one operation etc.

Code: Select all

(set-dispatch-macro-character #\# #\; (lambda (stream char number)
                                                (declare (ignore char number))
                                                (read stream t)
                                                (values)))
Functions, which create a recursive Function without giving them a name.
f is the recursive function.
The first argument of f is the function to call for recursion

Code: Select all

(defun recurse (f &rest rest)
  (apply f (recursive f) rest))

(defun recursive (f)
  (lambda (&rest rest) (apply #'recurse f rest)))
If you are interested, I could also post more.
I would also like if you have suggestions to improve the code.

Re: Which utilities do you use?

Posted: Sat Dec 20, 2014 3:45 am
by marcoxa
I used to use special home-brewed little thingies.

Now I know better and write everything in a "as-vanilla-as-you-can" style. I also get annoyed by the gratuitous use of non standard and difficult to understand regular macros; specialized syntax-altering reader macros are even more annoying to me :twisted:
Anaphoric macros cause me physical discomfort.

Bottom line: don't.

Having said that, your 'recurse' function is not in that category. It belongs to a library of "Higher Order Functions" of which there are plenty of (improperly) documented versions around.

Cheers

MA

Re: Which utilities do you use?

Posted: Mon Dec 22, 2014 11:27 pm
by pjstirling
Quiet, you curmudgeon! ;)

While undocumented functions and macros that don't observe referential integrity do have a real impact on how hard it is for someone unfamiliar with code to properly understand it, they are also useful to the author (or they wouldn't have been written in the first place [modulo how intelligent the author may be]). It's true that I'm not keen on it in code that I use written by others, but I have written lots of macros that engage in symbol 'magic' for my own use, because the way that I would name variables and functions is so predictable, that I don't see a need to make the choice explicit (and if necessary I can just macroexpand it).

And just to make it clear: I will beat you to death with my keyboard before I let you take away my AIF and AWHEN! :D

My own utility library is on github, though my sql library isn't yet up there.

SCONC is a necessity to me, given how often I need to (CONCATENATE 'STRING ...). I could wish that they had put the functionality into the standard under some name that would fuzzy-complete in less keystrokes.

JOIN, similarly, is used all over the place, though it has no equivalent in the standard.

I'm a LOOP refusenik, so I have a number of macros which handle the common types of iteration, i.e.: WHILE, UNTIL, DOVECTOR, and some collector macros that let you build lists without needing to NREVERSE at the end.

My syntax-unifying BIND macro (that I have previously posted in this forum) has had a couple of new features that are driving its adoption in my code (namely automatic (DECLARE (IGNORE ...)) for variables named with a trailing hyphen, and a way to insert sanity testing code in the middle of what is conceptually sequential binding of variables without causing indentation hell).

Re: Which utilities do you use?

Posted: Tue Dec 23, 2014 4:34 am
by marcoxa
Ananphoric macros mus die! :mrgreen:

Re: Which utilities do you use?

Posted: Thu Dec 25, 2014 4:28 pm
by porky11
marcoxa wrote:"as-vanilla-as-you-can"
I think, thats good in some way, especially for macros, that only help using few brackets (like a arc-like if (instead of if/cond) or a clojure-like let or a let1)
the additional, but unnecessary brackets are also good for visibility(?..<I dont want to google the right english word>)

Re: Which utilities do you use?

Posted: Tue Dec 30, 2014 4:55 am
by marcoxa
Not only that is good. It is good citizenship.

I DO NOT WANT to have 42 WHILE macros (and, of course, AIF and friends are to be dalekized :mrgreen: ) in my environment because I load 42 different libraries from 42 different authors who just couldn't help having their home brewed WHILE instead of using (LOOP WHILE ...)

Cheers
MA

Re: Which utilities do you use?

Posted: Wed Dec 31, 2014 12:43 pm
by pjstirling
marcoxa wrote:Not only that is good. It is good citizenship.

I DO NOT WANT to have 42 WHILE macros (and, of course, AIF and friends are to be dalekized :mrgreen: ) in my environment because I load 42 different libraries from 42 different authors who just couldn't help having their home brewed WHILE instead of using (LOOP WHILE ...)

Cheers
MA
That's a pretty silly complaint, WHILE macros will all compile to the same code, so the only real duplication involved is the macro functions (which are tiny) in each package. The fact that it appears in lots of utility libraries is really a sign that it should have been in the standard (along with DOVECTOR) in the first place!

I don't use LOOP for two reasons:
  • LOOP doesn't indent properly
  • while simple forms are understandable, multiple clauses require remembering how they interact leading to bugs (c.f. http://kvardek-du.kerno.org/2014/12/loop-quiz.html). Worse still, LOOP macroexpands into things like TAGBODY and GOTO (this is not unreasonable in-and-of itself), that are hard for human beings to read (there's a reason that GOTO is considered harmful...)
Oh, and on the subject of REAL technical duplication, a debug build of clang makes ld consume 4GB of RAM while linking (yay for C++).

Re: Which utilities do you use?

Posted: Thu Jan 01, 2015 7:02 pm
by edgar-rft
If I remember right the main reason for creating the Common Lisp Specification was that Lisp vendors had written so many "utility" functions that they were unable to execute each others code (compare the number of symbols in MacLisp and InterLisp to the number of symbols in Lisp 1.5)

I still think that "everything you write to understand something is a good and necessary thing" but if you want other people to read your code you should try to avoid unnecessary "convenience" stuff and try to write vanilla Common Lisp code as much as possible. It's hard enough to remember the nearly 1000 symbols and their possible consequences and side-effects from the Common Lisp Specification and I don't want to learn unnecessary extra stuff if there's no real good reason for that.

If I read code (no matter what programming language) that is obviously overloaded with convenience stuff and painfully forces me to learn lots of unnecessary extra things only to understand the very basics of the code I instantly stop reading and dump the code to the trash.

Of course I have my own megaton of utility functions (as a hardware electrician most of them simulating all sorts of arithmetic errors caused by register owerflow and other nasty things), but I don't use them in public code. Most of them are over-engineered for a myriad of special cases and therefore much too slow for practical code. In public code it's easier to write a vanilla replacement for the particular special case (that hopefully can be understood without special explanations) than to write extensive explantions what the over-engineered "utility" version does.

Don't get me wrong, I'm not against utility functions, but for every extra symbol you introduce you also need to write extra documentation, otherwise your symbol is useless. Writing and maintaining documentation is more work than one might think.

- edgar

Re: Which utilities do you use?

Posted: Mon Jan 05, 2015 3:42 am
by marcoxa
Dear Edgar,

you have the gift of eloquence and moderation. :D I don't :mrgreen:

Re: Which utilities do you use?

Posted: Mon Jan 05, 2015 8:35 am
by JamesF
I'm in Edgar's camp: I only write utilities if I can't find anything pre-existing that somebody else is maintaining.
It's a maintainability thing, both for myself and for anybody else who needs to maintain my code in the future. This includes myself after enough time has passed that I've lost all the state that was in my head when I wrote the code - I've learned this one the hard way.