r3wp [groups: 83 posts: 189283]
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r3wp

[!REBOL3-OLD1]

Steeve
31-Dec-2009
[20543]
well, if only the default math operators could accept series of scalars 
by default, it would be incredibly simple and powerful.
It should not be so hard to have that behaviour nativly...
A + [1 2 3 4] == A + 1 + 2 + 3 + 4
BrianH
31-Dec-2009
[20544]
Vector math has already been suggested.
Steeve
31-Dec-2009
[20545]
yep, but we are always waiting
Gregg
31-Dec-2009
[20546x2]
Coercion to decimal would be to prevetn overflow. I prefer not to 
do that though.
Zero versus none is a good question Brian. I have SUM return zero, 
but my AVG func returns none.
Steeve
31-Dec-2009
[20548]
0 seems better, it prevents throwing useless errors
(my old claim)
BrianH
31-Dec-2009
[20549x2]
We might not need to coerce to decimal. The first decimal in the 
list will make that coersion for us. And we don't want decimal results 
for a list of integers - it's a loss of precision. I suspect that 
initializing result with the integer 0 will be sufficient.
Then any decimal or money in the list will promote.
Gregg
31-Dec-2009
[20551]
Agreed. The only issue is if you don't get a decimal value before 
an overflow occurs, but that's just something to doc.
BrianH
31-Dec-2009
[20552]
Actually, documenting that this function doesn't do any error handling 
beyond the arguments of + would be good.
Gregg
31-Dec-2009
[20553]
Agreed.
Paul
31-Dec-2009
[20554]
Sounds like the function is moving along.  Great thing is that if 
you build the one you have what you need for the product function 
as well.
BrianH
31-Dec-2009
[20555x2]
sum: func [block [block! vector!] /local result] [
    result: 0
    foreach value reduce block [result: result + :value]
    result
]
Some error handing, some speedups, and vector support.
Steeve
31-Dec-2009
[20557x2]
no need to return the result at the end
foreach do that
BrianH
31-Dec-2009
[20559]
Not for a empty block.
Steeve
31-Dec-2009
[20560]
right
BrianH
31-Dec-2009
[20561x2]
And it will just throw an error if the block contains anything not 
addable.
That's the R3 way - throw a useful error so the programmer can fix 
their code, no DWIM :)
Paul
31-Dec-2009
[20563x2]
why the :value?
Your already reducing the block.
BrianH
31-Dec-2009
[20565]
In case reducing the block makes a function or some other active 
value - no double eval. It's a way to trip bad errors quicker.
Paul
31-Dec-2009
[20566]
ok.
Steeve
31-Dec-2009
[20567x3]
uggly one-liner version.

sum: func[block [block!]][

 foreach [v1: v2] next head reduce/into block copy [0 0][v1/1: :v2 
 + v1/0]
]
-_-;
yeah !!!! i do not use any locals
...
Gregg
31-Dec-2009
[20570]
It uses a lambda local. :-)
Steeve
31-Dec-2009
[20571]
i should have used forall, less uggly :-)
BrianH
31-Dec-2009
[20572]
And faster. Reversing the order of arguments might not be a good 
idea though - some operators are more forgiving of their left value.
Steeve
31-Dec-2009
[20573]
i was not seriously doing a proposal :-)
BrianH
31-Dec-2009
[20574]
v1/1: v1/0 + :v2
Gregg
31-Dec-2009
[20575x2]
Yeah, I'm trying to remember (since I didn't comment it) why I coerced 
the result. Something in my brain says there was a good reason.
Maybe I'll remember if we write a test suite for it.
BrianH
31-Dec-2009
[20577]
We should make a whole module of math functions, with test code. 
Let the REBOL optimizer at it and then see what we can include.
Steeve
31-Dec-2009
[20578]
Who's that optimizer ?
BrianH
31-Dec-2009
[20579]
The "REBOL optimizer" is a running joke. The best way to optimize 
your REBOL code is to post it publicly in AltME or R3 chat and dare 
people to improve it. Then the community tries to one-up each other 
to improve it :)
Steeve
31-Dec-2009
[20580]
-_-;
BrianH
31-Dec-2009
[20581]
It's the best optimizer known to man :)
Steeve
31-Dec-2009
[20582]
sure :)
BrianH
31-Dec-2009
[20583x2]
For certain project domains, R3 interpreted code can be faster than 
compiled code, once it's been through the REBOL optimizer.
That happened with some REBOL-vs-Java code the other day here.
Steeve
31-Dec-2009
[20585]
but there is several criteria to optimize something.
- Best Speed
- Shortest code
- shortest memory overhead
- best ratio of above criteria
BrianH
31-Dec-2009
[20586x2]
An interesting example of that is AJOIN. The R3 version has less 
memory overhead, the R2 version in 2.7.7 is faster.
Oh, and that means that the mezzanine is faster than the native :)
Steeve
31-Dec-2009
[20588]
rare
BrianH
31-Dec-2009
[20589x2]
Not really, for code that plays to REBOL's strengths. It happens 
quite often.
Rare for the kind of thing you tend to do though (emulators, iirc).
Rebolek
1-Jan-2010
[20591]
throw/catch does not work in R3?
Carl
7-Jan-2010
[20592]
Testing.