Intset-next micro-optimizations
[bpt/guile.git] / module / srfi / srfi-39.scm
1 ;;; srfi-39.scm --- Parameter objects
2
3 ;; Copyright (C) 2004, 2005, 2006, 2008, 2011 Free Software Foundation, Inc.
4 ;;
5 ;; This library is free software; you can redistribute it and/or
6 ;; modify it under the terms of the GNU Lesser General Public
7 ;; License as published by the Free Software Foundation; either
8 ;; version 3 of the License, or (at your option) any later version.
9 ;;
10 ;; This library is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;; Lesser General Public License for more details.
14 ;;
15 ;; You should have received a copy of the GNU Lesser General Public
16 ;; License along with this library; if not, write to the Free Software
17 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19 ;;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
20 ;;; Date: 2004-05-05
21
22 ;;; Commentary:
23
24 ;; This is an implementation of SRFI-39 (Parameter objects).
25 ;;
26 ;; The implementation is based on Guile's fluid objects, and is, therefore,
27 ;; thread-safe (parameters are thread-local).
28 ;;
29 ;; In addition to the forms defined in SRFI-39 (`make-parameter',
30 ;; `parameterize'), a new procedure `with-parameters*' is provided.
31 ;; This procedures is analogous to `with-fluids*' but taking as first
32 ;; argument a list of parameter objects instead of a list of fluids.
33 ;;
34
35 ;;; Code:
36
37 (define-module (srfi srfi-39)
38 ;; helper procedure not in srfi-39.
39 #:export (with-parameters*)
40 #:re-export (make-parameter
41 parameterize
42 current-input-port current-output-port current-error-port))
43
44 (cond-expand-provide (current-module) '(srfi-39))
45
46 (define (with-parameters* params values thunk)
47 (let more ((params params)
48 (values values)
49 (fluids '()) ;; fluids from each of PARAMS
50 (convs '())) ;; VALUES with conversion proc applied
51 (if (null? params)
52 (with-fluids* fluids convs thunk)
53 (more (cdr params) (cdr values)
54 (cons (parameter-fluid (car params)) fluids)
55 (cons ((parameter-converter (car params)) (car values)) convs)))))