Skip to content

Commit

Permalink
Support AVG (average) function.
Browse files Browse the repository at this point in the history
closes #157
  • Loading branch information
flanker authored and rubysolo committed Jan 23, 2018
1 parent 36cd6ab commit 4869b10
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log

## [HEAD] Unreleased
- Add `COUNT` and `AVG` functions

## [v3.1.0] 2017-01-10
- allow decimals with no leading zero
- nested hash and array support in bulk expression solver
Expand Down Expand Up @@ -151,6 +154,7 @@
## [v0.1.0] 2012-01-20
- initial release

[HEAD]: https://github.com/rubysolo/dentaku/compare/v3.1.0...HEAD
[v3.1.0]: https://github.com/rubysolo/dentaku/compare/v3.0.0...v3.1.0
[v3.0.0]: https://github.com/rubysolo/dentaku/compare/v2.0.11...v3.0.0
[v2.0.11]: https://github.com/rubysolo/dentaku/compare/v2.0.10...v2.0.11
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ Comparison: `<`, `>`, `<=`, `>=`, `<>`, `!=`, `=`,

Logic: `IF`, `AND`, `OR`, `NOT`, `SWITCH`

Numeric: `MIN`, `MAX`, `SUM`, `ROUND`, `ROUNDDOWN`, `ROUNDUP`
Numeric: `MIN`, `MAX`, `SUM`, `AVG`, `COUNT`, `ROUND`, `ROUNDDOWN`, `ROUNDUP`

Selections: `CASE` (syntax see [spec](https://github.com/rubysolo/dentaku/blob/master/spec/calculator_spec.rb#L292))

Expand Down
13 changes: 13 additions & 0 deletions lib/dentaku/ast/functions/avg.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require_relative '../function'

Dentaku::AST::Function.register(:avg, :numeric, ->(*args) {
if args.empty?
raise Dentaku::ArgumentError.for(
:too_few_arguments,
function_name: 'AVG()', at_least: 1, given: 0
), 'AVG() requires at least one argument'
end

flatten_args = args.flatten
flatten_args.map { |arg| Dentaku::AST::Function.numeric(arg) }.reduce(0, :+) / flatten_args.length
})
33 changes: 33 additions & 0 deletions spec/ast/avg_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'spec_helper'
require 'dentaku/ast/functions/avg'
require 'dentaku'

describe 'Dentaku::AST::Function::Avg' do
it 'returns the average of an array of Numeric values' do
result = Dentaku('AVG(1, x, 1.8)', x: 2.3)
expect(result).to eq 1.7
end

it 'returns the average of a single entry array of a Numeric value' do
result = Dentaku('AVG(x)', x: 2.3)
expect(result).to eq 2.3
end

it 'returns the average even if a String is passed' do
result = Dentaku('AVG(1, x, 1.8)', x: '2.3')
expect(result).to eq 1.7
end

it 'returns the average even if an array is passed' do
result = Dentaku('AVG(1, x, 2.3)', x: [4, 5])
expect(result).to eq 3.075
end

context 'checking errors' do
let(:calculator) { Dentaku::Calculator.new }

it 'raises an error if no arguments are passed' do
expect { calculator.evaluate!('AVG()') }.to raise_error(ArgumentError)
end
end
end

0 comments on commit 4869b10

Please sign in to comment.