17.sld 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. ;;; 17.scm --- SRFI 17: Generalized set!
  2. ;; Copyright (C) 2014 Taylan Ulrich Bayırlı/Kammer
  3. ;; Author: Taylan Ulrich Bayırlı/Kammer <taylanbayirli@gmail.com>
  4. ;; Keywords: srfi 17 srfi-17 generalized set!
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU Lesser General Public License as
  7. ;; published by the Free Software Foundation, either version 3 of the
  8. ;; License, or (at your option) any later version.
  9. ;; This program 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
  12. ;; GNU Lesser General Public License for more details.
  13. ;; You should have received a copy of the GNU Lesser General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;;; Code:
  18. (define-library (srfi 17)
  19. (export set! setter getter-with-setter)
  20. (import
  21. (rename (scheme base) (set! %set!))
  22. (srfi 1))
  23. (begin
  24. (define-syntax set!
  25. (syntax-rules ()
  26. ((_ (getter arg ...) val)
  27. ((setter getter) arg ... val))
  28. ((_ var val)
  29. (%set! var val))))
  30. (define setter
  31. (let ((setters `((,car . ,set-car!)
  32. (,cdr . ,set-cdr!)
  33. (,caar . ,(lambda (p v) (set-car! (car p) v)))
  34. (,cadr . ,(lambda (p v) (set-car! (cdr p) v)))
  35. (,cdar . ,(lambda (p v) (set-cdr! (car p) v)))
  36. (,cddr . ,(lambda (p v) (set-cdr! (cdr p) v)))
  37. (,list-ref . ,list-set!)
  38. (,vector-ref . ,vector-set!)
  39. (,string-ref . ,string-set!)
  40. (,bytevector-u8-ref . ,bytevector-u8-set!))))
  41. (letrec ((setter
  42. (lambda (proc)
  43. (let ((probe (assv proc setters)))
  44. (if probe
  45. (cdr probe)
  46. (error "No setter for " proc)))))
  47. (set-setter!
  48. (lambda (proc setter)
  49. (set! setters (cons (cons proc setter) setters)))))
  50. (set-setter! setter set-setter!)
  51. setter)))
  52. (define (getter-with-setter get set)
  53. (let ((proc (lambda args (apply get args))))
  54. (set! (setter proc) set)
  55. proc))
  56. ))
  57. ;;; 17.scm ends here