From mboxrd@z Thu Jan 1 00:00:00 1970 From: Bastien Subject: Re: [PATCH] Lookup functions, take two Date: Wed, 26 Sep 2012 16:26:32 +0200 Message-ID: <87k3vg273b.fsf@bzg.ath.cx> References: <87y5k0g3qc.fsf@syk.fi> <878vbxao4r.fsf@Rainer.invalid> <873925g1v4.fsf@bzg.ath.cx> <87txukyk1n.fsf@syk.fi> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([208.118.235.92]:56446) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TGsZP-0005VD-NP for emacs-orgmode@gnu.org; Wed, 26 Sep 2012 10:26:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TGsZL-0007hh-5h for emacs-orgmode@gnu.org; Wed, 26 Sep 2012 10:26:35 -0400 Received: from mail-wi0-f177.google.com ([209.85.212.177]:42180) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TGsZK-0007hY-S4 for emacs-orgmode@gnu.org; Wed, 26 Sep 2012 10:26:31 -0400 Received: by wibhn17 with SMTP id hn17so663627wib.12 for ; Wed, 26 Sep 2012 07:26:30 -0700 (PDT) In-Reply-To: <87txukyk1n.fsf@syk.fi> (Jarmo Hurri's message of "Wed, 26 Sep 2012 16:45:40 +0300") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Jarmo Hurri Cc: emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain Hi Jarmo, Jarmo Hurri writes: > I have nothing against the idea. However, I get only errors when I try > to apply your functions in my examples. I can track down the source of > the problems later (not today, though). There was a typo, I attach the correct patch. > But how about combining your idea about getting rid of CL's position > with the following idea of building the docstring on the fly in the > macro? That is, you do not need to define multiple functions by hand, > but the docstrings can still be unique. As you prefer. But C-h f will not point to org-table.el if we use this macro. This is acceptable, but we need to mention `org-lookup' as the "matrix" function in the docstring, so that user C-h f'ing org-lookup will find it in org-table.el. Another wish style-wise: the first sentence of the docstring should be one line long. See the short parameters names and the docstrings in my patch to get an idea -- but please feel free to also follow your taste here, of course. Thanks! --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=org-table.el.patch diff --git a/lisp/org-table.el b/lisp/org-table.el index 37889af..95b8231 100644 --- a/lisp/org-table.el +++ b/lisp/org-table.el @@ -4826,6 +4826,29 @@ list of the fields in the rectangle ." (org-table-get-range (match-string 0 form) tbeg 1)) form))))))))) +(defun org-lookup (val s-list r-list lastp &optional predicate) + "Look for VAL in S-LIST and return the corresponding element in R-LIST. +If LASTP, ignore all matching VAL in S-LIST except the last one. +If PREDICATE is not nil, use this instead of `equal' to match VAL." + (let ((p (or predicate 'equal)) (c 0) r) + (nth (dolist (i s-list r) (setq c (1+ c)) + (if (and (funcall p val i) (or lastp (not r))) + (setq r (1- c)))) + return-list))) + +(defun org-lookup-first (val s-list r-list &optional predicate) + "Look for VAL in S-LIST and return the corresponding element in R-LIST. +If PREDICATE is not nil, use this instead of `equal' to match VAL." + (org-lookup val s-list r-list nil predicate)) + +(defun org-lookup-last (val s-list r-list &optional predicate) + "Look for VAL in S-LIST and return the corresponding element in R-LIST. +If PREDICATE is not nil, use this instead of `equal' to match VAL." + (org-lookup val s-list r-list t predicate)) + +;; (org-lookup-first 2 '(1 2 3 2) '(A B C D E)) => B +;; (org-lookup-last 2 '(1 2 3 2) '(A B C D E)) => D + (provide 'org-table) ;;; org-table.el ends here --=-=-= Content-Type: text/plain -- Bastien --=-=-=--