Paste: method override: jsvar-encode

Author: otoburb
Mode: factor
Date: Tue, 21 Jun 2011 20:45:13
Plain Text |
M: hashtable json-print ( hashtable -- )
    CHAR: { write1 
    [ [ swap jsvar-encode >json % CHAR: : , >json % ] "" make ]
    { } assoc>map "," join write 
    CHAR: } write1 ;


! I want to remove jsvar-encode

M: hashtable json-print ( hashtable -- )
    CHAR: { write1 
    [ [ swap >json % CHAR: : , >json % ] "" make ]
    { } assoc>map "," join write 
    CHAR: } write1 ;

Annotation: Cannot apply "call" to a run-time computed value

Author: otoburb
Mode: factor
Date: Thu, 23 Jun 2011 05:36:10
Plain Text |
M: hashtable json-print ( hashtable -- )
    CHAR: { write1 
    jsvar-encode? get 
    [ [ [ swap jsvar-encode >json % CHAR: : , >json % ] "" make ] ] 
    [ [ [ swap >json % CHAR: : , >json % ] "" make ] ] if
    { } assoc>map "," join write 
    CHAR: } write1 ;

! Why doesn't the above work? I'd much rather be able to declare the word as per above, rather than the below: 

M: hashtable json-print ( hashtable -- )
    CHAR: { write1 
    [ [ swap jsvar-encode? get [ jsvar-encode ] when >json % CHAR: : , >json % ] "" make ] 
    { } assoc>map "," join write 
    CHAR: } write1 ;

Annotation: currify the variable get

Author: jon
Mode: factor
Date: Thu, 23 Jun 2011 15:03:19
Plain Text |
If you want to avoid getting the variable for each key/value, I'd say the idiomatic way to go would be to get jsvar-encode? outside and currify it into the quotation:

M: hashtable json-print ( hashtable -- )
    CHAR: { write1
    jsvar-encode? get 
    '[ [ swap _ [ jsvar-encode ] when >json % CHAR: : , >json % ] "" make ] 
     { } assoc>map "," join write 
    CHAR: } write1 ;

(or something along these lines..)

Annotation: currify'ing the variable worked

Author: otoburb
Mode: factor
Date: Tue, 5 Jul 2011 02:55:52
Plain Text |
@jon You are awesome. That worked like a charm. I had a needlessly complex solution that also currified, but yours was much more elegant. Thanks.

New Annotation

Summary:
Author:
Mode:
Body: