]> gitweb.factorcode.org Git - factor.git/commitdiff
gdbm: each-key - higher order combinator for sequential access
authorDmitry Shubin <dmitry.sh@gmail.com>
Sun, 27 Jun 2010 23:30:52 +0000 (03:30 +0400)
committerDmitry Shubin <dmitry.sh@gmail.com>
Sun, 27 Jun 2010 23:30:52 +0000 (03:30 +0400)
extra/gdbm/gdbm-docs.factor
extra/gdbm/gdbm-tests.factor
extra/gdbm/gdbm.factor

index e712da3c99b4de1b15a4c3f714c2ff83a60ecd5a..18e5d5cf33b709b82cd2bd3c5355a1b5a47de3c6 100644 (file)
@@ -44,6 +44,18 @@ HELP: exists?
 { $values { "key" object } { "?" boolean } }
 { $description "Searches for a particular key without retreiving it." } ;
 
+HELP: each-key
+{ $values { "quot" quotation } }
+{ $description "Applies the quotation to the each key in the database." } ;
+
+HELP: each-value
+{ $values { "quot" quotation } }
+{ $description "Applies the quotation to the each value in the database." } ;
+
+HELP: each-record
+{ $values { "quot" quotation } }
+{ $description "Applies the quotation to the each key-value pair in the database." } ;
+
 HELP: gdbm-file-descriptor
 { $values { "desc" integer } }
 { $description "Returns the file descriptor of the database. This is used for manual database locking if it was opened with " { $snippet "nolock" } " flag set to " { $link t } "." } ;
@@ -125,8 +137,10 @@ $nl
 { $subsections insert exists? fetch delete }
 
 { $heading "Sequential access" }
-"It is possible to iterate through all records in the database with."
+"It is possible to iterate through all records in the database with"
 { $subsections first-key next-key }
+"The following combinators, however, provide more convenient way to do that:"
+{ $subsections each-key each-value each-record }
 "The order in which records are accessed has nothing to do with the order in which records have been stored. Note that these words can only be used in read-only algorithms since delete operation re-arranges the hash table."
 ;
 
index 373ae1c624fe114fc34a6cddeb80717961a1e1d1..b720dfc0f7ce731cba0f3b680c49efcc87914c0c 100644 (file)
@@ -1,6 +1,6 @@
 ! Copyright (C) 2010 Dmitry Shubin.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: accessors continuations gdbm gdbm.ffi io.directories
+USING: accessors arrays continuations gdbm gdbm.ffi io.directories
 io.files.temp kernel sequences sets tools.test ;
 IN: gdbm.tests
 
@@ -49,12 +49,8 @@ CLEANUP
 
 [ t ]
 [
-    V{ }
-    [
-        first-key
-        [ next-key* ] [ [ swap push ] 2keep ] do while drop
-    ] with-test.db
-    V{ "foo" "bar" "baz" } set=
+    V{ } [ [ 2array append ] each-record ] with-test.db
+    V{ "foo" "bar" "baz" 42 43 44 } set=
 
 ] unit-test
 
index 021fadd514c2c29a2741049f369cb93e57eada07..6223a6b79e4d6d34c6339000731aeba20c366fc3 100644 (file)
@@ -93,6 +93,16 @@ PRIVATE>
 : first-key ( -- key/f ) first-key* drop ;
 : next-key ( key -- key/f ) next-key* drop ;
 
+:: each-key ( ... quot: ( ... key -- ... ) -- ... )
+    first-key*
+    [ [ next-key* ] [ quot keep ] do while ] when drop ; inline
+
+: each-value ( ... quot: ( ... value -- ... ) -- ... )
+    [ fetch ] prepose each-key ; inline
+
+: each-record ( ... quot: ( ... key value -- ... ) -- ... )
+    [ dup fetch ] prepose each-key ; inline
+
 : reorganize ( -- ) dbf gdbm_reorganize check-error ;
 
 : synchronize ( -- ) dbf gdbm_sync ;