Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Problem with 'collect?

 [1/7] from: andrew::martin::colenso::school::nz at: 13-Aug-2003 16:21


With this script: probe foreach Row [[A 1 B 2] [A 11 B 12]] collect [ reform selects Row [A B] ] I get this result: [11 12 "11 12"] I want to get this: ["1 2" "11 12"] I using these functions: Collect: function [ {Collects the results of block evaluations.} Block [block!] "The block to 'do." /Only "Inserts the result as a series." /Full "Don't ignore none! values." /Initial Results [series! datatype!] "Specifies the type of the result." ] [ Break Result ] [ Break: func [ "Breaks out of the 'Collect." /Return "Forces the loop function to return a Value." Value [any-type!] ] [ system/words/break/return either Return [ Value ] [ Results ] ] Results: any [ all [ datatype? Results make Results 0 ] Results copy [] ] compose/deep [ if not any [ unset? set/any 'Result do [(bind Block 'Break)] (pick [[none? :Result] []] not Full) ] [ (pick [insert insert/only] not Only) tail Results :Result Results ] Results ] ] Selects: function [ "Repeatedly finds a value in the series and returns the value or series after it." Series [series! port!] Values [block!] /Part "Limits the search to a given length or position." Range [number! series! port!] /Only "Treats a series value as a single value." /Case "Characters are case-sensitive." /Any "Enables the * and ? wildcards." /With "Allows custom wildcards." Wild [string!] {Specifies alternates for "*" and "?".} /Skip "Treat the series as records of fixed size." ] [ Body ] [ Body: reduce [ foreach Refinement first :Selects collect/initial [ if equal? /local Refinement [ break ] all [ refinement? Refinement get bind Refinement: to word! Refinement 'Body Refinement ] ] to path! 'Select 'Series 'Value ] all [ Part insert/only tail Body Range ] all [ With insert/only tail Body Wild ] foreach Value Values collect Body ] I feel that I've got something wrong in 'Collect, as it looks like the value attached to 'Results gets muddled up between the various uses of 'Collect. Can anyone point out what I've got wrong? Thanks! Andrew J Martin Attendance Officer & Information Systems Trouble Shooter Colenso High School Arnold Street, Napier. Tel: 64-6-8310180 ext 826 Fax: 64-6-8336759 http://colenso.net/scripts/Wiki.r?AJM http://www.colenso.school.nz/ DISCLAIMER: Colenso High School and its Board of Trustees is not responsible (or legally liable) for materials distributed to or acquired from user e-mail accounts. You can report any misuse of an e-mail account to our ICT Manager and the complaint will be investigated. (Misuse can come in many forms, but can be viewed as any material sent/received that indicate or suggest pornography, unethical or illegal solicitation, racism, sexism, inappropriate language and/or other issues described in our Acceptable Use Policy.) All outgoing messages are certified virus-free by McAfee GroupShield Exchange 5.10.285.0 Phone: +64 6 843 5095 or Fax: +64 6 833 6759 or E-mail: [postmaster--colenso--school--nz]

 [2/7] from: brett:codeconscious at: 17-Aug-2003 17:13


Hi Andrew,
> I feel that I've got something wrong in 'Collect, as it looks like the > value attached to 'Results gets muddled up between the various uses of > 'Collect. Can anyone point out what I've got wrong?
Perhaps Gabriele's comment on my original collect function is the key to your problem? - Gabriele wrote:
> Small note: you should bind your RESULT word to a fresh context > (USE, etc.), otherwise you'll be relying on COLLECT's context not > being modified while the body block is getting evaluated.
Regards, Brett.

 [3/7] from: AJMartin:orcon at: 17-Aug-2003 20:07


Brett wrote:
> Perhaps Gabriele's comment on my original collect function is the key to
your problem? - Gabriele wrote:
> > Small note: you should bind your RESULT word to a fresh context (USE,
etc.), otherwise you'll be relying on COLLECT's context not being modified while the body block is getting evaluated. Thanks for the hint from Gabriele, Brett. Collect: func [ {Collects the results of block evaluations.} Block [block!] "The block to 'do." /Only "Inserts the result as a series." /Full "Don't ignore none! values." /Initial Type [series! datatype!] "Specifies the type of the result." ] [ use [Break Result Results] [ Break: func [ "Breaks out of the 'Collect." /Return "Forces the loop function to return a Value." Value [any-type!] ] [ system/words/break/return either Return [ Value ] [ Results ] ] Results: any [ all [ datatype? Type make Type 0 ] Type copy [] ] compose/deep [ if not any [ unset? set/any 'Result do [(bind Block 'Break)] (pick [[none? :Result] []] not Full) ] [ (pick [insert insert/only] not Only) tail Results :Result Results ] Results ] ] ] With this script: probe foreach Row [[A 1 B 2] [A 11 B 12]] collect [ reform selects Row [A B] ] I get this result: ["1 2" "11 12"] Which is right! :) Andrew J Martin Learning from The Matrix... ICQ: 26227169 http://www.rebol.it/Valley/ http://valley.orcon.net.nz/ http://Valley.150m.com/

 [4/7] from: brett:codeconscious at: 17-Aug-2003 18:18


> Thanks for the hint from Gabriele, Brett.
....
> Which is right! :)
Very good. Strange thing though, I had incorporated a break into my collect function with a use context too. But when I use it with your Selects function it has an error, when I cut Selects down to the following it works ?! : Selects: func [ "Repeatedly finds a value in the series and returns the value or series after it." Series [series! port!] Values [block!] ] [ foreach Value Values collect [select series value] ] Mysteries, mysteries. Anyway, here's my lastest Collect for interest. collect: func [ {Collects block evaluations, use as body in For, Repeat, etc.} block [block!] "Block to evaluate." /initial result [series! datatype!] "Initialise the result." /only "Inserts into result using Only refinement." /local ] compose [ if not initial [result: block!] result: any [all [datatype? result make result 1000] result] use [break] [ break: does [system/words/break/return result] bind reduce [ 'head pick [insert insert/only] not only 'tail result to paren! block ] 'break ] ] Regards, Brett.

 [5/7] from: brett:codeconscious at: 17-Aug-2003 18:20


In my last post my Collect function had an unneccessary compose - which can be safely deleted. Brett.

 [6/7] from: brett:codeconscious at: 17-Aug-2003 18:26


Also regarding that mysterious - error - having just used your new collect then mine, mine works. I must have had an inconsistent environment while testing. Regards, Brett.

 [7/7] from: AJMartin:orcon at: 17-Aug-2003 21:35


Brett wrote:
> Very good. Strange thing though, I had incorporated a break into my
collect function with a use context too. But when I use it with your Selects function it has an error, when I cut Selects down to the following it works ?! :
> Selects: func [ > "Repeatedly finds a value in the series and returns the value or series
<<quoted lines omitted: 4>>
> foreach Value Values collect [select series value] > ]
That would be because of this: Body: reduce [ foreach Refinement first :Selects collect/initial [ if equal? /local Refinement [ break ] all [ refinement? Refinement get bind Refinement: to word! Refinement 'Body Refinement ] ] to path! 'Select 'Series 'Value ] There's two 'collect in the one function. Andrew J Martin ICQ: 26227169 http://www.rebol.it/Valley/ http://valley.orcon.net.nz/ http://Valley.150m.com/

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted