all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* (n)reverse vs. length
@ 2014-05-14 14:19 Dmitry Antipov
  2014-05-14 19:10 ` Stefan Monnier
  0 siblings, 1 reply; 7+ messages in thread
From: Dmitry Antipov @ 2014-05-14 14:19 UTC (permalink / raw)
  To: Emacs development discussions

Should (n)reverse has the same semantics as length, i.e. accept
vectors and strings as well? IMHO this is the convenient and expected
behavior from the point of view of the elisp programmer.

Dmitry



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

* Re: (n)reverse vs. length
  2014-05-14 14:19 (n)reverse vs. length Dmitry Antipov
@ 2014-05-14 19:10 ` Stefan Monnier
  2014-05-14 20:12   ` David Kastrup
  0 siblings, 1 reply; 7+ messages in thread
From: Stefan Monnier @ 2014-05-14 19:10 UTC (permalink / raw)
  To: Dmitry Antipov; +Cc: Emacs development discussions

> Should (n)reverse has the same semantics as length, i.e. accept
> vectors and strings as well?

Good question.

> IMHO this is the convenient and expected behavior from the point of
> view of the elisp programmer.

Elisp is pretty lame and messy in this respect.
E.g. we have elt/aref/nth which operate on various subsets of sequences.
We have mapc/mapcar which apply to lists and arrays (and strings), but
mapcar returns a list in any case.

I think it would be OK to make `reverse' work on arrays and strings.
I definitely don't want `nreverse' working on strings (strings should
be immutable), but I guess making it work on arrays is fine.


        Stefan



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

* Re: (n)reverse vs. length
  2014-05-14 19:10 ` Stefan Monnier
@ 2014-05-14 20:12   ` David Kastrup
  2014-05-15  1:45     ` Stefan Monnier
  0 siblings, 1 reply; 7+ messages in thread
From: David Kastrup @ 2014-05-14 20:12 UTC (permalink / raw)
  To: emacs-devel

Stefan Monnier <monnier@IRO.UMontreal.CA> writes:

>> Should (n)reverse has the same semantics as length, i.e. accept
>> vectors and strings as well?
>
> Good question.
>
>> IMHO this is the convenient and expected behavior from the point of
>> view of the elisp programmer.
>
> Elisp is pretty lame and messy in this respect.
> E.g. we have elt/aref/nth which operate on various subsets of sequences.
> We have mapc/mapcar which apply to lists and arrays (and strings), but
> mapcar returns a list in any case.
>
> I think it would be OK to make `reverse' work on arrays and strings.
> I definitely don't want `nreverse' working on strings (strings should
> be immutable),

Doesn't aset work on strings?

-- 
David Kastrup




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

* Re: (n)reverse vs. length
  2014-05-14 20:12   ` David Kastrup
@ 2014-05-15  1:45     ` Stefan Monnier
  2014-05-15  4:29       ` Dmitry Antipov
  0 siblings, 1 reply; 7+ messages in thread
From: Stefan Monnier @ 2014-05-15  1:45 UTC (permalink / raw)
  To: David Kastrup; +Cc: emacs-devel

>> strings should be immutable
> Doesn't aset work on strings?

Yes, I said "should be" not "are".


        Stefan



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

* Re: (n)reverse vs. length
  2014-05-15  1:45     ` Stefan Monnier
@ 2014-05-15  4:29       ` Dmitry Antipov
  2014-05-15  5:49         ` David Kastrup
  2014-05-15 12:38         ` Stefan Monnier
  0 siblings, 2 replies; 7+ messages in thread
From: Dmitry Antipov @ 2014-05-15  4:29 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: David Kastrup, emacs-devel

On 05/15/2014 05:45 AM, Stefan Monnier wrote:

>>> strings should be immutable
>> Doesn't aset work on strings?
>
> Yes, I said "should be" not "are".

Hm, what about an existing code which relies on aset'table strings? Questions
like http://lists.gnu.org/archive/html/emacs-devel/2014-04/msg00378.html shows
that treating string as an array of characters is a common practice (which is
"natural" for the people with C background). And, in case of large strings,
in-place reverse may save a lot of memory.

Dmitry




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

* Re: (n)reverse vs. length
  2014-05-15  4:29       ` Dmitry Antipov
@ 2014-05-15  5:49         ` David Kastrup
  2014-05-15 12:38         ` Stefan Monnier
  1 sibling, 0 replies; 7+ messages in thread
From: David Kastrup @ 2014-05-15  5:49 UTC (permalink / raw)
  To: Dmitry Antipov; +Cc: Stefan Monnier, emacs-devel

Dmitry Antipov <dmantipov@yandex.ru> writes:

> On 05/15/2014 05:45 AM, Stefan Monnier wrote:
>
>>>> strings should be immutable
>>> Doesn't aset work on strings?
>>
>> Yes, I said "should be" not "are".
>
> Hm, what about an existing code which relies on aset'table strings? Questions
> like http://lists.gnu.org/archive/html/emacs-devel/2014-04/msg00378.html shows
> that treating string as an array of characters is a common practice (which is
> "natural" for the people with C background). And, in case of large strings,
> in-place reverse may save a lot of memory.

I am not sure there is a use case for in-place reverse of strings.  If
there is, the algorithm for multibyte characters would definitely
warrant writing in C as it is quite non-trivial.  Two characters of
buffer should suffice: just copy one character from each end to its
respective buffer, and then repeat copying and refilling the respective
one of the two buffers which has enough space to fit in the original
string.  Doing it in C would be O(n), doing it with aset instead would
be O(n^2).

-- 
David Kastrup



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

* Re: (n)reverse vs. length
  2014-05-15  4:29       ` Dmitry Antipov
  2014-05-15  5:49         ` David Kastrup
@ 2014-05-15 12:38         ` Stefan Monnier
  1 sibling, 0 replies; 7+ messages in thread
From: Stefan Monnier @ 2014-05-15 12:38 UTC (permalink / raw)
  To: Dmitry Antipov; +Cc: David Kastrup, emacs-devel

> Hm, what about an existing code which relies on aset'table strings?

I recommend against it.  And I've been known to install changes in trunk
to make such code use arrays instead (or avoid `aset', depending on the
situation).

> Questions like
> http://lists.gnu.org/archive/html/emacs-devel/2014-04/msg00378.html
> shows that treating string as an array of characters is a common
> practice (which is "natural" for the people with C background).

I know.  And we should discourage that practice.

> And, in case of large strings, in-place reverse may save a lot
> of memory.

I can't think of any case where it can save a significant amount
of memory.


        Stefan



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

end of thread, other threads:[~2014-05-15 12:38 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-05-14 14:19 (n)reverse vs. length Dmitry Antipov
2014-05-14 19:10 ` Stefan Monnier
2014-05-14 20:12   ` David Kastrup
2014-05-15  1:45     ` Stefan Monnier
2014-05-15  4:29       ` Dmitry Antipov
2014-05-15  5:49         ` David Kastrup
2014-05-15 12:38         ` Stefan Monnier

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.