unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: "Jose A. Ortega Ruiz" <jao@gnu.org>
Subject: Re: srfi-39 implementation
Date: Fri, 14 May 2004 01:23:52 +0200	[thread overview]
Message-ID: <871xlnq5br.fsf@holmes.localdomain> (raw)
In-Reply-To: <87oeos119q.fsf@zip.com.au> (Kevin Ryde's message of "Fri, 14 May 2004 07:10:41 +1000")

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


Kevin Ryde <user42@zip.com.au> writes:

> "Jose A. Ortega Ruiz" <jao@gnu.org> writes:
>>
>> Although you are right in that primitive-make-property & co. can be
>> used, i think the case-based thing is slightly lighter and maybe even
>> faster, since no hashtable creation/lookup is involved. Or, at any
>> rate, the difference should be minimal,
>
> Ok.  Another possibility I guess (for both styles) would be a single
> call or lookup giving back both fluid and converter.  Doesn't quite
> suit the way with-parameters* does its `map's though.
>

Yes. I cannot think at the moment of a simple way of using a single
call---all that comes to mind involves call-with-values, fold or the
like, and makes the code more complex.

>> (define-module (srfi srfi-39)
>>   #:export (make-parameter)
>>   #:export-syntax (parameterize)
>>
>>   ;; procedure not in srfi-39.
>>   #:export (with-parameters*))
>
> One #:export (with whatever comment) is enough I expect.
>

OK.

>
> In emacs
>
> 	(put 'with-test-prefix 'scheme-indent-function 1)
> [...]

Ah. Thanks for the tip. I had overlooked it.

>
>>  (let ((conv (lambda (x) (if (x > 10) 10 x))))
>>    (pass-if "converter 1" (let ((a (make-parameter 32))) (eqv? (a) 32)))
>
> A leftover from something?  conv seems unused, and "(x > 10)" looks
> pretty doubtful.
>

Ooops. An error. 'conv', or rather, a correct version of it, was meant
to be used in the tests within the 'let'. I've corrected it.

> Otherwise looks good to me.

Great. Please find attached a new version of both files that,
hopefully, addresses the above issues (except the single lookup).

Best regards,
jao
-- 
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare. - Blair Houghton.


[-- Attachment #2: srfi-39.scm --]
[-- Type: application/octet-stream, Size: 4052 bytes --]

;;; srfi-39.scm --- Parameter objects

;; 	Copyright (C) 2004 Free Software Foundation, Inc.
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this software; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
;; Boston, MA 02111-1307 USA
;;
;; As a special exception, the Free Software Foundation gives permission
;; for additional uses of the text contained in its release of GUILE.
;;
;; The exception is that, if you link the GUILE library with other files
;; to produce an executable, this does not by itself cause the
;; resulting executable to be covered by the GNU General Public License.
;; Your use of that executable is in no way restricted on account of
;; linking the GUILE library code into it.
;;
;; This exception does not however invalidate any other reasons why
;; the executable file might be covered by the GNU General Public License.
;;
;; This exception applies only to the code released by the
;; Free Software Foundation under the name GUILE.  If you copy
;; code from other Free Software Foundation releases into a copy of
;; GUILE, as the General Public License permits, the exception does
;; not apply to the code that you add in this way.  To avoid misleading
;; anyone as to the status of such modified files, you must delete
;; this exception notice from them.
;;
;; If you write modifications of your own for GUILE, it is your choice
;; whether to permit this exception to apply to your modifications.
;; If you do not wish that, delete this exception notice.

;;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
;;; Date: 2004-05-05

;;; Commentary:

;; This is an implementation of SRFI-39 (Parameter objects).
;;
;; The implementation is based on Guile's fluid objects, and is, therefore,
;; thread-safe (parameters are thread-local).
;;
;; In addition to the forms defined in SRFI-39 (`make-parameter',
;; `parameterize'), a new procedure `with-parameters*' is provided.
;; This procedure is analogous to `with-fluids*' but taking as first
;; argument a list of parameter objects instead of a list of fluids.
;;

;;; Code:

(define-module (srfi srfi-39)
  #:export (make-parameter
            with-parameters*) ;; procedure not in srfi-39.
  #:export-syntax (parameterize))

(define fluid-prop (list 0))
(define conv-prop (list 0))

(define (make-parameter val . conv)
  (let ((value (make-fluid))
        (conv (if (null? conv)
                  identity
                  (if (null? (cdr conv))
                      (car conv)
                      (scm-error 'wrong-number-of-args
                                 "make-parameter"
                                 "Wrong number of arguments")))))
    (fluid-set! value (conv val))
    (lambda new-value
      (cond
       ((null? new-value) (fluid-ref value))
       ((null? (cdr new-value))
        (fluid-set! value (conv (car new-value))))
       ((eq? (cadr new-value) fluid-prop) value) ; used by parameterize
       ((eq? (cadr new-value) conv-prop) conv)   ; ditto
       (else (error "A parameter expects 0 or 1 arguments" new-value))))))

(define-macro (parameterize params . body)
  `(with-parameters* (list ,@(map car params))
                     (list ,@(map cadr params))
                     (lambda () ,@body)))

