Refactor problem spec code

This commit is contained in:
Kiana Sheibani 2024-03-22 00:54:00 -04:00
parent ef7caec424
commit 52a62591b6
Signed by: toki
GPG key ID: 6CB106C25E86A9F7

View file

@ -3520,24 +3520,24 @@ This section is for code with little or no associated documentation. This could
"An alist of regexps matching problem specs.")
(setq ~/org-problem-spec-alist
`((,(rx (group (+ digit))
(* space) "-" (* space)
(group (+ digit))
(* space) "odd")
. ,(lambda (beg end)
(when (cl-evenp beg) (cl-incf beg))
(number-sequence beg end 2)))
(,(rx (group (+ digit))
(* space) "-" (* space)
(group (+ digit))
(* space) "even")
. ,(lambda (beg end)
(when (cl-oddp beg) (cl-incf beg))
(number-sequence beg end 2)))
(,(rx (group (+ digit))
(* space) "-" (* space)
(group (+ digit))) . number-sequence)
(,(rx (group (+ digit))) . list)))
`((,(rx (group (+ digit))
(* space) "-" (* space)
(group (+ digit))
(* space) "odd")
. ,(lambda (beg end)
(when (cl-evenp beg) (cl-incf beg))
(number-sequence beg end 2)))
(,(rx (group (+ digit))
(* space) "-" (* space)
(group (+ digit))
(* space) "even")
. ,(lambda (beg end)
(when (cl-oddp beg) (cl-incf beg))
(number-sequence beg end 2)))
(,(rx (group (+ digit))
(* space) "-" (* space)
(group (+ digit))) . number-sequence)
(,(rx (group (+ digit))) . list)))
(defun ~/org-generate-problem-list (&rest specs)
@ -3546,15 +3546,14 @@ This section is for code with little or no associated documentation. This could
(problems
(mapcan
(lambda (spec)
(let* ((match (car (or (--keep
(when-let ((x (cdr-safe
(s-match (car it) spec))))
(cons x (cdr it)))
alist)
(user-error "Invalid problem spec")))))
(let* ((match
(or (--some (-some-> (s-match (car it) spec)
cdr (cons (cdr it)))
alist)
(user-error "Invalid problem spec \"%s\"" spec))))
(apply (cdr match) (mapcar #'string-to-number (car match)))))
specs)))
(insert "\n")
(move-to-left-margin)
(dolist (num problems)
(insert (format "- [ ] %d\n" num)))))
#+end_src