unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: E Sabof <esabof@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Help with lexical binding
Date: Sat, 10 Aug 2013 18:32:54 +0100	[thread overview]
Message-ID: <CAEp6DyY9Ja7dio5NZ6fVacXQaiNd9kMwR0RCw2TNVBJgbqcaqQ@mail.gmail.com> (raw)

I have the snippet below. I can save it to a file, and run `eval-buffer' on
it. Or I can use `load'. In both cases it wlll work. However if I paste it
into scratch, and do M-: (setq lexical-binding t) and then M-x eval-buffer,
closures aren't created, and it fails. Is it a bug, or am I missing
something?

;; -*- lexical-binding: t -*-
(require 'cl-lib)
(defun es-scrape-web-listing
    (start-url collect-function next-url-function done-function
     &optional pages-limit silent)
  "Retrieve a list of things from a multi-page web document.

START-URL is the location from which to start scraping.

COLLECT-FUNCTION should return a list of collected things.

NEXT-URL-FUNCTION should be a function that will return the
link to the next page, or nil, when on the last page.

DONE-FUNCTION will be called once processing is finished, with
one argument - the results list.

One can limit the number of retrieved pages, by setting PAGES-LIMIT to a
number.

When SILENT is non-nil, no progress messages will be displayed.

The function returns a function that will stop the scraping process."
  (cl-assert (and start-url collect-function next-url-function
done-function))
  (let (retrieve-recursively
        collected
        next-url
        ( visited-urls (list start-url)))
    (setq retrieve-recursively
          (lambda (&rest args)
            (goto-char (point-min))
            (setq collected
                  (nconc collected
                         (funcall collect-function)))
            (goto-char (point-min))
            (unless silent
              (message (format "Scraped \"%s\". Collected so far: %s"
                               next-url
                               (length collected))))
            (cond ( (and pages-limit (<= (cl-decf pages-limit) 0))
                    (funcall done-function collected))
                  ( (and (setq next-url (funcall next-url-function))
                         (not (member next-url visited-urls)))
                    (push next-url visited-urls)
                    (url-retrieve next-url retrieve-recursively))
                  ( t (funcall done-function collected)))
            (kill-buffer)))
    (url-retrieve start-url
                  retrieve-recursively))
  (lambda ()
    (setq next-url-function (lambda ()))))

(defvar ncz-posts nil)
(defvar ncz-scraper-stop nil)
(setq ncz-scraper-stop
      (es-scrape-web-listing
       "http://www.nczonline.net/"
       (lambda ()
         (cl-loop with link-holder
                  with name-holder
                  while (and (search-forward "class=\"post-snippet" nil t)
                             (re-search-forward "href=\"\\(?1:.+?\\)\"" nil
t)
                             (setq link-holder (match-string 1))
                             (re-search-forward ">\\(?1:.+?\\)<" nil t)
                             (setq name-holder (match-string 1)))
                  collecting (cons link-holder name-holder)
                  ))
       (lambda ()
         (ignore-errors
           (search-forward "<div class=\"navigation\">")
           (re-search-forward "<a href=\"\\(?1:.+?\\)\" >")
           (match-string 1)))
       (lambda (result)
         (setq scrape-result result)
         (message (concat "Done. "
                          (number-to-string
                           (length scrape-result))
                          " items found.")))
       1))


             reply	other threads:[~2013-08-10 17:32 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-10 17:32 E Sabof [this message]
     [not found] <mailman.2986.1376155982.12400.help-gnu-emacs@gnu.org>
2013-08-10 18:06 ` Help with lexical binding esabof
2013-08-10 18:34   ` esabof
2013-08-11  2:56     ` Stefan Monnier
2013-08-11 13:18       ` esabof
2013-08-11 17:46         ` Stefan Monnier

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.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=CAEp6DyY9Ja7dio5NZ6fVacXQaiNd9kMwR0RCw2TNVBJgbqcaqQ@mail.gmail.com \
    --to=esabof@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /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.
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).