Skip to content
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

Shayan/feq 275/fix reduce of empty array with no initial value #9160

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions packages/bot-skeleton/src/scratch/blocks/Math/math_on_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ Blockly.JavaScript.math_on_list = block => {
const functionName = Blockly.JavaScript.provideFunction_('mathMean', [
`function ${Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_}(myList) {
var final_list = [];
return recursiveList(myList, final_list).reduce(function(x, y) {
return x + y;
});
return recursiveList(myList, final_list).reduce(function(x, y) {
return x + y;
},0);
}`,
]);
list = Blockly.JavaScript.valueToCode(block, 'LIST', Blockly.JavaScript.ORDER_NONE) || '[]';
Expand All @@ -108,9 +108,9 @@ Blockly.JavaScript.math_on_list = block => {
const functionName = Blockly.JavaScript.provideFunction_('mathMean', [
`function ${Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_}(myList) {
var final_list = [];
return recursiveList(myList, final_list).reduce(function(x, y) {
return x + y;
}) / myList.length;
return recursiveList(myList, final_list).reduce(function(x, y) {
return x + y;
}, 0) / myList.length;
}`,
]);

Expand All @@ -128,7 +128,7 @@ Blockly.JavaScript.math_on_list = block => {

function partition(arr, start, end){
var pivotValue = arr[end];
var pivotIndex = start;
var pivotIndex = start;
for (var i = start; i < end; i++) {
if (arr[i] < pivotValue) {
arr.swap(pivotIndex, i);
Expand All @@ -138,12 +138,12 @@ Blockly.JavaScript.math_on_list = block => {
arr.swap(end, pivotIndex);
return pivotIndex;
};

function quickSort(arr) {
var stack = [];
stack.push(0);
stack.push(arr.length - 1);

while(stack[stack.length - 1] >= 0){
end = stack.pop();
start = stack.pop();
Expand All @@ -165,13 +165,13 @@ Blockly.JavaScript.math_on_list = block => {

if (final_list.length % 2 == 0) {
return (final_list[final_list.length / 2 - 1] + final_list[final_list.length / 2]) / 2;
}
}
return final_list[(final_list.length - 1) / 2];
}

function ${Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_}(myList) {
var final_list = [];
return calculateMedian(recursiveList(myList, final_list));
return calculateMedian(recursiveList(myList, final_list));
}`,
]);

Expand Down Expand Up @@ -204,7 +204,7 @@ Blockly.JavaScript.math_on_list = block => {
}
maxCount = Math.max(thisCount, maxCount);
}

for (var j = 0; j < counts.length; j++) {
if (counts[j][1] == maxCount) {
modes.push(counts[j][0]);
Expand All @@ -230,7 +230,7 @@ Blockly.JavaScript.math_on_list = block => {
var counts = [];
var minCount = 1;
var countArray = [];

for (var i = 0; i < values.length; i++) {
var value = values[i];
var found = false;
Expand All @@ -241,9 +241,9 @@ Blockly.JavaScript.math_on_list = block => {
thisCount = ++counts[j][1];
found = true;
break;
}
}
}

if (!found) {
counts.push([value, 1]);
thisCount = 1;
Expand All @@ -262,7 +262,7 @@ Blockly.JavaScript.math_on_list = block => {

return antiMode;
}

function ${Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_}(list) {
var final_list = [];
return calculateMathAntiMode(recursiveList(list, final_list));
Expand All @@ -282,7 +282,7 @@ Blockly.JavaScript.math_on_list = block => {

var mean = numbers.reduce(function(x, y) {
return x + y;
}) / n;
}, 0) / n;

var variance = 0;
for (var j = 0; j < n; j++) {
Expand All @@ -294,7 +294,7 @@ Blockly.JavaScript.math_on_list = block => {

function ${Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_}(list) {
var final_list = [];
return calculateMathStandardDeviation(recursiveList(list, final_list));
return calculateMathStandardDeviation(recursiveList(list, final_list));
}`,
]);

Expand Down
19 changes: 17 additions & 2 deletions packages/bot-skeleton/src/scratch/hooks/workspace_svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,15 @@ Blockly.WorkspaceSvg.prototype.cleanUp = function (x = 0, y = 0, blocks_to_clean
block.moveBy(cursor_x, cursor_y);
} else {
const start = (column_index - 1) * blocks_per_column;
const initialValue = {
getHeightWidth: () => ({
width: 0,
}),
};

const fat_neighbour_block = root_blocks
.slice(start, start + blocks_per_column)
?.reduce((a, b) => (a.getHeightWidth().width > b.getHeightWidth().width ? a : b));
?.reduce((a, b) => (a.getHeightWidth().width > b.getHeightWidth().width ? a : b), initialValue);

let position_x = cursor_x + fat_neighbour_block.getHeightWidth().width + Blockly.BlockSvg.MIN_BLOCK_X;
if (!is_import) {
Expand All @@ -162,11 +168,20 @@ Blockly.WorkspaceSvg.prototype.cleanUp = function (x = 0, y = 0, blocks_to_clean
block.getRelativeToSurfaceXY().y + block.getHeightWidth().height + Blockly.BlockSvg.MIN_BLOCK_Y;
});

const initialValue = {
getRelativeToSurfaceXY: () => ({
y: 0,
}),
getHeightWidth: () => ({
height: 0,
}),
};

const lowest_root_block = root_blocks.reduce((a, b) => {
const a_metrics = a.getRelativeToSurfaceXY().y + a.getHeightWidth().height;
const b_metrics = b.getRelativeToSurfaceXY().y + b.getHeightWidth().height;
return a_metrics > b_metrics ? a : b;
});
}, initialValue);

original_cursor_y =
lowest_root_block.getRelativeToSurfaceXY().y +
Expand Down
2 changes: 1 addition & 1 deletion packages/indicators/src/utils/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export const takeField = (arr, field) => arr.map(x => (field ? x[field] : x));

export const takeLast = (arr, n, field) => takeField(arr.slice(n > arr.length ? 0 : arr.length - n, arr.length), field);

export const sum = data => data.reduce((acc, x) => acc + x);
export const sum = data => data.reduce((acc, x) => acc + x, 0);

export const mean = data => data.reduce((a, b) => a + b, 0) / data.length;

Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/utils/string/string_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const compareBigUnsignedInt = (a: number, b: number | string | undefined)

export const matchStringByChar = (s: string, p: string) => {
if (p?.length < 1) return true;
const z = p.split('').reduce((a, b) => `${a}[^${b}]*${b}`);
const z = p.split('').reduce((a, b) => `${a}[^${b}]*${b}`, '');
return RegExp(z, 'i').test(s);
};

Expand Down