~ubuntu-branches/ubuntu/trusty/librep/trusty

« back to all changes in this revision

Viewing changes to lisp/rep/util/memoize.jl

  • Committer: Bazaar Package Importer
  • Author(s): Christian Marillat
  • Date: 2001-11-13 15:06:22 UTC
  • Revision ID: james.westby@ubuntu.com-20011113150622-vgmgmk6srj3kldr3
Tags: upstream-0.15.2
ImportĀ upstreamĀ versionĀ 0.15.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
;; memoize.jl -- create caching-enabled functions
 
2
;; Copyright (C) 2000 John Harper <john@dcs.warwick.ac.uk>
 
3
 
 
4
;; $Id: memoize.jl,v 1.5 2000/07/23 22:24:33 john Exp $
 
5
 
 
6
;; This file is part of librep.
 
7
 
 
8
;; librep is free software; you can redistribute it and/or modify it
 
9
;; under the terms of the GNU General Public License as published by
 
10
;; the Free Software Foundation; either version 2, or (at your option)
 
11
;; any later version.
 
12
 
 
13
;; librep is distributed in the hope that it will be useful, but
 
14
;; WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
;; GNU General Public License for more details.
 
17
 
 
18
;; You should have received a copy of the GNU General Public License
 
19
;; along with librep; see the file COPYING.  If not, write to
 
20
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 
21
 
 
22
(define-structure rep.util.memoize
 
23
 
 
24
    (export memoize memoize-function)
 
25
 
 
26
    (open rep
 
27
          rep.data.tables)
 
28
 
 
29
  (define-structure-alias memoize rep.util.memoize)
 
30
 
 
31
  (define (memoize f)
 
32
    "Create and return a caching version of the function F. F may not be
 
33
an autoload definition."
 
34
 
 
35
    (unless (functionp f)
 
36
      (error "can only memoize functions: %s" f))
 
37
 
 
38
    (let ((cache (make-table equal-hash equal)))
 
39
 
 
40
      (lambda args
 
41
        (or (table-ref cache args)
 
42
            (table-set cache args (apply f args))))))
 
43
 
 
44
  ;; backwards compatibility
 
45
  (define memoize-function memoize))