Loading...

factor-talk@lists.sourceforge.net

[Prev] Thread [Next]  |  [Prev] Date [Next]

Re: [Factor-talk] Beginner question about style and stack shuffling Joe Groff Mon Mar 12 11:02:21 2012

On Mon, Mar 12, 2012 at 6:00 AM, [EMAIL PROTECTED]
<[EMAIL PROTECTED]> wrote:
> I quite like the style of something along the lines of:
>
> : swap-element ( i1 array1 i2 array2 -- )
>  [  [ nth  ] 2keep  ] 2bi@ ???? [ set-nth ] 2 smart-apply ;
>
> because it shows the dataflow better than with lexical variables.

Instead of collecting piles of related values on the data stack and
using n-ary variants of dataflow words, dataflow often becomes clearer
when you construct an object to contain related values. In your
swap-element case, each index-array pair could each be collected into
an `array-ref` object. After defining some operations on `array-ref`s,
the swap functionality becomes much easier to express (aside from the
somewhat awkward `bi-curry bi*` necessary to express transposed 2x2
dataflow):

```
TUPLE: array-ref index array ;
C: <array-ref> array-ref
: >array-ref< ( array-ref -- index array )
    [ index>> ] [ array>> ] bi ;
: get-ref ( array-ref -- elt )
    >array-ref< nth ;
: set-ref ( value array-ref -- elt )
    >array-ref< set-nth ;

: swap-refs ( x-ref y-ref -- )
    [ [ get-ref ] bi@ swap ] 2keep
    [ set-ref ] bi-curry bi* ;
```

------------------------------------------------------------------------------
Try before you buy = See our experts in action!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-dev2
_______________________________________________
Factor-talk mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/factor-talk