Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

"cl-case" with strings

3 views
Skip to first unread message

Emanuel Berg

unread,
Feb 3, 2016, 8:14:10 PM2/3/16
to help-gn...@gnu.org
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


Dmitry Gutov

unread,
Feb 3, 2016, 8:39:45 PM2/3/16
to help-gn...@gnu.org
On 02/04/2016 04:13 AM, Emanuel Berg wrote:
> 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.

Or you could use pcase.

Marcin Borkowski

unread,
Feb 4, 2016, 2:24:09 AM2/4/16
to Dmitry Gutov, help-gn...@gnu.org
Or this idiom:
(cl-case (intern string)
...)

Best,

--
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University

Emanuel Berg

unread,
Feb 4, 2016, 5:12:26 PM2/4/16
to help-gn...@gnu.org
Marcin Borkowski <mb...@mbork.pl> writes:

>> Or you could use pcase.
>
> Or this idiom: (cl-case (intern string) ...)

Then the keys cannot be strings - not an issue in this
"case"...

The instructions for `pcase' tho isn't exactly like
reading on the milk pack - perhaps the OP or someone
else can provide an example?

Joost Kremers

unread,
Feb 4, 2016, 5:16:54 PM2/4/16
to
http://newartisans.com/2016/01/pattern-matching-with-pcase/

EN:SiS(9)
Emanuel Berg wrote:
> The instructions for `pcase' tho isn't exactly like
> reading on the milk pack - perhaps the OP or someone
> else can provide an example?

--
Joost Kremers joostk...@fastmail.fm
Selbst in die Unterwelt dringt durch Spalten Licht

Stefan Monnier

