My 9-year-old son loves playing school. He’s got a whiteboard, Mom, Sister and Dad are his pupils and he teaches them various real or made-up things. At the beginning he checks attendance, using a long list of made-up pupils’ names. Some time ago he decided that he’d like to keep the attendance data in a digital form. Mom helped him make a MS Word document (I know, I know…), where he puts little minuses for absentees and pluses for the attendees.
Some time later he approached me asking if I could make the computer sum up the pluses and minuses. Well sure I could! For obvious reasons I didn’t want to do that in Word. Since I may have accidentally
introduced him to a certain superior tool for text editing a few months ago, I suggested he used it, and decided to code this in Org mode. I imagined a table like this:
| Name | Attendance | |------------+------------| | Bill Musk | -+- | | Jeff Gates | +-+ | | Elon Bezos | -++ | |------------+------------| | Total | ??? |
and set out to find a way to count the pluses in the last position of these strings using the Org mode spreadsheet feature.
It turns out that it’s even much simpler than I thought. Org has a special syntax for Elisp formulae, and it correctly interpolates cell ranges as multiple arguments. The only thing that remained was to write a function accepting an arbitrary number of string arguments and counting all its arguments as well as the ones ending with the plus sign:
(defun get-recent-attendance (&rest attendance-strings)
"Count all ATTENDANCE-STRINGS and how many end with a `+'."
(let ((all (length attendance-strings))
(pluses (length (seq-filter (lambda (string)
(string-match "\\+$" string))
attendance-strings))))
(format "%s/%s" pluses all)))
Now the only thing I needed to do was to add this:
#+TBLFM: @5$2='(get-recent-attendance @I..@II)
As usual, Emacs and Org mode turned out to be excellent at building little pieces of code to solve little problems. Not that I’m surprised
!
CategoryEnglish, CategoryBlog, CategoryEmacs, CategoryOrgMode