0

The following function add string associations if matches are found within a collection of alists. Currently I am listing the alists andromeda-assoc-tablet-N one by one. Can one manage to this without actually listing them individually because I have many of them (have got 57 alists)? Perhaps apply mapcar then pass the actual list to the loop.

Have been scrutinising why I cannot get the following implementation plan that uses mapcar to work.

(dolist (aggr (list (mapcar #'andromeda-construct-assoc-tablet
                            (number-sequence 1 57))))
  (setq rplc (cdr (assoc word aggr)))
  (unless (null rplc)
     (add-to-list 'nscrip
         (replace-regexp-in-string word rplc word))))

Here is the detailed description

(defvar nscrip '())

(defconst andromeda-assoc-tablet-1
  '( ("OrycteropusAfer" . "Aardvark")  
     ("VicugnaPacos" . "Alpaca") 
     ("MyrmecophagaTridactyla" . "Anteater") ))

(defconst andromeda-assoc-tablet-2
  '( ("Dasypodidae" . "Armadillo")  
     ("TaxideaTaxus" . "Badger")
     ("Beaver" . "Beaver") ))

(defconst andromeda-assoc-tablet-3
  '( ("LynxRufus" . "Bobcat")
     ("LepomisMacrochirus" . "Bluegill")
     ("RangiferTarandus" . "Caribou") ))

(defun andromeda-shorten-word ()
  "Shorten word at point according to specific rules."

  (interactive)

  (let* ( (bounds  (bounds-of-thing-at-point 'word))
      (word (downcase (buffer-substring (car bounds) (cdr bounds))))
      (rplc "") )

    (goto-char (car bounds))
    (add-to-list 'nscrip word)
    
    (dolist (aggr (list andromeda-assoc-tablet-1
                        andromeda-assoc-tablet-2
                        andromeda-assoc-tablet-3))
      (setq rplc (cdr (assoc word aggr)))
      (unless (null rplc)
         (add-to-list 'nscrip
             (replace-regexp-in-string word rplc word)))) ))

Thus if word is LynxRufus, then nscrip is Bobcat.

I have made the following function

(defun andromeda-construct-assoc-tablet (n)
  "Return value of symbol `andromeda-assoc-tablet-N', an alist of
paired string elements."

  (symbol-value (intern (format "andromeda-assoc-tablet-%d" n))))

My scheme is to use something like

(mapcar #'andromeda-construct-assoc-tablet (number-sequence 1 57)

that I can pass to aggr in function andromeda-shorten-word.

4
  • 1
    Could you please edit your question to show a minimal example of one of your input alists and also a minimal sample of what you need nscrip to look like afterwards. Aug 22, 2022 at 13:45
  • 1
    Having lots of almost-identical variable names that vary only by an index number is a "code smell"; you should probably revisit your design. If you know you will always have exactly 57 alists, no more no less, then you might consider an array of 57 alists instead. If you sometimes have more, sometimes less, then a list of alists is probably better. Aug 22, 2022 at 14:21
  • Currently 57 but could use more or less depending on task. Will put a minimal example.
    – Dilna
    Aug 22, 2022 at 22:42
  • The setup does not have a code smell problem, but necessitates from requirements of supportable code. Do not worry about the psychological smell.
    – Dilna
    Aug 22, 2022 at 23:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.