From dcc3bf64a638aebe3baf61f50bcc850ba807488c Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Tue, 24 Jan 2017 14:50:44 -0800 Subject: [PATCH] simple-flat-file: simplify expand-ranges. --- .../simple-flat-file-tests.factor | 4 ++- .../simple-flat-file/simple-flat-file.factor | 28 ++++++------------- 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/basis/simple-flat-file/simple-flat-file-tests.factor b/basis/simple-flat-file/simple-flat-file-tests.factor index 292500ac47..6d8fcc4ef3 100644 --- a/basis/simple-flat-file/simple-flat-file-tests.factor +++ b/basis/simple-flat-file/simple-flat-file-tests.factor @@ -7,7 +7,6 @@ IN: simple-flat-file.tests MEMO: ( -- code-table ) "vocab:simple-flat-file/test1.txt" flat-file>biassoc ; - { 0 } [ 0 at ] unit-test { 0 } [ 0 value-at ] unit-test @@ -19,3 +18,6 @@ MEMO: ( -- code-table ) { 0xAD31 } [ 0x8258 at ] unit-test { 0x8258 } [ 0xAD31 value-at ] unit-test + +{ 1 } [ "1" expand-range ] unit-test +{ { 1 31 } } [ "1..1F" expand-range ] unit-test diff --git a/basis/simple-flat-file/simple-flat-file.factor b/basis/simple-flat-file/simple-flat-file.factor index 49630510ac..793a3fd67a 100644 --- a/basis/simple-flat-file/simple-flat-file.factor +++ b/basis/simple-flat-file/simple-flat-file.factor @@ -1,7 +1,8 @@ ! Copyright (C) 2009 Daniel Ehrenberg ! See http://factorcode.org/license.txt for BSD license. -USING: sequences splitting kernel math.parser io.files io.encodings.utf8 -biassocs ascii namespaces arrays make assocs interval-maps sets ; +USING: arrays ascii assocs biassocs interval-maps +io.encodings.utf8 io.files kernel math.parser sequences +splitting ; IN: simple-flat-file : drop-comments ( seq -- newseq ) @@ -31,24 +32,11 @@ IN: simple-flat-file : data ( filename -- data ) utf8 file-lines drop-comments [ split-; ] map! ; -SYMBOL: interned +: expand-range ( range -- range' ) + ".." split1 [ hex> ] bi@ [ 2array ] when* ; -: range, ( value key -- ) - swap interned get - [ = ] with find nip 2array , ; - -: expand-ranges ( assoc -- interval-map ) - [ - [ - swap CHAR: . over member? [ - ".." split1 [ hex> ] bi@ 2array - ] [ hex> ] if range, - ] assoc-each - ] { } make ; - -: process-interval-file ( ranges -- table ) - dup values members interned - [ expand-ranges ] with-variable ; +: expand-ranges ( ranges -- table ) + [ [ expand-range ] dip ] assoc-map ; : load-interval-file ( filename -- table ) - data process-interval-file ; + data expand-ranges ;