add module contrib with some helpers
authorDaniel Hartwig <mandyke@gmail.com>
Fri, 8 Feb 2013 11:10:59 +0000 (19:10 +0800)
committerDaniel Hartwig <mandyke@gmail.com>
Fri, 8 Feb 2013 11:10:59 +0000 (19:10 +0800)
* figl/contrib.scm: New file.
  (memoize): Wrapper to create memoized procedures.
  (string-split): Regexp-enhanced, until Guile core gets one.

* Makefile.am: Update.

Makefile.am
figl/contrib.scm [new file with mode: 0644]

index 40d7ca9..09c9552 100644 (file)
@@ -6,6 +6,7 @@ godir=$(libdir)/guile/2.0/ccache
 SOURCES = \
        figl.scm \
        figl/config.scm \
+       figl/contrib.scm \
        figl/parse.scm \
        figl/runtime.scm \
        \
diff --git a/figl/contrib.scm b/figl/contrib.scm
new file mode 100644 (file)
index 0000000..dd92916
--- /dev/null
@@ -0,0 +1,59 @@
+;;; figl
+;;; Copyright (C) 2013 Andy Wingo <wingo@pobox.com>
+;;; Copyright (C) 2013 Daniel Hartwig <mandyke@gmail.com>
+;;; 
+;;; Figl is free software: you can redistribute it and/or modify it
+;;; under the terms of the GNU Lesser General Public License as
+;;; published by the Free Software Foundation, either version 3 of the
+;;; License, or (at your option) any later version.
+;;; 
+;;; Figl 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 Lesser General
+;;; Public License for more details.
+;;; 
+;;; You should have received a copy of the GNU Lesser General Public
+;;; License along with this program.  If not, see
+;;; <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+;;
+;; Small helpers that properly have no home, or are imported from
+;; other sources.
+;;
+;;; Code:
+
+(define-module (figl contrib)
+  #:use-module (ice-9 regex)
+  #:use-module (srfi srfi-69) ; hash tables
+  #:export (memoize)
+  #:replace (string-split))
+
+(define (memoize proc)
+  (let ((table (make-hash-table)))
+    (lambda args
+      (apply values
+             (hash-table-ref
+              table
+              args
+              (lambda ()
+                (call-with-values
+                    (lambda () (apply proc args))
+                  (lambda results
+                    (hash-table-set! table args results)
+                    results))))))))
+
+;; Based on code by Andy Wingo.  Eventually something like this will
+;; be included with Guile.
+(define (string-split str delimiter)
+  (if (regexp? delimiter)
+      (let ((ret (fold-matches
+                  delimiter str (cons '() 0)
+                  (lambda (m prev)
+                    (let ((parts (car prev))
+                          (start (cdr prev)))
+                      (cons (cons (substring str start (match:start m))
+                                  parts)
+                            (match:end m)))))))
+        (reverse (cons (substring str (cdr ret)) (car ret))))
+      ((@ (guile) string-split) str delimiter)))