[REBOL] Re: [Values] Penultimate Coerce
From: atruter:labyrinth:au at: 27-Nov-2003 10:20
Hi Andrew,
> Penultimate: func [
> "Returns the next-to-last value of a series."
> Series [series! port! tuple!]] [
> pick tail Series -2
> ]
Neat. Certainly better than mine ;)
second-last: func [series] [
pick series (length? series) - 1
]
> Coerce: func [
> "Forces a value to be within limits."
> Low_Limit [number! pair! char! money! date! time! tuple! series!]
> "Lower
> limit for value."
> Value [any-type!] "The value to limit."
> High_Limit [number! pair! char! money! date! time! tuple! series!]
> "Upper limit for value."
> ] [
> if not number? Value [
> Value: Low_Limit
> ]
> Value: max Low_Limit Value
> Value: min Value High_Limit
> ]
What's with the Capitalized_And_Underscored word names? ;)
Apart from that, how about the following minor changes?
coerce: func [
"Forces a value to be within limits, or none if types do not match."
low-limit [number! pair! char! money! date! time! tuple!] "Lower limit
for value."
value [number! pair! char! money! date! time! tuple! none!] "The value to
limit."
high-limit [number! pair! char! money! date! time! tuple!] "Upper limit
for value."
][
either all [(type? low-limit) = type? value (type? high-limit) = type?
value] [
value: max low-limit value
value: min value high-limit
][
none
]
]
>> coerce 1-Jan-1980 23-Dec-1999 30-Dec-1990
== 30-Dec-1990
>> coerce $10 $30 $20
== $20.00
>> coerce 1 none 2
== none
Regards,
Ashley