(i *interpreter, arguments []value)
| 2378 | } |
| 2379 | |
| 2380 | func builtinMaxArray(i *interpreter, arguments []value) (value, error) { |
| 2381 | arrv := arguments[0] |
| 2382 | keyFv := arguments[1] |
| 2383 | onEmpty := arguments[2] |
| 2384 | |
| 2385 | arr, err := i.getArray(arrv) |
| 2386 | if err != nil { |
| 2387 | return nil, err |
| 2388 | } |
| 2389 | keyF, err := i.getFunction(keyFv) |
| 2390 | if err != nil { |
| 2391 | return nil, err |
| 2392 | } |
| 2393 | num := arr.length() |
| 2394 | if num == 0 { |
| 2395 | if onEmpty == nil { |
| 2396 | return nil, i.Error("Expected at least one element in array. Got none") |
| 2397 | } else { |
| 2398 | return onEmpty, nil |
| 2399 | } |
| 2400 | } |
| 2401 | maxVal, err := arr.elements[0].getValue(i) |
| 2402 | if err != nil { |
| 2403 | return nil, err |
| 2404 | } |
| 2405 | maxValKey, err := keyF.call(i, args(arr.elements[0])) |
| 2406 | if err != nil { |
| 2407 | return nil, err |
| 2408 | } |
| 2409 | for index := 1; index < num; index++ { |
| 2410 | current, err := arr.elements[index].getValue(i) |
| 2411 | if err != nil { |
| 2412 | return nil, err |
| 2413 | } |
| 2414 | currentKey, err := keyF.call(i, args(arr.elements[index])) |
| 2415 | if err != nil { |
| 2416 | return nil, err |
| 2417 | } |
| 2418 | cmp, err := valueCmp(i, maxValKey, currentKey) |
| 2419 | if err != nil { |
| 2420 | return nil, err |
| 2421 | } |
| 2422 | if cmp < 0 { |
| 2423 | maxVal = current |
| 2424 | maxValKey = currentKey |
| 2425 | } |
| 2426 | } |
| 2427 | return maxVal, nil |
| 2428 | } |
| 2429 | |
| 2430 | func builtinNative(i *interpreter, name value) (value, error) { |
| 2431 | str, err := i.getString(name) |
nothing calls this directly
no test coverage detected
searching dependent graphs…