-
Notifications
You must be signed in to change notification settings - Fork 411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FEAT: Added SUM and AVERAGE functions #3498
Conversation
As often happens with me, as soon as something "gets real", my mind starts to think of more details. These are probably good as is, though I'm thinking of how spreadsheets work (don't think we want to do that in a basic func, as they coerce convertable strings), and the exact doc string wording. I'll sleep on it. |
Forcing float in |
|
The easy cases aren't an issue, but doc'ing things beyond "results are undefined if you mix numeric types in the block." gets tricky, and maybe leads to other design questions.
e.g. do we add refinements for control, or use HOF filter-style funcs in combination? |
Note that >> append inlay/head [1 2 3] 'add 0 ; problem is that you need a 'symmetry' in the form of 0
== [add 1 add 2 add 3 0]
>> do inlay collect [repeat i 5 [keep i]] '* ; factorial of 5
>> 120 You can insert an arbitrary block of data that way: >> inlay/head [1 2 3] [append something 1 +]
== [append something 1 + 1 append something 1 + 2 ...] Or cycle through it and insert each element periodically: >> inlay/thru [1 2 3] [+ -]
== [1 + 2 - 3]
>> x: [1 2 3]
== 6
>> length? x
== 3
>> sum x
== 6
>> divide sum x length? x ; mean
== 2
>> combine [1 2 3] [sum length?] ; apply each function separately and collect the results together
== [6 3]
>> do inlay combine [1 2 3] [sum length?] '/
== 2
>> apply combine [1 2 3] [sum length?] :divide
== 2
>> block: inlay/head/thru append/dup/only [] [1 2 3] 2 [[divide sum] length?]
== [divide sum [1 2 3] length? [1 2 3]]
>> do block
== 2 In short, I propose to think in terms of HOF and clever combinators here, instead of introducing ad hoc solutions. Another question is why we should restrict ourselves to |
The fold/accumulator approach is an option, but I'd like to keep these simple for now. As long as we get the spec right, we can change the body later. The main thing for me is the behavior. |
@9214 I agree that @greggirwin I don't think we should I propose the following version,
which also supports
What do you think? If that is good, I'll update my PR. |
For mixing numeric types in block; |
Looks good to me @endo64. Update the PR, let's get it in there, and solicit feedback. |
Updated the PR |
@endo64, feel free to announce the availability of these funcs on Gitter. |
is slightly more efficient And
would accept some series with mixed types summing summable things only, but is about twice slower. |
👍 to @toomasv and average: func [
"Returns the average of all values in a block" ;@@ block..?
block [block! vector! paren! hash!] ;@@ but it's not ONLY a block
][
unless empty? block [
divide sum block .0 + length? block
]
] |
Let's focus on behavior before optimizations. @9214, I'm not keen on that trick for casting to a float, as it doesn't show the intent. I also think |
@greggirwin okay, |
@9214 you will find I'm not a fan of |
|
I guess as long as suggestion is functionally equivalent to PR it will be rejected. I have nothing else to say then, except for: >> any-list!
== make typeset! [block! paren! hash!] |
Clear improvements are welcome, but these funcs are so simple that it's hard to make more than lateral moves. |
I agree, that probably the usual case would be summing/averaging some kind of number-blocks, and so the |
@toomasv |
@toomasv, @9214 Thanks for the inputs, as @greggirwin said current My first version was also using |
One additional note about @toomasv's version (#3498 (comment)), that I prefer |
Yes, this is the bigger picture question of how spreadsheets and DB aggregates behave, and taking those into account. e.g. if you're using these funcs to build tools like that, we have to manage expectations if we do something different. |
Added SUM and AVERAGE functions.
See also Design notes of SUM function