(define (with-parameters* params values thunk)
  (with-fluids* (map (lambda (p) (p #f fluid-prop)) params)
                (map (lambda (p v) ((p #f conv-prop) v)) params values)
                thunk))

\f
; arch-tag: 536b6766-9947-4ad0-a35e-2420000d8a72

[-- Attachment #3: srfi-39.test --]
[-- Type: application/octet-stream, Size: 3707 bytes --]

;; srfi-39.test -- Test suite for SRFI-39's functions. -*- scheme -*-

;; Copyright (C) 2004 Free Software Foundation, Inc.
;; Author: Jose A Ortega Ruiz <jao@gnu.org>
;; Date: Mon May 10, 2004 00:19

;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


(use-modules (srfi srfi-39)
             (test-suite lib))

(with-test-prefix "make-parameter"

  (pass-if-exception "too few args" exception:wrong-num-args (make-parameter))

  (pass-if-exception "too many args" exception:wrong-num-args
    (make-parameter 'a 'b 'c))

  (pass-if-exception "wrong converter type"
      (cons 'misc-error "^Wrong type to apply:.*") (make-parameter 'a 'b))

  (pass-if-exception "wrong converter type"
      exception:wrong-num-args (make-parameter 'a (lambda (a x) a)))

  (pass-if "numerical value"
    (let ((a (make-parameter 3))) (eqv? (a) 3)))

  (pass-if "string value"
    (let ((b (make-parameter "foo"))) (string=? (b) "foo")))

  (pass-if "change value 1"
    (let ((a (make-parameter 3)))
      (a 34)
      (eqv? (a) 34)))

  (pass-if "change value 2"
    (let ((a (make-parameter 23)))
      (a "foo")
      (string=? (a) "foo")))


  (let ((conv (lambda (x) (if (> x 10) 10 x))))
    (pass-if "converter 1"
      (let ((a (make-parameter 32 conv))) (eqv? (a) 10)))
    (pass-if "converter 2"
      (let ((a (make-parameter 3 conv))) (eqv? (a) 3)))
    (pass-if "converter 3"
      (let ((a (make-parameter 3 conv))) (a 89) (eqv? (a) 10)))))

(with-test-prefix "parameterize"

  (let* ((a (make-parameter 10))
         (b (make-parameter 20))
         (c (make-parameter 2 (lambda (x) (if (< x 10) x 10))))
         (d (make-parameter 1 (lambda (x) (if (< x 10) x 10))))
         (test-values (lambda (x y z w)
                        (and (eqv? (a) x)
                             (eqv? (b) y)
                             (eqv? (c) z)
                             (eqv? (d) w)))))

    (pass-if "simple"
      (and
       (test-values 10 20 2 1)
       (parameterize ((a 2) (b 1)) (test-values 2 1 2 1))
       (test-values 10 20 2 1)))

    (pass-if "nested"
      (and
       (test-values 10 20 2 1)
       (parameterize ((a 2) (b 1))
         (and (test-values 2 1 2 1)
              (parameterize ((d 8)) (test-values 2 1 2 8))
              (test-values 2 1 2 1)))
       (test-values 10 20 2 1)))

    (pass-if "simple with converter"
      (and
       (test-values 10 20 2 1)
       (parameterize ((c 43) (d 1)) (test-values 10 20 10 1))
       (test-values 10 20 2 1)
       (parameterize ((a 1) (c 100) (d 21)) (test-values 1 20 10 10))
       (test-values 10 20 2 1)))

    (pass-if "nested with converter"
      (and
       (test-values 10 20 2 1)
       (parameterize ((c 24) (b 1))
         (and (test-values 10 1 10 1)
              (parameterize ((d 88))
                (a 15)
                (and
                 (test-values 15 1 10 10)
                 (parameterize ((d 3)) (test-values 15 1 10 3))
                 (test-values 15 1 10 10)))
              (test-values 15 1 10 1)))
       (test-values 15 20 2 1)))))

[-- Attachment #4: Type: text/plain, Size: 142 bytes --]

_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel

  reply	other threads:[~2004-05-13 23:23 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-05-06  1:02 srfi-39 implementation Jose A Ortega Ruiz
2004-05-06  1:24 ` srfi-39 implementation -- Bug fix Jose A Ortega Ruiz
2004-08-15 20:43   ` Marius Vollmer
2004-05-09  0:48 ` srfi-39 implementation Kevin Ryde
2004-05-09  1:09   ` Paul Jarc
2004-05-10  0:21   ` Jose A. Ortega Ruiz
2004-05-13 21:10     ` Kevin Ryde
2004-05-13 23:23       ` Jose A. Ortega Ruiz [this message]
2004-07-19 20:17         ` Marius Vollmer

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/guile/

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

  git send-email \
    --in-reply-to=871xlnq5br.fsf@holmes.localdomain \
    --to=jao@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).