Hi, Ladislav,

Ladislav Mecir wrote:
> 
> 1) I didn't want to criticize the ineffectivity of the algorithm.
> I would rather say, that what matters to me may be a "naturality"
> of the algorithm.
>

FWIW, the algorithm wasn't the point of my note.  I really wish I
had chosen a different example.  The real point I wanted to focus
on was the contrast between deeply nested expressions vs. a long
sequence of word-setting expressions to build up the result in an
incremental fashion, and the differences in "mental bookkeeping"
required to read and understand each.
 
> 
> 3) Volker is right, that the introduction of iteration using
> FOREACH simplifies the code. For the comparison purposes, here
> is an iterative version of FLAT-APPEND:
> 
>     flat-append: func [
>         series [series! port!]
>         value [any-type!]
>     ] [
>         either block? get/any 'value [
>             foreach element value [
>                 flat-append :series get/any 'element
>             ]
>         ] [
>             insert/only tail :series get/any 'value
>         ]
>         :series
>     ]
> 

Actually, that's still recursive!  You refactored the "horizontal"
recursion into the FOREACH, but left the "vertical" recursion in
place.  Here's a completely iterative version of the flattener:

    nb-flatten: func [
        b [block!]
        /local front result
    ][
        result: copy []
        while [not empty? b][
            either block? front: first b [
                b: compose [(front) (next b)]
            ][
                insert tail result front
                b: next b
            ]
        ]
        result
    ]

>
> 4) I noticed a paradox: the most effective way of assembling blocks
> is to use APPEND (or INSERT TAIL), but we do not have it as a native.
>

Good observation!

-jn-

-- 
; Joel Neely                             joeldotneelyatfedexdotcom
REBOL [] do [ do func [s] [ foreach [a b] s [prin b] ] sort/skip
do function [s] [t] [ t: "" foreach [a b] s [repend t [b a]] t ] {
| e s m!zauafBpcvekexEohthjJakwLrngohOqrlryRnsctdtiub} 2 ]
-- 
To unsubscribe from this list, please send an email to
[EMAIL PROTECTED] with "unsubscribe" in the 
subject, without the quotes.

Reply via email to