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

World: r3wp

[!REBOL3-OLD1]

BrianH
31-Dec-2009
[20537]
Suggest them in CureCode as wishes. Or write them up as mezz or extension 
functions - those are easy to adapt.
Paul
31-Dec-2009
[20538]
will do when I get the chance.
Gregg
31-Dec-2009
[20539]
I'm all for it. I've suggested them in the past, but didn't get much 
traction. Here's a non-recursive starting point.

    ; Should our seed value be 0.0 to coerce to decimal? If they
    ; include a decimal value before any overflow, it will be OK.
    ; Changed to match the first type in the block.
    sum: func [block [any-block!] /local result] [
        result: any [
            attempt [make pick block 1 0]
            attempt [0 * pick block 1]
            0
        ]
        foreach value reduce block [result: result + value]
        result
    ]
Paul
31-Dec-2009
[20540x2]
I just added the wish request.
Why the coersion to decimal?  Just curious.
BrianH
31-Dec-2009
[20542]
Should sum [] return 0 or none?
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
[20586]
An interesting example of that is AJOIN. The R3 version has less 
memory overhead, the R2 version in 2.7.7 is faster.