| 694 | } |
| 695 | |
| 696 | SIValue AR_REDUCE |
| 697 | ( |
| 698 | SIValue *argv, |
| 699 | int argc, |
| 700 | void *private_data |
| 701 | ) { |
| 702 | // reduce(sum = 0, n IN [1,2,3] | sum + n) |
| 703 | // argv[0] - accumulator initial value |
| 704 | // argv[1] - array |
| 705 | // argv[2] - input record |
| 706 | |
| 707 | // return NULL if expected array is NULL |
| 708 | if(SI_TYPE(argv[1]) == T_NULL) return SI_NullVal(); |
| 709 | |
| 710 | // set arguments |
| 711 | SIValue accum = SI_ShareValue(argv[0]); |
| 712 | SIValue list = argv[1]; |
| 713 | Record rec = argv[2].ptrval; |
| 714 | ListReduceCtx *ctx = private_data; |
| 715 | |
| 716 | // on first invocation build the internal record |
| 717 | if(ctx->record == NULL) _PopulateReduceCtx(ctx, rec); |
| 718 | Record r = ctx->record; |
| 719 | |
| 720 | // populate record with the contents of the input record |
| 721 | Record_Clone(rec, r); |
| 722 | |
| 723 | // init accumulator within internal record |
| 724 | Record_AddScalar(r, ctx->accumulator_idx, accum); |
| 725 | |
| 726 | // evaluate expression for each list element |
| 727 | // e.g. for `n` in `list`, compute: sum = sum + n |
| 728 | uint len = SIArray_Length(list); |
| 729 | for(uint i = 0; i < len; i++) { |
| 730 | // retrieve the current element |
| 731 | SIValue elem = SIArray_Get(list, i); |
| 732 | |
| 733 | // set current element to the record |
| 734 | Record_AddScalar(r, ctx->variable_idx, elem); |
| 735 | // compute sum = sum + i |
| 736 | SIValue new_accum = AR_EXP_Evaluate_NoThrow(ctx->exp, r); |
| 737 | SIValue_Free(accum); |
| 738 | accum = new_accum; |
| 739 | // update accumulator within internal record |
| 740 | Record_AddScalar(r, ctx->accumulator_idx, accum); |
| 741 | } |
| 742 | |
| 743 | // clear internal record |
| 744 | Record_Remove(r, ctx->variable_idx); |
| 745 | Record_Remove(r, ctx->accumulator_idx); |
| 746 | |
| 747 | SIValue_Persist(&accum); |
| 748 | return accum; |
| 749 | } |
| 750 | |
| 751 | void Register_ListFuncs() { |
| 752 | SIType *types; |
nothing calls this directly
no test coverage detected