vocabs.hierachy: redo with cleaner API

db4
Slava Pestov 2009-07-06 04:23:26 -05:00
parent 6aaad1ea9f
commit 5484bd3241
2 changed files with 60 additions and 57 deletions

View File

@ -7,19 +7,18 @@ $nl
"Loading vocabulary hierarchies:" "Loading vocabulary hierarchies:"
{ $subsection load } { $subsection load }
{ $subsection load-all } { $subsection load-all }
"Getting all vocabularies on disk:" "Getting all vocabularies from disk:"
{ $subsection all-vocabs } { $subsection all-vocabs }
{ $subsection all-vocabs-seq } { $subsection all-vocabs-recursive }
"Getting " { $link "vocabs.metadata" } " for all vocabularies on disk:" "Getting all vocabularies from disk whose names which match a string prefix:"
{ $subsection child-vocabs }
{ $subsection child-vocabs-recursive }
"Getting " { $link "vocabs.metadata" } " for all vocabularies from disk:"
{ $subsection all-tags } { $subsection all-tags }
{ $subsection all-authors } ; { $subsection all-authors } ;
ABOUT: "vocabs.hierarchy" ABOUT: "vocabs.hierarchy"
HELP: all-vocabs
{ $values { "assoc" "an association list mapping vocabulary roots to sequences of vocabulary specifiers" } }
{ $description "Outputs an association list of all vocabularies which have been loaded or are available for loading." } ;
HELP: load HELP: load
{ $values { "prefix" string } } { $values { "prefix" string } }
{ $description "Load all vocabularies that match the provided prefix." } { $description "Load all vocabularies that match the provided prefix." }
@ -28,6 +27,3 @@ HELP: load
HELP: load-all HELP: load-all
{ $description "Load all vocabularies in the source tree." } ; { $description "Load all vocabularies in the source tree." } ;
HELP: all-vocabs-under
{ $values { "prefix" string } { "vocabs" "a sequence of vocabularies" } }
{ $description "Return a sequence of vocab or vocab-links for each vocab matching the provided prefix. Unlike " { $link all-child-vocabs } " this word will return both loaded and unloaded vocabularies." } ;

View File

