help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: complicated code to do trivial (?) thing


From: Anders Dalskov
Subject: Re: complicated code to do trivial (?) thing
Date: Wed, 12 Jun 2019 09:16:08 +0200
User-agent: mu4e 1.0; emacs 25.2.2

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 via email to

[Prev in Thread] Current Thread [Next in Thread]