note
mugwumpjism
<p>I'm not sure this is right.</p>
<p>The e-LISP <tt>mapcar</tt>, according to the info page "(elisp) Mapping Functions", is defined as follows:</p>
<pre> - Function: mapcar FUNCTION SEQUENCE
`mapcar' applies FUNCTION to each element of SEQUENCE in turn, and
returns a list of the results.
The argument SEQUENCE can be any kind of sequence except a
char-table; that is, a list, a vector, a bool-vector, or a string.
The result is always a list. The length of the result is the
same as the length of SEQUENCE.</pre>
<p>IANALH, but that sounds remarkably like Perl's <tt>map</tt> function.</p>
<p>However, it does go on to provide an example which seems to do (almost) the same thing that your <tt>mapcar</tt> does:</p>
<code>(defun mapcar* (function &rest args)
"Apply FUNCTION to successive cars of all ARGS.
Return the list of results."
;; If no list is exhausted,
(if (not (memq 'nil args))
;; apply function to CARs.
(cons (apply function (mapcar 'car args))
(apply 'mapcar* function
;; Recurse for rest of elements.
(mapcar 'cdr args)))))
(mapcar* 'cons '(a b c) '(1 2 3 4))
=> ((a . 1) (b . 2) (c . 3))</code>
<p>Besides that, "car" is an old term coming from the term "Contents of the Address part of the Register". I don't know where you got <tt>mapcaru</tt> from.</p>
<p>Perhaps this is a common lisp vs e-lisp thing, as I found at least one [http://xarch.tu-graz.ac.at/autocad/lisp/transpose.002.html|page] that makes reference to <tt>mapcar</tt> behaving in the way you specify.</p>
<p>I suggest the name <tt>mapshift</tt> instead of <tt>mapcar</tt>, for the version that returns short lists. The other one should perhaps be called something else, like <tt>mapfor</tt> or <tt>mapforeach</tt> perhaps.</p>
<p>Here is a new one, too - <tt>mapeach</tt>, which works on hash refs passed to it:</p>
<code>sub mapeach (&\%)
{
my $sub = shift;
my $hash = shift or do {
require Carp;
Carp::croak( "mapeach: Nothing to map" );
};
my @ret;
while ( my ($k, $v) = each %{$hash}) {
local ($_) = $k;
push @ret, $sub->($k, $v);
}
return wantarray ? @ret : { @ret };
}</code>
44763
44763