@ -1,11 +1,18 @@
! Copyright (C) 2007, 2009 Slava Pestov. ! Copyright (C) 2007, 2009 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: arrays assocs combinators.short-circuit fry USING: accessors arrays assocs combinators.short-circuit fry
io.directories io.files io.files.info io.pathnames kernel make io.directories io.files io.files.info io.pathnames kernel make
memoize namespaces sequences sorting splitting vocabs sets memoize namespaces sequences sorting splitting vocabs sets
vocabs.loader vocabs.metadata vocabs.errors ; vocabs.loader vocabs.metadata vocabs.errors ;
RENAME: child-vocabs vocabs => vocabs:child-vocabs
IN: vocabs.hierarchy IN: vocabs.hierarchy
TUPLE: vocab-prefix name ;
C: <vocab-prefix> vocab-prefix
M: vocab-prefix vocab-name name>> ;
<PRIVATE <PRIVATE
: vocab-subdirs ( dir -- dirs ) : vocab-subdirs ( dir -- dirs )
@ -15,74 +22,76 @@ IN: vocabs.hierarchy
] filter ] filter
] with-directory-files natural-sort ; ] with-directory-files natural-sort ;
: (all-child-vocabs) ( root name -- vocabs )
[
vocab-dir append-path dup exists?
[ vocab-subdirs ] [ drop { } ] if
] keep
[ '[ [ _ "." ] dip 3append ] map ] unless-empty ;
: vocab-dir? ( root name -- ? ) : vocab-dir? ( root name -- ? )
over over
[ ".factor" vocab-dir+ append-path exists? ] [ ".factor" vocab-dir+ append-path exists? ]
[ 2drop f ] [ 2drop f ]
if ; if ;
: vocabs-in-dir ( root name -- ) : (child-vocabs) ( root prefix -- vocabs )
dupd (all-child-vocabs) [ [ vocab-dir append-path dup exists? [ vocab-subdirs ] [ drop { } ] if ]
2dup vocab-dir? [ dup >vocab-link , ] when [ nip [ '[ [ _ "." ] dip 3append ] map ] unless-empty ]
vocabs-in-dir [ drop '[ _ over vocab-dir? [ >vocab-link ] [ <vocab-prefix> ] if ] map ]
] with each ; 2tri ;
PRIVATE> : ((child-vocabs-recursive)) ( root name -- )
dupd vocab-name (child-vocabs)
[ dup , ((child-vocabs-recursive)) ] with each ;
: all-vocabs ( -- assoc ) : (child-vocabs-recursive) ( root name -- seq )
vocab-roots get [ [ ((child-vocabs-recursive)) ] { } make ;
dup [ "" vocabs-in-dir ] { } make
] { } map>assoc ;
: all-vocabs-under ( prefix -- vocabs ) : no-rooted ( seq -- seq' ) [ find-vocab-root not ] filter ;
[
[ vocab-roots get ] dip '[ _ vocabs-in-dir ] each
] { } make ;
MEMO: all-vocabs-seq ( -- seq ) : one-level-only? ( name prefix -- ? )
"" all-vocabs-under ; ?head [ "." split1 nip not ] dip and ;
<PRIVATE
: unrooted-child-vocabs ( prefix -- seq ) : unrooted-child-vocabs ( prefix -- seq )
[ vocabs no-rooted ] dip
dup empty? [ CHAR: . suffix ] unless dup empty? [ CHAR: . suffix ] unless
vocabs '[ vocab-name _ one-level-only? ] filter ;
[ find-vocab-root not ] filter
[ : unrooted-child-vocabs-recursive ( prefix -- seq )
vocab-name swap ?head CHAR: . rot member? not and vocabs:child-vocabs no-rooted ;
] with filter
[ vocab ] map ;
PRIVATE> PRIVATE>
: all-child-vocabs ( prefix -- assoc ) : no-prefixes ( seq -- seq' ) [ vocab-prefix? not ] filter ;
vocab-roots get [
dup pick (all-child-vocabs) [ >vocab-link ] map
] { } map>assoc
swap unrooted-child-vocabs f swap 2array suffix ;
: all-child-vocabs-seq ( prefix -- assoc ) : no-roots ( assoc -- seq ) values concat ;
vocab-roots get swap '[
dup _ (all-child-vocabs) : child-vocabs ( prefix -- assoc )
[ vocab-dir? ] with filter [ [ vocab-roots get ] dip '[ dup _ (child-vocabs) ] { } map>assoc ]
] map concat ; [ unrooted-child-vocabs [ vocab ] map f swap 2array ]
bi suffix ;
: all-vocabs ( -- assoc )
"" child-vocabs ;
: child-vocabs-recursive ( prefix -- assoc )
[ [ vocab-roots get ] dip '[ dup _ (child-vocabs-recursive) ] { } map>assoc ]
[ unrooted-child-vocabs-recursive [ vocab ] map f swap 2array ]
bi suffix ;
MEMO: all-vocabs-recursive ( -- assoc )
"" child-vocabs-recursive ;
: all-vocab-names ( -- seq )
all-vocabs-recursive no-roots no-prefixes [ vocab-name ] map ;
<PRIVATE <PRIVATE
: filter-unportable ( seq -- seq' ) : filter-unportable ( seq -- seq' )
[ vocab-name unportable? not ] filter ; [ vocab-name unportable? not ] filter ;
: collect-vocabs ( quot -- seq )
[ all-vocabs-recursive no-roots no-prefixes ] dip
gather natural-sort ; inline
PRIVATE> PRIVATE>
: (load) ( prefix -- failures ) : (load) ( prefix -- failures )
all-vocabs-under child-vocabs-recursive
filter-unportable filter-unportable
require-all ; require-all ;
@ -92,8 +101,6 @@ PRIVATE>
: load-all ( -- ) : load-all ( -- )
"" load ; "" load ;
MEMO: all-tags ( -- seq ) MEMO: all-tags ( -- seq ) [ vocab-tags ] collect-vocabs ;
all-vocabs-seq [ vocab-tags ] gather natural-sort ;
MEMO: all-authors ( -- seq ) MEMO: all-authors ( -- seq ) [ vocab-authors ] collect-vocabs ;
all-vocabs-seq [ vocab-authors ] gather natural-sort ;