[COLLECT] COLLECT function/Examples?
[1/5] from: tim-johnsons:web at: 12-May-2006 13:59
Hi Ladislav:
I've tried using this, but am unsure how.
Can you give some examples?
thanks
tim
* Ladislav Mecir <lmecir-mbox.vol.cz> [060512 06:46]:
> Gregg Irwin napsal(a):
> > ; Accepts only word! and block! values for args now.
<<quoted lines omitted: 36>>
> To unsubscribe from the list, just send an email to
> lists at rebol.com with unsubscribe as the subject.
--
Tim Johnson <tim-johnsons-web.com>
http://www.alaska-internet-solutions.com
[2/5] from: greggirwin:mindspring at: 12-May-2006 16:28
Hi Tim,
TJ> I've tried using this, but am unsure how. Can you give some
TJ> examples?
>> b: [1 2 3 4 5 6 7 8 9 10]
== [1 2 3 4 5 6 7 8 9 10]
>> collect n [foreach [x y] b [n add x y]]
== [3 7 11 15 19]
COLLECT returns a block. The word you give it is the "collector
function"; wherever that word appears in the block, a value will be
collected.
It's main goal is to eliminate tedious code. e.g.
fn: func [/local res] [
res: copy []
foreach ... [append res value]
res
]
becomes:
fn: does [collect [foreach val ... [val value]]
Does that make sense?
-- Gregg
[3/5] from: tim-johnsons:web at: 12-May-2006 15:07
* Gregg Irwin <greggirwin-mindspring.com> [060512 14:41]:
> Hi Tim,
> TJ> I've tried using this, but am unsure how. Can you give some
<<quoted lines omitted: 15>>
> fn: does [collect [foreach val ... [val value]]
> Does that make sense?
Of course...
Duh! Was thinking it worked like my 'yield function.
I see now. It will be very handy.
FYI:
I'm seeing something strange on my system:
It appears that [throw] is suppressing the docstring.
Remove it or replace with [catch] and the docstring is enabled.
Are you getting the same?
tim
> -- Gregg
>
> --
> To unsubscribe from the list, just send an email to
> lists at rebol.com with unsubscribe as the subject.
--
Tim Johnson <tim-johnsons-web.com>
http://www.alaska-internet-solutions.com
[4/5] from: greggirwin::mindspring::com at: 12-May-2006 17:06
Hi Tim,
TJ> I'm seeing something strange on my system:
TJ> It appears that [throw] is suppressing the docstring.
TJ> Remove it or replace with [catch] and the docstring is enabled.
TJ> Are you getting the same?
Yes. I've seen this before. Putting the func attributes after the doc
string works as well. New version below. Thanks for "catching" that!
-- Gregg
collect: func [
{Collects values, returning them as a series.}
[throw]
'emit [word!] "Word used to collect values"
block [block!] "Block to evaluate"
/into dest [series!] "Where to append results"
/only "Insert series values as series"
] [
; Create a new context containing just one word (the EMIT
; argument) and set the 'emit word to refer to the new context
; word. Note the care taken to suppress possible conflicts and
; undesired evaluations.
emit: reduce [emit]
emit: first use emit reduce [emit]
use [dst ins] copy/deep [
; copy/deep lets us use just [] as the fallback value here.
dst: any [:dest []]
; make the function used to collect values.
ins: either only [[insert/only]] [[insert]]
set emit func [value [any-type!]] compose [
(ins) tail :dst get/any 'value
get/any 'value
]
do bind/copy block emit
head :dst
]
]
[5/5] from: tim-johnsons::web::com at: 12-May-2006 15:39
* Gregg Irwin <greggirwin-mindspring.com> [060512 15:27]:
> Hi Tim,
> TJ> I'm seeing something strange on my system:
<<quoted lines omitted: 3>>
> Yes. I've seen this before. Putting the func attributes after the doc
> string works as well. New version below. Thanks for "catching" that!
Got it.
Thanks
> -- Gregg
> collect: func [
<<quoted lines omitted: 27>>
> To unsubscribe from the list, just send an email to
> lists at rebol.com with unsubscribe as the subject.
--
Tim Johnson <tim-johnsons-web.com>
http://www.alaska-internet-solutions.com
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted