[REBOL] REP: Introduce new functions for finding minimum and maximum of a serie
From: petr::krenzelok::trz::cz at: 26-Dec-2000 10:51
I was asked yesterday to handle some kind of data, and the part of the task was to find
minimum and maximum of a series. To my surprise, current Rebol state showed as ineffective.
Rebol currently offers us two functions, 'min and 'max. Let's look at the source:
>> source min
min: native [
"Returns the lesser of the two values."
value1 [number! pair! char! money! date! time! tuple! series!]
value2 [number! pair! char! money! date! time! tuple! series!]
]
>>
What really surprised me, is a level in which the functionality is solved - native. In
my opinion, for returning lesser (or greater in case of 'max) of two values given, some
one line mezzanine function would be sufficient (min: func [arg1 arg2][either arg1 <
arg2 [arg1][arg2]]).
I asked about the easiest way of how to find minimal or maximal value of a series, and
was given an answer to use 'sort. Ladislav Mecir suggested me to use iteration though,
and I would never believe iteration could be faster than native 'sort. Let's look in
a real-life examples:
print "Finding min and max values using 'sort ..."
start: now/time
sort/skip tmpx: copy img-data 4
end: now/time
print end - start
print "Finding min and max values using iteration ..."
start: now/time
min: max: copy/part img-data 4
repeat count (length? img-data) / 4 [
if (tmp: copy/part img-data 4) < min [min: tmp]
if tmp > max [max: tmp]
img-data: skip img-data 4
]
end: now/time
print end - start
results:
sort: 19 sec.
iter: 13 sec.
even using smaller amount of data, iteration was faster (4 sec sort vs. 3 sec iter)
Lack of support of such relatively primitive functionality led me to conclusion to submit
this proposal:
Introduce new serie of functions: 'minimum and 'maximum. The best way would be to replace
current 'min and 'max, as they are merely useless, but to prevent some scripts from crash
upon new functionality, we can live with new names ...
MINIMUM
Usage
MINIMUM series
Description
Returns minimal value or set of values from a series
Arguments
series - series to find minimal value or set of values in
Refinements
/skip - skip values to treat series as a set of records
/count - returns block of minimal value found, and number of value occurances in series
/several - amount of minimal values to return
num - number of minimal values to return
Conclusion:
I am not sure if all the refinements are easy to implement, but we should support at
least /skip, although /several could be usefull for some fuzzy purposes, and /count for
other yet unspecified purposes. If Rebol can't be faster in it's nature, we should help
ourselves by introduction of new natives, or improving existing ones, if a solution doesn't
introduce significant overhead to the language ....
Comments welcomed ...
Cheers,
-pekr-