unread,
Feb 4, 2016, 5:31:14 PM2/4/16
to help-gn...@gnu.org
> The instructions for `pcase' tho isn't exactly like
> reading on the milk pack - perhaps the OP or someone
> else can provide an example?

(pcase foo
("toto" do-the-toto-thing)
("titi" do-the-titi-thing))

For some older Emacsen, IIRC, it might be necessary to do

(pcase foo
(`"toto" do-the-toto-thing)
(`"titi" do-the-titi-thing))


-- Stefan


Emanuel Berg

unread,
Feb 4, 2016, 5:42:11 PM2/4/16
to help-gn...@gnu.org
Stefan Monnier <mon...@iro.umontreal.ca> writes:

>> The instructions for `pcase' tho isn't exactly like
>> reading on the milk pack - perhaps the OP or
>> someone else can provide an example?
>
> (pcase foo ("toto" do-the-toto-thing) ("titi" do-the-titi-thing))

Does it work with multiple keys (keylists) as well?

Here are the line from the original code:

(string-case from
'(("sv" "da") "ord")
'(("en") "word")
'(("es") "palabra") )

Marcin Borkowski

unread,
Feb 4, 2016, 6:04:16 PM2/4/16
to Emanuel Berg, help-gn...@gnu.org

On 2016-02-04, at 23:12, Emanuel Berg <embe...@student.uu.se> wrote:

> Marcin Borkowski <mb...@mbork.pl> writes:
>
>>> Or you could use pcase.
>>
>> Or this idiom: (cl-case (intern string) ...)
>
> Then the keys cannot be strings - not an issue in this
> "case"...

Why not? It works fine for me when `string' is a string.

Emanuel Berg

unread,
Feb 4, 2016, 11:05:52 PM2/4/16
to help-gn...@gnu.org
Marcin Borkowski <mb...@mbork.pl> writes:

>>> Or this idiom: (cl-case (intern string) ...)
>>
>> Then the keys cannot be strings - not an issue in
>> this "case"...
>
> Why not? It works fine for me when `string' is
> a string.

It works for me too but to me it is rather a hack
than idiomatic.

I'd like the keys to be strings if the (original) data
is. Then the keys/strings are also properly
highlighted as strings because that's what they are.

Now when I have my defun I don't want to change for
this, but had I known of this before, perhaps (?)
I wouldn't have written the defun.

Emanuel Berg

unread,
Feb 4, 2016, 11:52:54 PM2/4/16
to help-gn...@gnu.org
Emanuel Berg <embe...@student.uu.se> writes:

> So I wrote this.

Non-recursive version - perhaps (?) better:

(defun string-case-non-recursive (string &rest clauses)
(cl-some (lambda (c)
(let ((keys (car c)))
(and (or (eq t keys)
(member string keys) )
(cadr c) ))) clauses) )

Dmitry Gutov

unread,
Feb 4, 2016, 11:56:02 PM2/4/16
to help-gn...@gnu.org
On 02/05/2016 01:41 AM, Emanuel Berg wrote:

>> (pcase foo ("toto" do-the-toto-thing) ("titi" do-the-titi-thing))
>
> Does it work with multiple keys (keylists) as well?

(pcase "bar"
("toto" do-the-toto-thing)
((or "foo" "bar") do-the-foobar-thing)
("titi" do-the-titi-thing))



Emanuel Berg

unread,
Feb 4, 2016, 11:58:46 PM2/4/16
to help-gn...@gnu.org
Dmitry Gutov <dgu...@yandex.ru> writes:

>> Does it work with multiple keys (keylists) as well?
>
> (pcase "bar"
> ("toto" do-the-toto-thing)
> ((or "foo" "bar") do-the-foobar-thing)
> ("titi" do-the-titi-thing))

Clever!

But I still like mine the best!

Dmitry Gutov

unread,
Feb 5, 2016, 12:02:42 AM2/5/16
to help-gn...@gnu.org
On 02/05/2016 07:58 AM, Emanuel Berg wrote:

> But I still like mine the best!

Of course you do. ;-)

But if you ever write a package to distribute to other users via e.g.
ELPA, using pcase would be a smarter choice.

Marcin Borkowski

unread,
Feb 5, 2016, 3:25:47 AM2/5/16
to Emanuel Berg, help-gn...@gnu.org

On 2016-02-05, at 05:05, Emanuel Berg <embe...@student.uu.se> wrote:

> Marcin Borkowski <mb...@mbork.pl> writes:
>
>>>> Or this idiom: (cl-case (intern string) ...)
>>>
>>> Then the keys cannot be strings - not an issue in
>>> this "case"...
>>
>> Why not? It works fine for me when `string' is
>> a string.
>
> It works for me too but to me it is rather a hack
> than idiomatic.

Well, it was not my idea - I found it somewhere (can't find it right
now). I'm not sure whether this is a hack or the proper way to do it;
I quite like it personally.

> I'd like the keys to be strings if the (original) data
> is. Then the keys/strings are also properly
> highlighted as strings because that's what they are.

Ah, I see now what you meant.

> Now when I have my defun I don't want to change for
> this, but had I known of this before, perhaps (?)
> I wouldn't have written the defun.

:-)

Emanuel Berg

unread,
Feb 5, 2016, 1:20:36 PM2/5/16
to help-gn...@gnu.org
Dmitry Gutov <dgu...@yandex.ru> writes:

> But if you ever write a package to distribute to
> other users via e.g. ELPA, using pcase would be
> a smarter choice.

Yes.

By the way, to anyone who by chance didn't know,
"yandex.ru" is one of those old 90s Internet
"portals".

It is like the Russian Passagen (Sweden) or Yahoo!,
AltaVista, etc. in the US.

But actually Yandex is older so perhaps the comparison
should be in the opposite direction...

Dmitry Gutov

unread,
Feb 5, 2016, 1:37:19 PM2/5/16
to help-gn...@gnu.org
On 02/05/2016 09:20 PM, Emanuel Berg wrote:

> By the way, to anyone who by chance didn't know,
> "yandex.ru" is one of those old 90s Internet
> "portals".

Not really. Maybe it was a "portal" in the 90s and the beginning of 00s.

These days it's a search engine not unlike Google, handling about half
of all search traffic in Russia. It also has a decent hosted email service.

0 new messages