[Top][All Lists]

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

Re: string> missing?

From: Tassilo Horn
Subject: Re: string> missing?
Date: Thu, 04 Jun 2015 08:43:20 +0200
User-agent: Gnus/5.130014 (Ma Gnus v0.14) Emacs/25.0.50 (gnu/linux)

Eli Zaretskii <address@hidden> writes:

>> Emacs is inconsistent.  That's part of its inheritance.
>> If someone wants to add string>, I'm perfectly OK with it.
> Why not _remove_ string< and use compare-strings for both jobs?

Oh yes, please.  Everybody wants to use a function of 6-7 arguments! ;-)

Fun aside: aside from the reason of symmertry/consistency, I can see at
least two practical advantages of having `string>'.  Whenever it should
be used as predicate of a higher-order function,

  (sort coll #'string>)

is more concise than

  (sort coll (lambda (a b) (string< b a)))

and it is also better to edebug because you are usually not
interested in edebugging the lambda.  (What can possibly go wrong

However, this is true for any other simple predicate aside from number
or string comparison.  So I'd suggest to have a `complement' function
which simply returns a function negating the value of some predicate,

(defun complement (fn)
  "Returns a function calling FN and returning FN's negated value."
  (lambda (&rest args)
    (not (apply fn args))))

Then you can do

  (sort coll (complement #'string<))

which looks like a good compromise wrt conciseness and edebuggability.
Of course, (complement #'string<) is string>= which will work fine for
`sort' but probably not for other higer-order functions.

Grepping the emacs source code, there are indeed at least three
occurrences of (nreverse (sort coll predicate)) which could make use of
that.  And I can also find occurrences of (lambda (a b) (string< b a)).
However, when the number of definitions is what makes a function which
is currently not provided by Elisp important, than it looks to me that
there should be a function for

  (lambda (a b) (string< (car a) (car b)))

For that, `rgrep' delivers 40 matches in the elisp repositories I have
on my disk, and I only searched for

  "(lambda (. .) (string< (car .) (car .)))"

so it has to occur on one line with the exact spacing and 1-char wide
arguments.  So that suggests we want to have a
`car-string-less-than-car' (or indeed want to have the numerical
comparison functions work for strings, too, as Stefan suggested because
there's already `car-less-than-car').


reply via email to

[Prev in Thread] Current Thread [Next in Thread]