Paste: assoc at-path (retrieve a value from nested assocs)
Author: | x6j8x |
Mode: | factor |
Date: | Sun, 8 Mar 2009 12:19:59 |
Plain Text |
USING: sequences strings io.encodings.string io.encodings.utf8 splitting memoize
fry kernel assocs math ;
IN: assoc-path
<PRIVATE
: split-path ( string -- vector )
"." split ; inline
: rest-slice* ( seq -- seq/f )
[ length 1 = ] keep swap
[ drop f ] [ rest-slice ] if ; inline
: extract-key ( seq -- seq/f key )
[ rest-slice* ] [ first ] bi ; inline
: at@ ( key object -- value/f )
dup sequence?
[ swap '[ [ _ ] dip at ] map
[ ] filter dup
length 0 > [ drop f ] unless ]
[ at ] if ; inline
: [build-path-accessor] ( pathseq -- quot: ( assoc -- value/f ) )
'[ [ _ extract-key ] dip
at@ dup
[ [ swap [ [build-path-accessor] call ] when* ]
[ drop f ] if* ]
[ 2drop f ] if ] ; inline recursive
MEMO: [path-accessor] ( path -- quot: ( assoc -- value/f ) )
split-path [build-path-accessor] ;
PRIVATE>
: at-path ( pathstr assoc -- value/f )
swap [path-accessor] call ; inline
Author: | Samuel Tardieu |
Mode: | factor |
Date: | Sun, 8 Mar 2009 12:29:23 |
Plain Text |
USING: assocs kernel sequences splitting ;
: at-path ( path assoc -- value/f ) swap "." split [ swap at ] each ;
Author: | x6j8x |
Mode: | factor |
Date: | Sun, 8 Mar 2009 12:42:54 |
Plain Text |
USING: assocs fry kernel math sequences splitting ;
IN: assoc-path
<PRIVATE
: at@ ( key object -- value/f )
dup sequence?
[ swap '[ [ _ ] dip at ] map
[ ] filter dup
length 0 > [ drop f ] unless ]
[ at ] if ; inline
PRIVATE>
: at-path ( pathstr assoc -- value/f ) swap "." split [ swap at@ ] each ;
Author: | x6j8x |
Mode: | factor |
Date: | Sun, 8 Mar 2009 12:58:40 |
Plain Text |
USING: assocs fry kernel math sequences splitting ;
IN: assoc-path
<PRIVATE
: at@ ( key object -- value/f )
dup sequence?
[ [ at ] with map sift
dup empty? [ drop f ] when ]
[ at ] if ; inline
PRIVATE>
: at-path ( pathstr assoc -- value/f ) swap "." split [ swap at@ ] each ;
Author: | x6j8x |
Mode: | factor |
Date: | Mon, 9 Mar 2009 01:13:31 |
Plain Text |
USING: assocs fry kernel math sequences splitting ;
IN: assoc-path
<PRIVATE
: at@ ( key object -- value/f )
dup sequence?
[ [ at ] with map sift
dup empty? [ drop f ]
[ dup length 1 = [ first ] when ] if ]
[ at ] if ; inline
PRIVATE>
: at-path ( pathstr assoc -- value/f ) swap "." split [ swap at@ ] each ;
New Annotation