Home   Archive   Permalink



Would this function be called three times and how does one avoid that

Let us say I have a function that produces three strings. The only way I can think of for returning them would be to return them in a block, like this: ['string 1' 'string 2' 'string 3'].
    
Let us say the function is called RETURN-STRINGS. If I want to work with the three returned strings, and code something like this:
    
print first RETURN-STRINGS
print second RETURN-STRINGS
print third RETURN-STRINGS
    
I would suppose that RETURN-STRINGS would be executed three times, which would be unnecessary.
    
To get RETURN-STRINGS to execute only once, would I code:
    
RETURNED-STRING-BLOCK: RETURN-STRINGS
print RETURNED-STRING-BLOCK/1
print RETURNED-STRING-BLOCK/2
print RETURNED-STRING-BLOCK/3
    
OR, would the line 'RETURNED-STRING-BLOCK: RETURN-STRINGS' just create a different reference to RETURN-STRING, and would RETURN-STRINGS STILL be executed three times?
    
Thank you.

posted by:   Steven White       29-Aug-2016/17:39:21-7:00



> would RETURN-STRINGS STILL be executed three times?
    
It'd be executed just the once, and it returns a block that you assign to RETURNED-STRING-BLOCK.
    
Another way to retain the three returned strings:
    
set [s1 s2 s3] RETURN-STRINGS
    
print s1
etc

posted by:   Sunanda       29-Aug-2016/18:08:42-7:00



Let us say your block! Of string!(s) and you tagged it with a set-word!, s like so:
    
s: [
    {Any coward can fight a battle when he's sure of winning but give me the man who has pluck to fight when he's sure of losing.}
    {The first ingredient in conversation is truth the next good sense the third good humor and the fourth wit.}
    {Diligence is the philosopher's stone that turns everything into gold.}
]
    
The REBEL, REBOL Compositional way is to define a word that tags a function for data transformation:
    
deblocker: func [
    {takes a block and prints datum in the block, if string!}
    b [block!]
    /local
][
    
    forall b [
        if string? b/1 [
            print b/1
            ;; OR ANY STRING PROCESSING WORD YOU HAVE DEFINED
            ;; E.G., DESTRINGER b/1    
        ]
    ]
]
    
>> deblocker s
Any coward can fight a battle when he's sure of winning but give me the man
who has pluck to fight when he's sure of losing.
The first ingredient in conversation is truth the next good sense the third
good humor and the fourth wit.
Diligence is the philosopher's stone that turns everything into gold.
    
    
Get RVC Zen and master REBOL here:
    
The RVC, REBOL and the Clone Called Red
https://timeserieslord.github.io/rvc/
    
REBOL is all about block!(s). You should shuttle groups of data in block!(s).
    
    


posted by:   Time Series Lord       4-Oct-2016/18:21:58-7:00