From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Eric Schulte" Newsgroups: gmane.emacs.devel Subject: Re: general lazy list facility for Emacs Lisp? Date: Wed, 23 Mar 2011 23:27:48 -0600 Message-ID: <87hbathyuj.fsf@gmail.com> References: <87pqphswn3.fsf@lifelogs.com> <877hbpzsy8.fsf@member.fsf.org> <87oc51rcal.fsf@lifelogs.com> <87tyetfymt.fsf@member.fsf.org> <87pqphfyak.fsf@member.fsf.org> <87oc51ipy5.fsf@lifelogs.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: dough.gmane.org 1300944492 18914 80.91.229.12 (24 Mar 2011 05:28:12 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Thu, 24 Mar 2011 05:28:12 +0000 (UTC) Cc: emacs-devel@gnu.org To: Ted Zlatanov Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Mar 24 06:28:08 2011 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Q2d5b-0004NR-Ft for ged-emacs-devel@m.gmane.org; Thu, 24 Mar 2011 06:28:07 +0100 Original-Received: from localhost ([127.0.0.1]:47335 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Q2d5a-0004S2-RA for ged-emacs-devel@m.gmane.org; Thu, 24 Mar 2011 01:28:06 -0400 Original-Received: from [140.186.70.92] (port=56584 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Q2d5P-0004Qj-Kj for emacs-devel@gnu.org; Thu, 24 Mar 2011 01:27:56 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Q2d5O-0002TS-CR for emacs-devel@gnu.org; Thu, 24 Mar 2011 01:27:55 -0400 Original-Received: from mail-iy0-f169.google.com ([209.85.210.169]:59621) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Q2d5O-0002TL-8E for emacs-devel@gnu.org; Thu, 24 Mar 2011 01:27:54 -0400 Original-Received: by iyf13 with SMTP id 13so12109632iyf.0 for ; Wed, 23 Mar 2011 22:27:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version:content-type; bh=iUhOzi4YBJbnGKRCdpNKG7D5tsJ6wikwgazs9EMMoSw=; b=cWLjEdzAfLosf5YwTlprjsKf6QRvtWKsoW8cyHHCGGrZm8SS3sKk7vFVtzcaW+y4mq SLB+wZC+7wsXRHL/+MDUCYpztOkkg1dulDwz3L/4TnYyO1T8RxOdDH0kcclXzk+dq2YO oO9oafiv4WMfk2R9oIpZvC0bsvTN9gaeQelCQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-type; b=Ajnt+KnaGy82kH3KUbk/mEjVF8GtW4aS9HJNu0Bb+VwUBQ3xXlTS8R71S9C1AlEMrg M+7uOA5VVIJGozgOqmyRuFU2FYpb+H5a3XiGY1WUt9d2Tu8P/orq36ujyOT1VM3Yev6Y irp3jZMwKJimuZKfdRvaEvXDeW/hSzRbQaPgk= Original-Received: by 10.231.113.80 with SMTP id z16mr7461405ibp.180.1300944473679; Wed, 23 Mar 2011 22:27:53 -0700 (PDT) Original-Received: from roll (c-76-18-83-230.hsd1.nm.comcast.net [76.18.83.230]) by mx.google.com with ESMTPS id he40sm5562220ibb.33.2011.03.23.22.27.50 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 23 Mar 2011 22:27:51 -0700 (PDT) In-Reply-To: <87oc51ipy5.fsf@lifelogs.com> (Ted Zlatanov's message of "Wed, 23 Mar 2011 14:42:26 -0500") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-Received-From: 209.85.210.169 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:137626 Archived-At: --=-=-= Content-Type: text/plain Ted Zlatanov writes: > On Wed, 23 Mar 2011 20:10:27 +0100 Tassilo Horn wrote: > > TH> Just a short remark on what I've had in mind with "real" memoization: If > TH> elisp had closures (not clojures ;-)), then one could write memoize as > TH> function > > I think `lexical-let' (according to the docstring at least) creates true > CL-style closures in ELisp. > Just for completeness, a lazy.el file is attached implementing lazy sequences in Emacs Lisp. This is basically an exact copy of lazy.lisp with `lexical-let' used where closures are needed. Best -- Eric --=-=-= Content-Type: application/emacs-lisp Content-Disposition: inline; filename=lazy.el Content-Transfer-Encoding: quoted-printable ;;; lazy.el --- lazy sequences for Emacs lisp ;; taken from http://landoflisp.com/lazy.lisp ;;; Code: (require 'cl) (defmacro lazy (&rest body) (let ((forced (gensym)) (value (gensym))) `(lexical-let ((,forced nil) (,value nil)) (lambda () (unless ,forced (setq ,value (progn ,@body)) (setq ,forced t)) ,value)))) (defun force (lazy-value) (funcall lazy-value)) (defmacro lazy-cons (a d) `(lazy (cons ,a ,d))) (defun lazy-car (x) (car (force x))) (defun lazy-cdr (x) (cdr (force x))) (defun next-int (n) ;; (message "calculating %d" n) ; for proving laziness (lexical-let ((n n)) (lazy-cons n (next-int (1+ n))))) (setq *integers* (next-int 1)) (defun lazy-nil () (lazy nil)) (defun lazy-null (x) (not (force x))) (defun make-lazy (lst) (lazy (when lst (cons (car lst) (make-lazy (cdr lst)))))) (defun take (n lst) (unless (or (zerop n) (lazy-null lst)) (cons (lazy-car lst) (take (1- n) (lazy-cdr lst))))) (defun take-all (lst) (unless (lazy-null lst) (cons (lazy-car lst) (take-all (lazy-cdr lst))))) (defun lazy-mapcar (fun lst) (lazy (unless (lazy-null lst) (cons (funcall fun (lazy-car lst))=20 (lazy-mapcar fun (lazy-cdr lst)))))) (defun lazy-mapcan (fun lst) (labels ((f (lst-cur) (if (lazy-null lst-cur) (force (lazy-mapcan fun (lazy-cdr lst))) (cons (lazy-car lst-cur) (lazy (f (lazy-cdr lst-cur))))))) (lazy (unless (lazy-null lst) (f (funcall fun (lazy-car lst))))))) (defun lazy-find-if (fun lst) (unless (lazy-null lst) (let ((x (lazy-car lst))) (if (funcall fun x) x (lazy-find-if fun (lazy-cdr lst)))))) (defun lazy-nth (n lst)=20 (if (zerop n) (lazy-car lst) (lazy-nth (1- n) (lazy-cdr lst)))) ;;; lazy.el ends here --=-=-=--