<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: collect code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 14:28:27 GMT</pubDate>
    <description>DZone Snippets: collect code</description>
    <item>
      <title>file-size-comparison-ctx - find files matching size criteria; dialected interface</title>
      <link>http://snippets.dzone.com/posts/show/2091</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;file-size-comparison-ctx: context [&lt;br /&gt;    =negate-op?: none&lt;br /&gt;    =or-equal?: none&lt;br /&gt;    =op: none&lt;br /&gt;    =size: 0&lt;br /&gt;    =size-mul: 1&lt;br /&gt;    =parse-end-mark: none&lt;br /&gt;&lt;br /&gt;    make-lit-word: func [val] [to lit-word! :val]&lt;br /&gt;    lit-lesser: make-lit-word "&lt;"&lt;br /&gt;    lit-greater: make-lit-word "&gt;"&lt;br /&gt;    lit-lesser-or-equal: make-lit-word "&lt;="&lt;br /&gt;    lit-greater-or-equal: make-lit-word "&gt;="&lt;br /&gt;&lt;br /&gt;    size=: [&lt;br /&gt;        (=size-mul: 1)&lt;br /&gt;        set =size number!&lt;br /&gt;        opt [&lt;br /&gt;            'bytes ; no change to size-mul&lt;br /&gt;            | ['kilobytes | 'KB] (=size-mul: 1024.0)&lt;br /&gt;            | ['megabytes | 'MB] (=size-mul: 1048576.0)&lt;br /&gt;            | ['gigabytes | 'GB] (=size-mul: 1073741824.0)&lt;br /&gt;        ]&lt;br /&gt;        (=size: =size * =size-mul)&lt;br /&gt;    ]&lt;br /&gt;    word-comparison=: [&lt;br /&gt;        [&lt;br /&gt;            ['more | 'bigger | 'larger | 'greater] (=op: 'greater)&lt;br /&gt;            | ['less | 'smaller] (=op: 'lesser)&lt;br /&gt;        ] 'than&lt;br /&gt;        opt ['or 'equal 'to (=or-equal?: true)]&lt;br /&gt;    ]&lt;br /&gt;    lit-comparison=: [&lt;br /&gt;        lit-lesser             (=or-equal?: false  =op: 'lesser)&lt;br /&gt;        | lit-greater          (=or-equal?: false  =op: 'greater)&lt;br /&gt;        | lit-lesser-or-equal  (=or-equal?: true   =op: 'lesser)&lt;br /&gt;        | lit-greater-or-equal (=or-equal?: true   =op: 'greater)&lt;br /&gt;    ]&lt;br /&gt;    rules=: [&lt;br /&gt;        (=negate-op?: =or-equal?: =op: =parse-end-mark: none)&lt;br /&gt;        opt 'if opt ['size | size?]&lt;br /&gt;        opt [['no | 'not] (=negate-op?: true)]&lt;br /&gt;        [word-comparison= | lit-comparison=]&lt;br /&gt;        (if =negate-op? [=op: pick [greater lesser] =op = 'lesser])&lt;br /&gt;        (=op: to word! rejoin [=op either =or-equal? ['-or-equal] [""] '?])&lt;br /&gt;        size=&lt;br /&gt;        =parse-end-mark:&lt;br /&gt;    ]&lt;br /&gt;&lt;br /&gt;    set 'size-comparison-cmd? func [input [block!]] [&lt;br /&gt;        parse input rules=&lt;br /&gt;        return =parse-end-mark&lt;br /&gt;    ]&lt;br /&gt;&lt;br /&gt;    set 'make-file-size-comparison-func func [spec] [&lt;br /&gt;        parse spec rules=&lt;br /&gt;        either =parse-end-mark [&lt;br /&gt;            func [file] reduce [=op 'size? 'file =size]&lt;br /&gt;        ] [none]&lt;br /&gt;    ]&lt;br /&gt;&lt;br /&gt;    set 'files-matching-size-spec func [&lt;br /&gt;        files [block!]&lt;br /&gt;        spec [block!]&lt;br /&gt;    ][&lt;br /&gt;        if match?: make-file-size-comparison-func spec [&lt;br /&gt;            collect 'keep [&lt;br /&gt;                foreach file files [if match? file [keep: file]]&lt;br /&gt;            ]&lt;br /&gt;        ]&lt;br /&gt;    ]&lt;br /&gt;]&lt;br /&gt;;foreach file files-matching-size-spec read %. [&gt;= 64 kb] [print [file size? file]]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 23 May 2006 21:43:57 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2091</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
    <item>
      <title>file-date-comparison-ctx - find files matching date criteria; dialected interface</title>
      <link>http://snippets.dzone.com/posts/show/2090</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;file-date-comparison-ctx: context [&lt;br /&gt;    =negate-op?: none&lt;br /&gt;    =or-equal?: none&lt;br /&gt;    =op: none&lt;br /&gt;    =parse-end-mark: none&lt;br /&gt;    =attr: 'modification-date&lt;br /&gt;    =date: none&lt;br /&gt;&lt;br /&gt;    make-lit-word: func [val] [to lit-word! :val]&lt;br /&gt;    lit-equal: make-lit-word "="&lt;br /&gt;    lit-lesser: make-lit-word "&lt;"&lt;br /&gt;    lit-greater: make-lit-word "&gt;"&lt;br /&gt;    lit-lesser-or-equal: make-lit-word "&lt;="&lt;br /&gt;    lit-greater-or-equal: make-lit-word "&gt;="&lt;br /&gt;&lt;br /&gt;    attr=: [&lt;br /&gt;        ['changed | 'modified | 'upated | 'modification-date]&lt;br /&gt;        | ['created | 'creation-date | 'create-date] (=attr: 'creation-date)&lt;br /&gt;        | ['accessed | 'access-date] (=attr: 'access-date)&lt;br /&gt;    ]&lt;br /&gt;    date=: [&lt;br /&gt;        [&lt;br /&gt;            set =date date!&lt;br /&gt;            | set =date file!      (=date: get-modes =date =attr)&lt;br /&gt;            | 'yesterday (=date: now/date - 1)&lt;br /&gt;            | 'today     (=date: now/date)&lt;br /&gt;            | 'tomorrow  (=date: now/date + 1)&lt;br /&gt;        ]&lt;br /&gt;    ]&lt;br /&gt;    word-comparison=: [&lt;br /&gt;        [&lt;br /&gt;            ['after | 'since | 'newer 'than] (=op: 'greater)&lt;br /&gt;            | ['before | 'older 'than] (=op: 'lesser)&lt;br /&gt;        ]&lt;br /&gt;        opt ['or 'equal 'to (=or-equal?: true)]&lt;br /&gt;    ]&lt;br /&gt;    lit-comparison=: [&lt;br /&gt;        lit-equal              (=or-equal?: false  =op: 'equal)&lt;br /&gt;        | lit-lesser           (=or-equal?: false  =op: 'lesser)&lt;br /&gt;        | lit-greater          (=or-equal?: false  =op: 'greater)&lt;br /&gt;        | lit-lesser-or-equal  (=or-equal?: true   =op: 'lesser)&lt;br /&gt;        | lit-greater-or-equal (=or-equal?: true   =op: 'greater)&lt;br /&gt;    ]&lt;br /&gt;    rules=: [&lt;br /&gt;        (&lt;br /&gt;            =negate-op?: =or-equal?: =op: =parse-end-mark: =date: none&lt;br /&gt;            =attr: 'modification-date&lt;br /&gt;        )&lt;br /&gt;        opt 'if opt ['date | 'date?]&lt;br /&gt;        opt [['no | 'not] (=negate-op?: true)]&lt;br /&gt;        opt ['date | 'date?]&lt;br /&gt;        opt attr=&lt;br /&gt;        [word-comparison= | lit-comparison=]&lt;br /&gt;        (if =negate-op? [=op: pick [greater lesser] =op = 'lesser])&lt;br /&gt;        (=op: to word! rejoin [=op either =or-equal? ['-or-equal] [""] '?])&lt;br /&gt;        date=&lt;br /&gt;        =parse-end-mark:&lt;br /&gt;    ]&lt;br /&gt;&lt;br /&gt;    set 'date-comparison-cmd? func [input [block!]] [&lt;br /&gt;        parse input rules=&lt;br /&gt;        return =parse-end-mark&lt;br /&gt;    ]&lt;br /&gt;&lt;br /&gt;    set 'make-file-date-comparison-func func [spec] [&lt;br /&gt;        parse spec rules=&lt;br /&gt;        either =parse-end-mark [&lt;br /&gt;            func [file] reduce [=op  'get-modes 'file to lit-word! =attr  =date]&lt;br /&gt;        ] [none]&lt;br /&gt;    ]&lt;br /&gt;&lt;br /&gt;    set 'files-matching-date-spec func [&lt;br /&gt;        files [block!]&lt;br /&gt;        spec [block!]&lt;br /&gt;    ][&lt;br /&gt;        if match?: make-file-date-comparison-func spec [&lt;br /&gt;            collect 'keep [&lt;br /&gt;                foreach file files [if match? file [keep: file]]&lt;br /&gt;            ]&lt;br /&gt;        ]&lt;br /&gt;    ]&lt;br /&gt;]&lt;br /&gt;;foreach file files-matching-date-spec read %. [accessed after 1-jan-2006] [print [file modified? file]]&lt;br /&gt;;foreach file files-matching-date-spec read %. [date &lt; 1-jan-2006] [print [file modified? file]]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 23 May 2006 21:42:36 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2090</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
    <item>
      <title>parse-replace - parse-based replace; faster; doesn't change series in place</title>
      <link>http://snippets.dzone.com/posts/show/2049</link>
      <description>&lt;code&gt;&lt;br /&gt;    parse-replace: func [&lt;br /&gt;        {Replaces the search value with the replace value within the target series.}&lt;br /&gt;        target [series!] "Series that is being modified."&lt;br /&gt;        search "Value to be replaced."&lt;br /&gt;        replace "Value to replace with."&lt;br /&gt;        /all "Replace all occurrences."&lt;br /&gt;        /case "Case-sensitive replacement."&lt;br /&gt;        /local len data rule text&lt;br /&gt;    ][&lt;br /&gt;        len: length? search&lt;br /&gt;        collect/into data [&lt;br /&gt;            rule: [&lt;br /&gt;                [copy text to search (if text [data: join text replace]) len skip]&lt;br /&gt;                copy text to end (if text [data: text])&lt;br /&gt;            ]&lt;br /&gt;            if all [insert rule 'any]&lt;br /&gt;            either case [parse/all/case target rule] [parse/all target rule]&lt;br /&gt;        ] copy make target length? target&lt;br /&gt;    ]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 17 May 2006 01:37:48 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2049</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
    <item>
      <title>collect function</title>
      <link>http://snippets.dzone.com/posts/show/1130</link>
      <description>&lt;code&gt;&lt;br /&gt;    ; What about extending this to work on string values?&lt;br /&gt;    collect: func [  ; a.k.a. gather ?&lt;br /&gt;        [throw]&lt;br /&gt;        {Collects block evaluations.}&lt;br /&gt;        'word "Word to collect (as a set-word! in the block)"&lt;br /&gt;        block [any-block!] "Block to evaluate"&lt;br /&gt;        /into dest [series!] "Where to append results"&lt;br /&gt;        /only "Insert series results as series"&lt;br /&gt;        ;/debug&lt;br /&gt;        /local code marker at-marker? marker* mark replace-marker rules&lt;br /&gt;    ] [&lt;br /&gt;        block: copy/deep block&lt;br /&gt;        dest: any [dest make block! []]&lt;br /&gt;        ; "not only" forces the result to logic!, for use with PICK.&lt;br /&gt;        ; insert+tail pays off here over append.&lt;br /&gt;        ;code: reduce [pick [insert insert/only] not only 'tail 'dest]&lt;br /&gt;        ; FIRST BACK allows pass-thru assignment of value. Speed hit though.&lt;br /&gt;        ;code: reduce ['first 'back pick [insert insert/only] not only 'tail 'dest]&lt;br /&gt;        code: compose [first back (pick [insert insert/only] not only) tail dest]&lt;br /&gt;        marker: to set-word! word&lt;br /&gt;        at-marker?: does [mark/1 = marker]&lt;br /&gt;        ; We have to use change/part since we want to replace only one&lt;br /&gt;        ; item (the marker), but our code is more than one item long.&lt;br /&gt;        replace-marker: does [change/part mark code 1]&lt;br /&gt;        ;if debug [probe code probe marker]&lt;br /&gt;        marker*: [mark: set-word! (if at-marker? [replace-marker])]&lt;br /&gt;        parse block rules: [any [marker* | into rules | skip]]&lt;br /&gt;        ;if debug [probe block]&lt;br /&gt;        do block&lt;br /&gt;        head :dest&lt;br /&gt;    ]&lt;br /&gt;&lt;br /&gt;    comment {&lt;br /&gt;        ;collect/debug zz [repeat n 10 [zz: n * 100]]&lt;br /&gt;        collect zz []&lt;br /&gt;        collect zz [repeat i 10 [if (zz: i) &gt;= 3 [break]]]&lt;br /&gt;        collect zz [repeat i 10 [zz: i  if i &gt;= 3 [break]]]&lt;br /&gt;        collect zz [repeat i 10 [either i &lt;= 3 [zz: i][break]]]&lt;br /&gt;        dest: copy []&lt;br /&gt;        collect/into zz [repeat n 10 [zz: n * 100]] dest&lt;br /&gt;        collect zz [for i 1 10 2 [zz: i * 10]]&lt;br /&gt;        collect zz [for x 1 10 1 [zz: x]]&lt;br /&gt;        collect zz [foreach [a b] [1 2 3 4] [zz: a + b]]&lt;br /&gt;        collect zz [foreach w [a b c d] [zz: w]]&lt;br /&gt;        collect zz [repeat e [a b c %.txt] [zz: file? e]]&lt;br /&gt;        iota: func [n [integer!]][collect zz [repeat i n [zz: i]]]&lt;br /&gt;        iota 10&lt;br /&gt;        collect zz [foreach x first system [zz: to-set-word x]]&lt;br /&gt;        x: first system&lt;br /&gt;        collect zz [forall x [zz: length? x]]&lt;br /&gt;        x: first system&lt;br /&gt;        collect zz [forskip x 2 [zz: length? x]]&lt;br /&gt;        collect zz [forskip x 2 [zz: (length? x) / 0]]&lt;br /&gt;        collect/only zz [foreach [a b] [1 2 3 4] [zz: a zz: b zz: reduce [a b a + b]]]&lt;br /&gt;        collect/only zz [&lt;br /&gt;            foreach [a b] [1 2 3 4] [&lt;br /&gt;                zz: a zz: b zz: reduce [a b a + b]&lt;br /&gt;                foreach n reduce [a b a + b] [zz: n * 10]&lt;br /&gt;            ]&lt;br /&gt;        ]&lt;br /&gt;&lt;br /&gt;        dest: copy ""&lt;br /&gt;        collect/into zz [repeat n 10 [zz: n * 100 zz: " "]] dest&lt;br /&gt;&lt;br /&gt;        dest: copy []&lt;br /&gt;        collect/into zz [&lt;br /&gt;            foreach [num blk] [1 [a b c] 2 [d e f] 3 [g h i]] [&lt;br /&gt;                zz: num&lt;br /&gt;                collect/only/into yy [&lt;br /&gt;                    zz: blk&lt;br /&gt;                    foreach word blk [zz: yy: num  yy: word]&lt;br /&gt;                    yy: blk&lt;br /&gt;                ] dest&lt;br /&gt;            ]&lt;br /&gt;        ] dest&lt;br /&gt;&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 10 Jan 2006 06:25:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1130</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
  </channel>
</rss>
