I have a function that iterates through a block, tests each value against a given function and is placed in a block associated with the product of that function.
CATEGORIZE items test
categorize [ "Aardvark" "Bison" "Antelope" "Baboon" "Anaconda" "Basking Shark" ] [first item]
[ #"A" [ "Aardvark" "Antelope" "Anaconda" ] #"B" [ "Bison" "Baboon" "Basking Shark" ] ]
test argument, a
function! or a
block! is accepted—a
block! is transformed into a function with a single argument
new-line before returning the block neatens the resultant block—for aesthetic purposes only.
Review questions I have are:
Is there a more efficient way to evaluate each iteration step?
Are there any potential optimization possibilities for common comparisons? e.g. by first letter, etc.
This is supposed to be a general-purpose function—are there any glaring cases this function fails? Any enhancement suggestions?
Rebol [ Title: "Categorize" Date: 11-Mar-2014 Author: "Christopher Ross-Gill" Type: 'module Exports: [categorize] ] categorize: func [items [block!] test [any-function! block!] /local out value target][ out: copy  if block? :test [test: func [item] :test] foreach item items [ value: test item unless target: select out value [ repend out [value target: copy ] ] append/only target item ] foreach [value items] out [new-line/all items true] new-line/all/skip out true 2 ]