srfi-39.scm 2.1 KB

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