Paste: assoc at-path (retrieve a value from nested assocs)

Author: x6j8x
Mode: factor
Date: Sun, 8 Mar 2009 12:19:59
Plain Text |
! Copyright (c) 2009 - Sascha Matzke 
! See http://factorcode.org/license.txt for BSD license

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>

! retrieves value from nested assocs...
! "a.b.c" H{ { "a" H{ { "b" { H{ { "c" 23 } } H{ { "c" 42 } } } } } } } at-path
! => { 23 42 }
: at-path ( pathstr assoc -- value/f )
    swap [path-accessor] call ; inline

Annotation: Alternate version

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 ;

Annotation: shorter and same functionality

Author: x6j8x
Mode: factor
Date: Sun, 8 Mar 2009 12:42:54
Plain Text |
! Copyright (c) 2009 - Sascha Matzke, Samuel Tardieu
! See http://factorcode.org/license.txt for BSD license

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>

! retrieves values from nested assocs...
! "a.b.c" H{ { "a" H{ { "b" { H{ { "c" 23 } } H{ { "c" 42 } } } } } } } at-path
! => { 23 42 }
: at-path ( pathstr assoc -- value/f ) swap "." split [ swap at@ ] each ;

Annotation: better at@ word

Author: x6j8x
Mode: factor
Date: Sun, 8 Mar 2009 12:58:40
Plain Text |
! Copyright (c) 2009 - Sascha Matzke, Samuel Tardieu
! See http://factorcode.org/license.txt for BSD license

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>

! retrieves values from nested assocs...
! "a.b.c" H{ { "a" H{ { "b" { H{ { "c" 23 } } H{ { "c" 42 } } } } } } } at-path
! => { 23 42 }
: at-path ( pathstr assoc -- value/f ) swap "." split [ swap at@ ] each ;

Annotation: bugfix

Author: x6j8x
Mode: factor
Date: Mon, 9 Mar 2009 01:13:31
Plain Text |
! See http://factorcode.org/license.txt for BSD license

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>

! retrieves values from nested assocs...
! "a.b.c" H{ { "a" H{ { "b" { H{ { "c" 23 } } H{ { "c" 42 } } } } } } } at-path
! => { 23 42 }
: at-path ( pathstr assoc -- value/f ) swap "." split [ swap at@ ] each ;

New Annotation

Summary:
Author:
Mode:
Body: