Howdy,
This word is from the asn1 vocabulary:
: >ber-seq-internal ( array code -- byte-array )
1array "C" pack-native swap dup length >ber-length-encoding
swapd append swap [ number>string ] map "" join >array append ;
If you study it for a while, you'll see that at an intermediate stage of the
computation, there exists 3 arrays which are eventually appended together. In
this word, the tri* combinator is applicable. tri* is one of the 'spread'
combinators. bi, tri, and tetra are the 'cleave' combinators. The same words
with a '*' suffix are the 'spread' combinators.
Here's a few of examples of tri* :
( scratchpad ) 0 0 0 [ ] [ ] [ ] tri*
----------
0
0
0
----------
( scratchpad ) [ 1 + ] [ 2 + ] [ 3 + ] tri*
----------
1
2
3
----------
( scratchpad ) [ 1 - ] [ ] [ ] tri*
----------
0
2
3
----------
( scratchpad )
So tri* "maps" the 3 quotations onto the next 3 values on the stack. bi* is
for 2 values, tetra* is for 4.
Now, back to the >ber-seq-internal word. We'd like the code to clearly express
that 3 arrays are formed and then appended together. Here's a version using
tri* :
: >ber-seq-internal ( array code -- byte-array )
swap dup
[ 1array "C" pack-native ]
[ length >ber-length-encoding ]
[ [ number>string ] map "" join >array ]
tri*
3append ;
Ah... You can clearly see the 3 pieces of code that correspond to the 3
arrays. After the tri* we stick'em together. The 'swap dup' at the beginning
makes our 3 inputs and lines em up.
Here's another version of the word using pcall instead. pcall stands for
parallel-call.
: >ber-seq-internal ( array code -- byte-array )
swap dup 3array
{ [ 1array "C" pack-native ]
[ length >ber-length-encoding ]
[ [ number>string ] map "" join >array ] }
pcall concat ;
Just another style.
These words are in the combinators.lib vocabulary.
The alternative versions of >ber-seq-internal pass the asn1 tests.
Elie, that's some good looking code by the way!
Ed
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk