unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* "cl-case" with strings
@ 2016-02-04  1:13 Emanuel Berg
  2016-02-04  1:39 ` Dmitry Gutov
  2016-02-05  4:52 ` Emanuel Berg
  0 siblings, 2 replies; 16+ messages in thread
From: Emanuel Berg @ 2016-02-04  1:13 UTC (permalink / raw)
  To: help-gnu-emacs

Today when I did a new interface to my interface [1]
to `google-translate' which is an interface to
Google Translate, I used `cl-case' with strings but
was frustrated with failure, because `cl-case' uses
equality function(s) that report nil for
identical strings.

So I wrote this.

I guess I like it fine - so far.

(defun string-case (string &rest clauses)
  "Search for STRING in CLAUSES and return the corresponding value.\n
CLAUSES is a list of elements of the form ((KEY-1 ... KEY-N) VALUE).
If STRING is a `member' of (KEY-1 ... KEY-N), return VALUE.\n
The default clause is (t DEFAULT-VALUE).
When found, DEFAULT-VALUE is unconditionally returned.
Subsequent clauses are discarded, because clauses are examined left-to-right.
This is also factor if STRING matches keys in several clauses.\n
If there isn't a default clause, nil is returned if STRING isn't a key."
  (if clauses
      (let*((c      (car clauses))
            (keys   (car c)) )
        (if (or (member string keys) ; on match or
                (eq keys t) )        ;    default
            (cadr c)                 ;      return value
          (apply `(string-case ,string ,@(cdr clauses))) ))))

[1] http://user.it.uu.se/~embe8573/conf/emacs-init/translate.el

-- 
underground experts united
http://user.it.uu.se/~embe8573




^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2016-02-05 18:37 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-02-04  1:13 "cl-case" with strings Emanuel Berg
2016-02-04  1:39 ` Dmitry Gutov
2016-02-04  7:23   ` Marcin Borkowski
2016-02-04 22:12     ` Emanuel Berg
2016-02-04 22:30       ` Stefan Monnier
2016-02-04 22:41         ` Emanuel Berg
2016-02-05  4:55           ` Dmitry Gutov
2016-02-05  4:58             ` Emanuel Berg
2016-02-05  5:02               ` Dmitry Gutov
2016-02-05 18:20                 ` Emanuel Berg
2016-02-05 18:37                   ` Dmitry Gutov
2016-02-04 23:03       ` Marcin Borkowski
2016-02-05  4:05         ` Emanuel Berg
2016-02-05  8:25           ` Marcin Borkowski
     [not found]     ` <mailman.3642.1454623944.843.help-gnu-emacs@gnu.org>
2016-02-04 22:16       ` Joost Kremers
2016-02-05  4:52 ` Emanuel Berg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).