Add a block to reverse a list (#844)

This commit is contained in:
Tim Dawborn
2017-01-22 05:48:42 +11:00
committed by Andrew n marshall
parent f6168e1364
commit 46316c7cea
12 changed files with 240 additions and 1 deletions

View File

@@ -363,3 +363,20 @@ Blockly.Lua['lists_split'] = function(block) {
var code = functionName + '(' + input + ', ' + delimiter + ')';
return [code, Blockly.Lua.ORDER_HIGH];
};
Blockly.Lua['lists_reverse'] = function(block) {
// Block for reversing a list.
var list = Blockly.Lua.valueToCode(block, 'LIST',
Blockly.Lua.ORDER_NONE) || '{}';
var functionName = Blockly.Lua.provideFunction_(
'list_reverse',
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(input)',
' local reversed = {}',
' for i = #input, 1, -1 do',
' table.insert(reversed, input[i])',
' end',
' return reversed',
'end']);
var code = 'list_reverse(' + list + ')';
return [code, Blockly.Lua.ORDER_HIGH];
};