1. As a style thing, use hyphens to separate the "words" in your names:
cell-address, cell-at, do-cell! (probably better-named change-cell, to be
consistent with the convention of "changer" words), set-cell, do-line.

2. Short-circuiting can be accomplished with short-circuit combinators:
http://docs.factorcode.org/content/article-combinators.short-circuit.htmlE.g.,

  { [ cy by = ] [ cx bx = ] } 0&& [ f continue! ] when

3.  Isn't your "continue" logic just trying to break out of the loop at
some specific point?  I.e., if this were a C-style language, you'd just use
a break statement?  If so, you could look into with-return:
http://docs.factorcode.org/content/word-with-return,continuations.html

4.  Your stack effects would read better if you followed the naming
conventions: http://docs.factorcode.org/content/article-effects.html  Also
worth doing is declaring the "nested" stack-effects of the input
quotations.  E.g.,

    :: change-cell ( array x y quot: ( elt -- newelt ) -- ) ... ;

5.  In general, I can't make heads or tails of do-line.  Is there a working
C-style implementation you're trying to port?  If so, could you link to it?

Regards,
--Alex Vondrak

On Wed, Jul 3, 2013 at 11:18 AM, Mark Green <m...@antelope.nildram.co.uk>wrote:

> Hi,
>
> Thanks very much for the previous help. Since then I've tried to refine
> the Bresenham routine a bit and did actually get it working, but then
> decided to indulge my functional-programmer urges and try to make it higher
> level. Unfortunately, this hit another problem..
>
> The idea of doline is that it takes two coordinates and a quotation,
> traces a line between the coordinates, and calls the quotation at each
> point along the line, leaving the x and y coordinates on the stack for it.
> The quotation is expected to leave two value on the stack itself: a
> "return" value which is what doline should return if that's the end of the
> line, and a "continue" value which specifies if the quotation wants to
> force the line loop to halt at that point. The line loop may halt without
> being forced if it has reached the destination coordinate, which is why the
> quotation is always expected to push a return and continue value, rather
> than only pushing a return if continue is f.
>
> The problem, is after calling the quotation, if the quotation's continue
> value is t (ie, it doesn't want to halt) and the line has not reached the
> end, then an unused return value is left on the stack and I want to drop
> it. Unfortunately it seems that there is no conditional function that will
> allow me to drop a value based on a condition, as all of the conditions
> seem to require that the stack is balanced on both sides of the
> conditional. So this code refuses to compile because of the drop inside the
> when block. Is there any way of doing this?
>
> (Also, is the manual short circuit optimization of "and" into two whens
> necessary or would the compiler have done it automatically? And, the code
> is still.. um, rather C-like.. is there a way to style this better?)
>
> Mark
>
>
> ! Create a new 10x10 grid
> : <grid> ( -- byte-array ) 100 <byte-array> ;
>
> ! Get address of cell x,y
> : celladdress ( n n -- n ) 10 * + ;
>
> ! Get value of cell at x,y
> : cellat ( byte-array n n -- n ) celladdress swap nth ;
>
> ! Do operation on cell at x,y
> :: docell! ( array x y op -- ) x y celladdress array op change-nth ; inline
>
> ! Set value of cell at x,y
> :: setcell! ( array x y val -- )  array x y [ drop val ] docell! ; inline
>
> :: doLine ( ax ay bx by code -- codeout )
>      bx ax - abs :> dx!
>      by ay - abs :> dy!
>      dx dy - :> err!
>      t :> continue!
>      ax :> cx!
>      ay :> cy!
>      [ continue ] [
>         cx cy code call continue!
>         cy by = [ cx bx = [ f continue! ] when ] when
>         [ continue ] [ drop
>           err 2 *
>           dup 0 dy - > [ err dy - err! cx dx sgn + cx! ] when
>           dx < [ err dx + err! cy dy sgn + cy! ] when
>         ] when
>      ] while ; inline
>
> :: los? ( map ax ay bx by -- bool ) ax ay bx by [| cx cy | map cx cy
> cellat 0 = dup ] doLine ;
>
>
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by Windows:
>
> Build for Windows Store.
>
> http://p.sf.net/sfu/windows-dev2dev
> _______________________________________________
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
>
------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to