complicated code to do trivial (?) thing

classic Classic list List threaded Threaded
3 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