complicated code to do trivial (?) thing

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

complicated code to do trivial (?) thing

Emacs - Help mailing list
Why isn't this easy to do?

Or is it?

;;; -*- lexical-binding: t -*-

;; This file: http://user.it.uu.se/~embe8573/emacs-init/abc.el
;;            https://dataswamp.org/~incal/emacs-init/abc.el

(require 'cl-lib)

(defun alphabet (&optional as-list)
  (let ((abc "a b c d e f g h i j k l m n o p q r s t u v w x y z"))
    (if as-list
        (cl-remove ?\  (string-to-list abc))
      abc
      )))
;; (alphabet t) ; (97 98 99 100 101 102 103 104 105 106 107 ...)
;; (alphabet)   ; "a b c d e f g h i j k ..."

(defun echo-alphabet (&optional number)
  (interactive "P")
  (let*((num        (or number (length (alphabet t))))
        (part       (cl-subseq (alphabet t) 0 num))
        (str-list   (cl-mapcar (lambda (c) (char-to-string c)) part))
        (str-almost (format "%s" str-list))
        (str        (substring str-almost 1 (1- (length str-almost))))
        )
    (message str) ))
;; (echo-alphabet)     ; "a b c ... x y z"
;; (echo-alphabet  10) ; "a b c d e f g h i j"
;; (echo-alphabet -10) ; "a b c d e f g h i j k l m n o p"
;; (call-interactively #'echo-alphabet)
(defalias 'abc #'echo-alphabet)

(provide 'abc)

--
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal


Reply | Threaded
Open this post in threaded view
|

Re: complicated code to do trivial (?) thing

Anders Dalskov
It can be simplified somewhat, I think. My take is below. Instead of
`cl-remove' you can use `split-string', and instead of `mapcar' +
`format' + `substring' you can use `mapconcat'.


(defun alphabet (&optional as-list)
  (let ((abc "a b c d e f g h i j k l m n o p q r s t u v w x y z"))
    (if as-list
        (split-string abc)
      abc)))

(defun echo-alphabet (&optional number)
  (interactive "P")
  (let* ((abc-as-list (alphabet t))
         (number (or number (length abc-as-list))))
    (mapconcat 'identity (subseq abc-as-list 0 number) " ")))


Also, if you don't need the alphabet function (or you only want to use
the english alphabet), then you should probably just assign
`abc-as-list' directly and remove the `alphabet' function.

Emanuel Berg via help-gnu-emacs writes:

> Why isn't this easy to do?
>
> Or is it?
>
> ;;; -*- lexical-binding: t -*-
>
> ;; This file: http://user.it.uu.se/~embe8573/emacs-init/abc.el
> ;;            https://dataswamp.org/~incal/emacs-init/abc.el
>
> (require 'cl-lib)
>
> (defun alphabet (&optional as-list)
>   (let ((abc "a b c d e f g h i j k l m n o p q r s t u v w x y z"))
>     (if as-list
>         (cl-remove ?\  (string-to-list abc))
>       abc
>       )))
> ;; (alphabet t) ; (97 98 99 100 101 102 103 104 105 106 107 ...)
> ;; (alphabet)   ; "a b c d e f g h i j k ..."
>
> (defun echo-alphabet (&optional number)
>   (interactive "P")
>   (let*((num        (or number (length (alphabet t))))
>         (part       (cl-subseq (alphabet t) 0 num))
>         (str-list   (cl-mapcar (lambda (c) (char-to-string c)) part))
>         (str-almost (format "%s" str-list))
>         (str        (substring str-almost 1 (1- (length str-almost))))
>         )
>     (message str) ))
> ;; (echo-alphabet)     ; "a b c ... x y z"
> ;; (echo-alphabet  10) ; "a b c d e f g h i j"
> ;; (echo-alphabet -10) ; "a b c d e f g h i j k l m n o p"
> ;; (call-interactively #'echo-alphabet)
> (defalias 'abc #'echo-alphabet)
>
> (provide 'abc)


--
- Anders

Reply | Threaded
Open this post in threaded view
|

Re: complicated code to do trivial (?) thing

Emacs - Help mailing list
Anders Dalskov wrote:

> It can be simplified somewhat, I think.
> My take is below. Instead of `cl-remove' you
> can use `split-string', and instead of
> `mapcar' + `format' + `substring' you can use
> `mapconcat'.
>
> (defun alphabet (&optional as-list)
>   (let ((abc "a b c d e f g h i j k l m n o p q r s t u v w x y z"))
>     (if as-list
> (split-string abc)
>       abc)))
>
> (defun echo-alphabet (&optional number)
>   (interactive "P")
>   (let* ((abc-as-list (alphabet t))
> (number (or number (length abc-as-list))))
>     (mapconcat 'identity (subseq abc-as-list 0 number) " ")))

That looks much better, yes. Thank you, will
try it out, examine the code, and if it works
as well as mine ;) I will replace it, and put
your initials next it it.

Mange tak!

--
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal


Reply | Threaded
Open this post in threaded view
|

Re: complicated code to do trivial (?) thing

Emacs - Help mailing list
In reply to this post by Anders Dalskov
I wierd problem with messages has appeared
in the system.

It is described - or shown - in the source
file [1], ~downmost, or lines 28-34.

But in short, sometimes the echoed message is
the intended

    a b c d e f g h i j k l m n o p q r s t u v w x y z

or

    a b c d e f g h i j k l m n o p

but sometimes both examples (see the file)
produces abbreviated messages, actually the
same one:

    a b c d e f g h i j k l ...

Are long messages matched against messages
already showed or showed recently and
abbreviated as they are considered redundant as
"have already been showed"?

What do you know? What do you think?
Excepts I should learn the English
alphabet already?


[1] https://dataswamp.org/~incal/emacs-init/abc.el

--
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal


Reply | Threaded
Open this post in threaded view
|

Re: complicated code to do trivial (?) thing

Anders Dalskov
Emacs sometimes inserts an ellipsis if the result is longer than some
threshold (what that threshold is, I don't know).

Though this only happens with s-expressions, not strings, afaict.

Emanuel Berg via help-gnu-emacs writes:

> I wierd problem with messages has appeared
> in the system.
>
> It is described - or shown - in the source
> file [1], ~downmost, or lines 28-34.
>
> But in short, sometimes the echoed message is
> the intended
>
>     a b c d e f g h i j k l m n o p q r s t u v w x y z
>
> or
>
>     a b c d e f g h i j k l m n o p
>
> but sometimes both examples (see the file)
> produces abbreviated messages, actually the
> same one:
>
>     a b c d e f g h i j k l ...
>
> Are long messages matched against messages
> already showed or showed recently and
> abbreviated as they are considered redundant as
> "have already been showed"?
>
> What do you know? What do you think?
> Excepts I should learn the English
> alphabet already?
>
>
> [1] https://dataswamp.org/~incal/emacs-init/abc.el


--
- Anders

Reply | Threaded
Open this post in threaded view
|

Re: complicated code to do trivial (?) thing

Emacs - Help mailing list
Anders Dalskov wrote:

> Emacs sometimes inserts an ellipsis if the
> result is longer than some threshold (what
> that threshold is, I don't know).
>
> Though this only happens with s-expressions,
> not strings, afaict.

Okay, but the strangest thing is sometimes
I get the entire string, sometimes the
"insertion of ellipsis", for the same command
and result! Same result, I assume! Otherwise it
is even more strange...

--
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal


Reply | Threaded
Open this post in threaded view
|

Re: complicated code to do trivial (?) thing

Tomas Zerolo
On Wed, Jul 10, 2019 at 10:59:38AM +0200, Emanuel Berg via help-gnu-emacs wrote:

> Anders Dalskov wrote:
>
> > Emacs sometimes inserts an ellipsis if the
> > result is longer than some threshold (what
> > that threshold is, I don't know).
> >
> > Though this only happens with s-expressions,
> > not strings, afaict.
>
> Okay, but the strangest thing is sometimes
> I get the entire string, sometimes the
> "insertion of ellipsis", for the same command
> and result! Same result, I assume! Otherwise it
> is even more strange...
You're most probably seeing this abbreviation in the message
line (which ends up in *Messages* or something).

There are two levels here:

The "print" function, which is the generic S-expression printer,
obeys two variables: "print-length" and "print-level", which can
limit how wide and deep to print S-expressions. By default they
are both nil, which means "print everything.

For the message area, "eval-expression" calls on "print" to
present the results, but "eval-expression-print-length" and
"eval-expression-print-depth" take over, and by default, both
are set to finite values.

Cheers
-- t

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: complicated code to do trivial (?) thing

Emacs - Help mailing list
In reply to this post by Emacs - Help mailing list
> From: help-gnu-emacs On Behalf Of Anders Dalskov
> Sent: 10 July, 2019 4:12 AM

> Emacs sometimes inserts an ellipsis if the result is longer than some
> threshold (what that threshold is, I don't know).

> Though this only happens with s-expressions, not strings, afaict.

Are you referring to the last sentence of:

Lisp Interaction mode defined in 'elisp-mode.el':
Major mode for typing and evaluating Lisp forms.
Like Lisp mode except that C-j evals the Lisp expression
before point, and prints its value into the buffer, advancing point.
Note that printing is controlled by 'eval-expression-print-length'
and 'eval-expression-print-level'.

(from C-h m in the *scratch* buffer)?

>> but sometimes both examples (see the file)
>> produces abbreviated messages, actually the
>> same one:
>>
>>     a b c d e f g h i j k l ...
>>
>> [1] https://dataswamp.org/~incal/emacs-init/abc.el

I get "ERR_CONNECTION_RESET" when I click on that file URL.

The content of this message is APPLIED MATERIALS CONFIDENTIAL. If you are not the intended recipient, please notify me, delete this email and do not use or distribute this email.

Reply | Threaded
Open this post in threaded view
|

RE: complicated code to do trivial (?) thing

Emacs - Help mailing list
From: help-gnu-emacs  On Behalf Of Joe Trivers via help-gnu-emacs
Sent: 10 July, 2019 1:34 PM

>>> but sometimes both examples (see the file)
>>> produces abbreviated messages, actually the
>>> same one:
>>>
>>>     a b c d e f g h i j k l ...
>>>
>>> [1] https://dataswamp.org/~incal/emacs-init/abc.el
>
>I get "ERR_CONNECTION_RESET" when I click on that file URL.

Sorry, that was my employer's network nanny. From my home ISP it worked just fine.

(echo-alphabet)
"a b c d e f g h i j k l m n o p q r s t u v w x y z"

(let ((print-length 8))
  (echo-alphabet))
"a b c d e f g h ..."

(let ((print-length 12))
  (echo-alphabet))
"a b c d e f g h i j k l ..."

(let ((eval-expression-print-length 12))
  (echo-alphabet))
"a b c d e f g h i j k l m n o p q r s t u v w x y z"

I haven't experienced any indeterminacy, though.
/Joe
The content of this message is APPLIED MATERIALS CONFIDENTIAL. If you are not the intended recipient, please notify me, delete this email and do not use or distribute this email.