MCPcopy Create free account
hub / github.com/couchbaselabs/cbgb / javascriptReduceStats

Function javascriptReduceStats

reduction.go:112–161  ·  view source on GitHub ↗
(call otto.FunctionCall)

Source from the content-addressed store, hash-verified

110}
111
112func javascriptReduceStats(call otto.FunctionCall) otto.Value {
113 rere, err := call.Argument(2).ToBoolean()
114 if err != nil {
115 return ottoMust(otto.ToValue(fmt.Sprintf("Error getting rere flag: %v", err)))
116 }
117
118 ob, err := call.Argument(1).Export()
119 if err != nil {
120 return ottoMust(otto.ToValue(fmt.Sprintf("Error getting stuff: %v", err)))
121 }
122 l, ok := ob.([]interface{})
123 if !ok {
124 return ottoMust(otto.ToValue(fmt.Sprintf("unhandled %v/%T", ob, ob)))
125 }
126
127 rv := statsResult{}
128
129 if len(l) == 0 {
130 return rv.toOtto()
131 }
132
133 if rere {
134 // Rereduction goes here.
135 rv.load(l[0])
136 for i := 1; i < len(l); i++ {
137 ob := statsResult{}
138 ob.load(l[i])
139 rv.Add(ob)
140 }
141 return rv.toOtto()
142 }
143
144 // Initial reduction
145 rv.count = 1
146 rv.sum = zeroate(l[0])
147 rv.min = rv.sum
148 rv.max = rv.sum
149 rv.sumsqr = rv.sum * rv.sum
150
151 for i := 1; i < len(l); i++ {
152 v := zeroate(l[i])
153 rv.count++
154 rv.sum += v
155 rv.min = math.Min(rv.min, v)
156 rv.max = math.Max(rv.max, v)
157 rv.sumsqr += (v * v)
158 }
159
160 return rv.toOtto()
161}

Callers

nothing calls this directly

Calls 5

toOttoMethod · 0.95
loadMethod · 0.95
AddMethod · 0.95
ottoMustFunction · 0.85
zeroateFunction · 0.85

Tested by

no test coverage detected