* "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
* Re: "cl-case" with strings
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-05 4:52 ` Emanuel Berg
1 sibling, 1 reply; 16+ messages in thread
From: Dmitry Gutov @ 2016-02-04 1:39 UTC (permalink / raw)
To: help-gnu-emacs
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.
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: "cl-case" with strings
2016-02-04 1:39 ` Dmitry Gutov
@ 2016-02-04 7:23 ` Marcin Borkowski
2016-02-04 22:12 ` Emanuel Berg
[not found] ` <mailman.3642.1454623944.843.help-gnu-emacs@gnu.org>
0 siblings, 2 replies; 16+ messages in thread
From: Marcin Borkowski @ 2016-02-04 7:23 UTC (permalink / raw)
To: Dmitry Gutov; +Cc: help-gnu-emacs
On 2016-02-04, at 02:39, Dmitry Gutov <dgutov@yandex.ru> wrote:
> 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.
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
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: "cl-case" with strings
2016-02-04 7:23 ` Marcin Borkowski
@ 2016-02-04 22:12 ` Emanuel Berg
2016-02-04 22:30 ` Stefan Monnier
2016-02-04 23:03 ` Marcin Borkowski
[not found] ` <mailman.3642.1454623944.843.help-gnu-emacs@gnu.org>
1 sibling, 2 replies; 16+ messages in thread
From: Emanuel Berg @ 2016-02-04 22:12 UTC (permalink / raw)
To: help-gnu-emacs
Marcin Borkowski <mbork@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?
--
underground experts united
http://user.it.uu.se/~embe8573
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: "cl-case" with strings
2016-02-04 22:12 ` Emanuel Berg
@ 2016-02-04 22:30 ` Stefan Monnier
2016-02-04 22:41 ` Emanuel Berg
2016-02-04 23:03 ` Marcin Borkowski
1 sibling, 1 reply; 16+ messages in thread
From: Stefan Monnier @ 2016-02-04 22:30 UTC (permalink / raw)
To: help-gnu-emacs
> 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
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: "cl-case" with strings
2016-02-04 22:30 ` Stefan Monnier
@ 2016-02-04 22:41 ` Emanuel Berg
2016-02-05 4:55 ` Dmitry Gutov
0 siblings, 1 reply; 16+ messages in thread
From: Emanuel Berg @ 2016-02-04 22:41 UTC (permalink / raw)
To: help-gnu-emacs
Stefan Monnier <monnier@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") )
--
underground experts united
http://user.it.uu.se/~embe8573
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: "cl-case" with strings
2016-02-04 22:41 ` Emanuel Berg
@ 2016-02-05 4:55 ` Dmitry Gutov
2016-02-05 4:58 ` Emanuel Berg
0 siblings, 1 reply; 16+ messages in thread
From: Dmitry Gutov @ 2016-02-05 4:55 UTC (permalink / raw)
To: help-gnu-emacs
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))
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: "cl-case" with strings
2016-02-05 4:55 ` Dmitry Gutov
@ 2016-02-05 4:58 ` Emanuel Berg
2016-02-05 5:02 ` Dmitry Gutov
0 siblings, 1 reply; 16+ messages in thread
From: Emanuel Berg @ 2016-02-05 4:58 UTC (permalink / raw)
To: help-gnu-emacs
Dmitry Gutov <dgutov@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!
--
underground experts united
http://user.it.uu.se/~embe8573
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: "cl-case" with strings
2016-02-05 4:58 ` Emanuel Berg
@ 2016-02-05 5:02 ` Dmitry Gutov
2016-02-05 18:20 ` Emanuel Berg
0 siblings, 1 reply; 16+ messages in thread
From: Dmitry Gutov @ 2016-02-05 5:02 UTC (permalink / raw)
To: help-gnu-emacs
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.
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: "cl-case" with strings
2016-02-05 5:02 ` Dmitry Gutov
@ 2016-02-05 18:20 ` Emanuel Berg
2016-02-05 18:37 ` Dmitry Gutov
0 siblings, 1 reply; 16+ messages in thread
From: Emanuel Berg @ 2016-02-05 18:20 UTC (permalink / raw)
To: help-gnu-emacs
Dmitry Gutov <dgutov@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...
--
underground experts united
http://user.it.uu.se/~embe8573
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: "cl-case" with strings
2016-02-05 18:20 ` Emanuel Berg
@ 2016-02-05 18:37 ` Dmitry Gutov
0 siblings, 0 replies; 16+ messages in thread
From: Dmitry Gutov @ 2016-02-05 18:37 UTC (permalink / raw)
To: help-gnu-emacs
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.
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: "cl-case" with strings
2016-02-04 22:12 ` Emanuel Berg
2016-02-04 22:30 ` Stefan Monnier
@ 2016-02-04 23:03 ` Marcin Borkowski
2016-02-05 4:05 ` Emanuel Berg
1 sibling, 1 reply; 16+ messages in thread
From: Marcin Borkowski @ 2016-02-04 23:03 UTC (permalink / raw)
To: Emanuel Berg; +Cc: help-gnu-emacs
On 2016-02-04, at 23:12, Emanuel Berg <embe8573@student.uu.se> wrote:
> Marcin Borkowski <mbork@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.
Best,
--
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: "cl-case" with strings
2016-02-04 23:03 ` Marcin Borkowski
@ 2016-02-05 4:05 ` Emanuel Berg
2016-02-05 8:25 ` Marcin Borkowski
0 siblings, 1 reply; 16+ messages in thread
From: Emanuel Berg @ 2016-02-05 4:05 UTC (permalink / raw)
To: help-gnu-emacs
Marcin Borkowski <mbork@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.
--
underground experts united
http://user.it.uu.se/~embe8573
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: "cl-case" with strings
2016-02-05 4:05 ` Emanuel Berg
@ 2016-02-05 8:25 ` Marcin Borkowski
0 siblings, 0 replies; 16+ messages in thread
From: Marcin Borkowski @ 2016-02-05 8:25 UTC (permalink / raw)
To: Emanuel Berg; +Cc: help-gnu-emacs
On 2016-02-05, at 05:05, Emanuel Berg <embe8573@student.uu.se> wrote:
> Marcin Borkowski <mbork@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.
:-)
--
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University
^ permalink raw reply [flat|nested] 16+ messages in thread
[parent not found: <mailman.3642.1454623944.843.help-gnu-emacs@gnu.org>]
* Re: "cl-case" with strings
2016-02-04 1:13 "cl-case" with strings Emanuel Berg
2016-02-04 1:39 ` Dmitry Gutov
@ 2016-02-05 4:52 ` Emanuel Berg
1 sibling, 0 replies; 16+ messages in thread
From: Emanuel Berg @ 2016-02-05 4:52 UTC (permalink / raw)
To: help-gnu-emacs
Emanuel Berg <embe8573@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) )
--
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).