From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Mark H Weaver Newsgroups: gmane.lisp.guile.user Subject: Re: optimizing lazy sequences: srfi-41 vs delayed continuation with values + promise vs delayed continuation with cons + lambda Date: Fri, 02 Mar 2018 18:22:00 -0500 Message-ID: <87fu5i5ahz.fsf@netris.org> References: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1520032873 24323 195.159.176.226 (2 Mar 2018 23:21:13 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 2 Mar 2018 23:21:13 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux) Cc: Guile User To: Amirouche Boubekki Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Sat Mar 03 00:21:09 2018 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ertzA-0005gQ-N0 for guile-user@m.gmane.org; Sat, 03 Mar 2018 00:21:08 +0100 Original-Received: from localhost ([::1]:37880 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eru1A-0001Pf-8U for guile-user@m.gmane.org; Fri, 02 Mar 2018 18:23:12 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:49816) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eru0j-0001PL-Ow for guile-user@gnu.org; Fri, 02 Mar 2018 18:22:47 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eru0g-0002zf-Jq for guile-user@gnu.org; Fri, 02 Mar 2018 18:22:45 -0500 Original-Received: from world.peace.net ([50.252.239.5]:35068) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eru0g-0002zT-Ea for guile-user@gnu.org; Fri, 02 Mar 2018 18:22:42 -0500 Original-Received: from turntable.mit.edu ([18.18.160.11] helo=jojen) by world.peace.net with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1eru0f-0003ul-50; Fri, 02 Mar 2018 18:22:41 -0500 In-Reply-To: (Amirouche Boubekki's message of "Sun, 25 Feb 2018 15:16:48 +0100") X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 50.252.239.5 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.org gmane.lisp.guile.user:14473 Archived-At: Hi, Amirouche Boubekki writes: [...] > Now, I got another idea what if I replace the 'cons' returned > by the traversi procedures by 'values' and replace the > lambda with 'delay' with some use of 'force' at the correct > places. It will result in the following procedures: > > (define-public (list->stream lst) > (let loop ((lst lst)) > (if (null? lst) > (delay (values #f #f)) > (delay (values (car lst) (loop (cdr lst))))))) > > (define-public (stream->list stream) > (let loop ((stream stream) > (out '())) > (call-with-values (lambda () (force stream)) > (lambda (value next) > (if next > (loop next (cons value out)) > (reverse! out)))))) > > (define-public (stream-map proc stream) > (let loop ((stream stream)) > (call-with-values (lambda () (force stream)) > (lambda (value next) > (if next > (delay (values (proc value) (loop next))) > (delay (values #f #f))))))) This code assumes that promises can store multiple values. Although Guile's legacy core promises *accidentally* support multiple values today, there's no guarantee that they will continue to do so in the future. None of the standards allow this, and Guile's manual states in the documentation for 'delay' that "The effect of returning multiple values is unspecified." Supporting multiple values in promises makes them more complex, and inevitably less efficient. SRFI-45 promises are simpler than Guile's legacy core promises in two ways: (1) they do not include built-in thread synchronization, and (2) they do not support multiple values. I would recommend using SRFI-45 promises. Although they are implemented in Scheme, last I checked I found that they were about as fast as Guile's legacy core promises implemented in C, presumably because of the built-in thread synchronization in our core promises. In any case, I would strongly recommend against writing code that assumes that Guile's promises can hold multiple values. Regards, Mark