[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Example?



From: Alexey Radul <axofch@xxxxxxxxx>
Subject: Example?
Date: Tue, 22 Sep 2009 08:43:13 -0400

> I wonder about the utility of a facility like this when it has such
> weak guarantees.  Can you give an example of a situation when this
> would be useful without having confidence that the arity information
> is accurate at least for "most" procedures?

Probably it will be much less useful for portable libraries that
don't know how much of procedures have artiy information attached.

If you're writing something to specific implementations,
this feature sometimes comes handy for ad-hoc optimization.

This is something I found I wrote for Gauche.   It makes sense
in Gauche because variable-length arguments are always folded
to list so it incurs allocation overhead.  (This pattern can
be abstracted to a macro and applied to other combinators,
but apparently I didn't bother to do so when I wrote this).

(define (complement fn)
  (case (arity fn) ;; some optimization
    ((0) (lambda () (not (fn))))
    ((1) (lambda (x) (not (fn x))))
    ((2) (lambda (x y) (not (fn x y))))
    (else (lambda args (not (apply fn args))))))

Of course, if the implementation is smart enough to recognize
(lambda args ... (apply fn args)) pattern and to be able to leave
args unfolded in the stack, this kind of ad-hoc optimization
doesn't make sense.   In genreal, as David suggests, it is
useful mainly for introspection in diagnosis, I think.

--shiro