You could conceptually make the block, but only in JS/TS. The summation logic itself is fine; the limitation is with MakeCode Blocks.
In TypeScript, you can use a callback that returns a number:
//% block="Sum of $index|from $num|to $num2| with block input"
//% draggableParameters
export function Sumof(
num: number,
num2: number,
handler: (index: number) => number
): number {
let sum = 0
for (let index = num; index <= num2; index++) {
sum += handler(index)
}
return sum
}
You could then use it like this:
let result = Sumof(1, 5, function (index) {
return index * index
})
That calculates:
1² + 2² + 3² + 4² + 5² = 55
So this part of the original code is correct:
handler: (index: number) => number
The problem is that MakeCode Blocks cannot currently represent a callback that has a draggable variable like index and also returns a number that can be used as a reporter block.
That is why it either creates an empty callback:
Sumof(0, 0, function (index) {
})
or removes the callback completely when you put the block into a variable:
Sumof(0, 0)
You can still make a statement-style summation block in Blocks, but a true sigma block that takes an expression and returns the sum needs to be used in TypeScript, or redesigned for Blocks using something like a separate summation result variable.
I have tried doing this, but like I said, MakeCode’s block is just limited with the PXT.