emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Bastien <bzg@altern.org>
To: Jarmo Hurri <jarmo.hurri@syk.fi>
Cc: emacs-orgmode@gnu.org
Subject: Re: [PATCH] Lookup functions, take two
Date: Wed, 26 Sep 2012 16:26:32 +0200	[thread overview]
Message-ID: <87k3vg273b.fsf@bzg.ath.cx> (raw)
In-Reply-To: <87txukyk1n.fsf@syk.fi> (Jarmo Hurri's message of "Wed, 26 Sep 2012 16:45:40 +0300")

[-- Attachment #1: Type: text/plain, Size: 1030 bytes --]

Hi Jarmo,

Jarmo Hurri <jarmo.hurri@syk.fi> 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!


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: org-table.el.patch --]
[-- Type: text/x-patch, Size: 1415 bytes --]

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

[-- Attachment #3: Type: text/plain, Size: 14 bytes --]


-- 
 Bastien

  reply	other threads:[~2012-09-26 14:26 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-09-23 15:26 [PATCH] Lookup functions, take two Jarmo Hurri
2012-09-24 14:01 ` Bastien
2012-09-24 17:33   ` Jarmo Hurri
2012-09-25  1:49     ` Bastien
2012-09-25  4:13       ` Jarmo Hurri
2012-09-25 10:04         ` Bastien
2012-09-25  9:15       ` Dominik, Carsten
2012-09-25 10:02         ` Bastien
2012-09-26 13:00           ` Jarmo Hurri
2012-09-26 13:14             ` Bastien
2012-09-25 19:39 ` Achim Gratz
2012-09-25 22:43   ` Bastien
2012-09-26 13:45     ` Jarmo Hurri
2012-09-26 14:26       ` Bastien [this message]
2012-09-26 17:47         ` Jarmo Hurri

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87k3vg273b.fsf@bzg.ath.cx \
    --to=bzg@altern.org \
    --cc=emacs-orgmode@gnu.org \
    --cc=jarmo.hurri@syk.fi \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).