Paste: Indices

Author: foo
Mode: factor
Date: Mon, 17 Nov 2008 21:41:48
Plain Text |
USING: arrays io io.files kernel math parser strings system
tools.test words namespaces make io.encodings.utf8
io.encodings.binary sequences splitting hashtables assocs peg peg.ebnf peg.search ;

IN: indexes

: db-path ( -- path ) "resource:work/indexes/db" ;
: load-db ( -- contents ) db-path utf8 file-contents ;
: index-lines ( -- lines ) load-db string-lines [ empty? not ] filter ;
: indices ( -- inds ) index-lines [ 6 head ] map ;
: names ( -- names ) index-lines [ 7 tail ] map ;
: i-n# ( -- hash ) indices names zip >hashtable ;

! second part

: src-path ( -- path ) "resource:work/indexes/kolo" ;
: load-src ( -- path ) src-path utf8 file-contents ;
: replaced ( -- ) load-src [EBNF Result = ([0-9][0-9][0-9][0-9][0-9][0-9] => [[ i-n# value-at ]] | (!([0-9][0-9][0-9][0-9][0-9][0-9][0-9]) .)+)* EBNF] replace ;

Annotation: example

Author: slava
Mode: factor
Date: Mon, 17 Nov 2008 21:54:38
Plain Text |
: replace-foo-with-bar ( string -- string' )
    [EBNF
        foo = "foo" => [[ "bar" ]]

        non-foo = (!(foo).)+ => [[ >string ]]
        text = (foo | non-foo)*
    EBNF] concat ;

( scratchpad ) "hello foo world, foo!" replace-foo-with-bar .
"hello bar world, bar!"

Annotation: works now!

Author: foo
Mode: factor
Date: Mon, 17 Nov 2008 22:31:57
Plain Text |
USING: arrays io io.files kernel math parser strings system
tools.test words namespaces make io.encodings.utf8
io.encodings.binary sequences splitting hashtables assocs peg peg.ebnf peg.search memoize ;

IN: indexes

: db-path ( -- path ) "resource:work/indexes/db" ;
: load-db ( -- contents ) db-path utf8 file-lines ;
: index-lines ( -- lines ) load-db harvest ;
: indices ( lines -- inds ) [ 6 head ] map ;
: names ( lines -- names ) [ 7 tail ] map ;
MEMO: i-n# ( -- hash ) index-lines [ indices ] [ names ] bi zip >hashtable ;
! second part

: src-path ( -- path ) "resource:work/indexes/kolo" ;
: load-src ( -- path ) src-path utf8 file-contents ;
: replaced ( -- string ) load-src
                  [EBNF index = ([0-9][0-9][0-9][0-9][0-9][0-9]) => [[ >string i-n# at ]]
                        non-index = (!(index).)+ => [[ >string ]]
                        text = (index | non-index)*
                  EBNF] concat ;

Annotation: works now!

Author: foo
Mode: factor
Date: Mon, 17 Nov 2008 22:32:19
Plain Text |
USING: arrays io io.files kernel math parser strings system
tools.test words namespaces make io.encodings.utf8
io.encodings.binary sequences splitting hashtables assocs peg peg.ebnf peg.search memoize ;

IN: indexes

: db-path ( -- path ) "resource:work/indexes/db" ;
: load-db ( -- contents ) db-path utf8 file-lines ;
: index-lines ( -- lines ) load-db harvest ;
: indices ( lines -- inds ) [ 6 head ] map ;
: names ( lines -- names ) [ 7 tail ] map ;
MEMO: i-n# ( -- hash ) index-lines [ indices ] [ names ] bi zip >hashtable ;
! second part

: src-path ( -- path ) "resource:work/indexes/kolo" ;
: load-src ( -- path ) src-path utf8 file-contents ;
: replaced ( -- string ) load-src
                  [EBNF index = ([0-9][0-9][0-9][0-9][0-9][0-9]) => [[ >string i-n# at ]]
                        non-index = (!(index).)+ => [[ >string ]]
                        text = (index | non-index)*
                  EBNF] concat ;

New Annotation

Summary:
Author:
Mode:
Body: