mirror of
https://github.com/google/blockly.git
synced 2026-01-09 01:50:11 +01:00
Add a block to reverse a list (#844)
This commit is contained in:
committed by
Andrew n marshall
parent
f6168e1364
commit
46316c7cea
@@ -841,3 +841,27 @@ Blockly.Blocks['lists_split'] = {
|
||||
this.updateType_(xmlElement.getAttribute('mode'));
|
||||
}
|
||||
};
|
||||
|
||||
Blockly.Blocks['lists_reverse'] = {
|
||||
/**
|
||||
* Block for reversing a list.
|
||||
* @this Blockly.Block
|
||||
**/
|
||||
init: function() {
|
||||
this.jsonInit({
|
||||
"message0": Blockly.Msg.LISTS_REVERSE_MESSAGE0,
|
||||
"args0": [
|
||||
{
|
||||
"type": "input_value",
|
||||
"name": "LIST",
|
||||
"check": "Array"
|
||||
}
|
||||
],
|
||||
"output": "Array",
|
||||
"inputsInline": true,
|
||||
"colour": Blockly.Blocks.lists.HUE,
|
||||
"tooltip": Blockly.Msg.LISTS_REVERSE_TOOLTIP,
|
||||
"helpUrl": Blockly.Msg.LISTS_REVERSE_HELPURL
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -450,3 +450,12 @@ Blockly.Dart['lists_split'] = function(block) {
|
||||
var code = input + '.' + functionName + '(' + delimiter + ')';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart['lists_reverse'] = function(block) {
|
||||
// Block for reversing a list.
|
||||
var list = Blockly.Dart.valueToCode(block, 'LIST',
|
||||
Blockly.Dart.ORDER_NONE) || '[]';
|
||||
// XXX What should the operator precedence be for a `new`?
|
||||
var code = 'new List.from(' + list + '.reversed)';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
@@ -392,3 +392,11 @@ Blockly.JavaScript['lists_split'] = function(block) {
|
||||
var code = input + '.' + functionName + '(' + delimiter + ')';
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['lists_reverse'] = function(block) {
|
||||
// Block for reversing a list.
|
||||
var list = Blockly.JavaScript.valueToCode(block, 'LIST',
|
||||
Blockly.JavaScript.ORDER_FUNCTION_CALL) || '[]';
|
||||
var code = list + '.slice().reverse()';
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
@@ -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];
|
||||
};
|
||||
|
||||
@@ -502,3 +502,11 @@ Blockly.PHP['lists_split'] = function(block) {
|
||||
var code = functionName + '(' + value_delim + ', ' + value_input + ')';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.PHP['lists_reverse'] = function(block) {
|
||||
// Block for reversing a list.
|
||||
var list = Blockly.PHP.valueToCode(block, 'LIST',
|
||||
Blockly.PHP.ORDER_COMMA) || '[]';
|
||||
var code = 'array_reverse(' + list + ')';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
@@ -353,3 +353,11 @@ Blockly.Python['lists_split'] = function(block) {
|
||||
}
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.Python['lists_reverse'] = function(block) {
|
||||
// Block for reversing a list.
|
||||
var list = Blockly.Python.valueToCode(block, 'LIST',
|
||||
Blockly.Python.ORDER_NONE) || '[]';
|
||||
var code = 'list(reversed(' + list + '))';
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"@metadata": {
|
||||
"author": "Ellen Spertus <ellen.spertus@gmail.com>",
|
||||
"lastupdated": "2017-01-18 10:58:30.631169",
|
||||
"lastupdated": "2017-01-20 09:40:15.080443",
|
||||
"locale": "en",
|
||||
"messagedocumentation" : "qqq"
|
||||
},
|
||||
@@ -343,6 +343,9 @@
|
||||
"LISTS_SPLIT_WITH_DELIMITER": "with delimiter",
|
||||
"LISTS_SPLIT_TOOLTIP_SPLIT": "Split text into a list of texts, breaking at each delimiter.",
|
||||
"LISTS_SPLIT_TOOLTIP_JOIN": "Join a list of texts into one text, separated by a delimiter.",
|
||||
"LISTS_REVERSE_HELPURL": "",
|
||||
"LISTS_REVERSE_MESSAGE0": "reverse %1",
|
||||
"LISTS_REVERSE_TOOLTIP": "Reverse a copy of a list.",
|
||||
"ORDINAL_NUMBER_SUFFIX": "",
|
||||
"VARIABLES_GET_HELPURL": "https://github.com/google/blockly/wiki/Variables#get",
|
||||
"VARIABLES_GET_TOOLTIP": "Returns the value of this variable.",
|
||||
|
||||
@@ -337,6 +337,9 @@
|
||||
"LISTS_SPLIT_WITH_DELIMITER": "block text - Prompts for a letter to be used as a separator when splitting or joining text.",
|
||||
"LISTS_SPLIT_TOOLTIP_SPLIT": "tooltip - See [https://github.com/google/blockly/wiki/Lists#make-list-from-text https://github.com/google/blockly/wiki/Lists#make-list-from-text] for more information.",
|
||||
"LISTS_SPLIT_TOOLTIP_JOIN": "tooltip - See [https://github.com/google/blockly/wiki/Lists#make-text-from-list https://github.com/google/blockly/wiki/Lists#make-text-from-list] for more information.",
|
||||
"LISTS_REVERSE_HELPURL": "url - Information describing reversing a list.",
|
||||
"LISTS_REVERSE_MESSAGE0": "Reverse a list of items %1.",
|
||||
"LISTS_REVERSE_TOOLTIP": "tooltip - See [https://github.com/google/blockly/wiki/Lists#reversing-a-list].",
|
||||
"ORDINAL_NUMBER_SUFFIX": "grammar - Text that follows an ordinal number (a number that indicates position relative to other numbers). In most languages, such text appears before the number, so this should be blank. An exception is Hungarian. See [[Translating:Blockly#Ordinal_numbers]] for more information.",
|
||||
"VARIABLES_GET_HELPURL": "url - Information about ''variables'' in computer programming. Consider using your language's translation of [https://en.wikipedia.org/wiki/Variable_(computer_science) https://en.wikipedia.org/wiki/Variable_(computer_science)], if it exists.",
|
||||
"VARIABLES_GET_TOOLTIP": "tooltip - This gets the value of the named variable without modifying it.",
|
||||
|
||||
@@ -1039,6 +1039,13 @@ Blockly.Msg.LISTS_SPLIT_TOOLTIP_SPLIT = 'Split text into a list of texts, breaki
|
||||
/// https://github.com/google/blockly/wiki/Lists#make-text-from-list] for more information.
|
||||
Blockly.Msg.LISTS_SPLIT_TOOLTIP_JOIN = 'Join a list of texts into one text, separated by a delimiter.';
|
||||
|
||||
/// url - Information describing reversing a list.
|
||||
Blockly.Msg.LISTS_REVERSE_HELPURL = 'https://github.com/google/blockly/wiki/Lists#reversing-a-list';
|
||||
/// block text - Title of block that returns a copy of a list (%1) with the order of items reversed.
|
||||
Blockly.Msg.LISTS_REVERSE_MESSAGE0 = 'reverse %1';
|
||||
/// tooltip - Short description for a block that reverses a copy of a list.
|
||||
Blockly.Msg.LISTS_REVERSE_TOOLTIP = 'Reverse a copy of a list.';
|
||||
|
||||
/// grammar - Text that follows an ordinal number (a number that indicates
|
||||
/// position relative to other numbers). In most languages, such text appears
|
||||
/// before the number, so this should be blank. An exception is Hungarian.
|
||||
|
||||
@@ -296,6 +296,7 @@ h1 {
|
||||
<block type="lists_getSublist"></block>
|
||||
<block type="lists_sort"></block>
|
||||
<block type="lists_split"></block>
|
||||
<block type="lists_reverse"></block>
|
||||
</category>
|
||||
<category name="Colour" colour="20">
|
||||
<block type="colour_picker"></block>
|
||||
|
||||
@@ -54,6 +54,11 @@
|
||||
<next>
|
||||
<block type="procedures_callnoreturn">
|
||||
<mutation name="test sort numeric"></mutation>
|
||||
<next>
|
||||
<block type="procedures_callnoreturn">
|
||||
<mutation name="test reverse"></mutation>
|
||||
</block>
|
||||
</next>
|
||||
</block>
|
||||
</next>
|
||||
</block>
|
||||
@@ -8131,4 +8136,149 @@
|
||||
</block>
|
||||
</statement>
|
||||
</block>
|
||||
<block type="procedures_defnoreturn" x="13" y="20463">
|
||||
<field name="NAME">test reverse</field>
|
||||
<comment pinned="false" h="80" w="160">Tests the "list reverse" block.</comment>
|
||||
<statement name="STACK">
|
||||
<block type="variables_set">
|
||||
<field name="VAR">list</field>
|
||||
<value name="VALUE">
|
||||
<block type="lists_create_with" inline="true">
|
||||
<mutation items="4"></mutation>
|
||||
<value name="ADD0">
|
||||
<block type="math_number">
|
||||
<field name="NUM">8</field>
|
||||
</block>
|
||||
</value>
|
||||
<value name="ADD1">
|
||||
<block type="math_number">
|
||||
<field name="NUM">18</field>
|
||||
</block>
|
||||
</value>
|
||||
<value name="ADD2">
|
||||
<block type="math_number">
|
||||
<field name="NUM">-1</field>
|
||||
</block>
|
||||
</value>
|
||||
<value name="ADD3">
|
||||
<block type="math_number">
|
||||
<field name="NUM">64</field>
|
||||
</block>
|
||||
</value>
|
||||
</block>
|
||||
</value>
|
||||
<next>
|
||||
<block type="unittest_assertequals" inline="false">
|
||||
<value name="MESSAGE">
|
||||
<block type="text">
|
||||
<field name="TEXT">reverse a copy</field>
|
||||
</block>
|
||||
</value>
|
||||
<value name="ACTUAL">
|
||||
<block type="lists_reverse">
|
||||
<value name="LIST">
|
||||
<block type="variables_get">
|
||||
<field name="VAR">list</field>
|
||||
</block>
|
||||
</value>
|
||||
</block>
|
||||
</value>
|
||||
<value name="EXPECTED">
|
||||
<block type="lists_create_with" inline="true">
|
||||
<mutation items="4"></mutation>
|
||||
<value name="ADD0">
|
||||
<block type="math_number">
|
||||
<field name="NUM">64</field>
|
||||
</block>
|
||||
</value>
|
||||
<value name="ADD1">
|
||||
<block type="math_number">
|
||||
<field name="NUM">-1</field>
|
||||
</block>
|
||||
</value>
|
||||
<value name="ADD2">
|
||||
<block type="math_number">
|
||||
<field name="NUM">18</field>
|
||||
</block>
|
||||
</value>
|
||||
<value name="ADD3">
|
||||
<block type="math_number">
|
||||
<field name="NUM">8</field>
|
||||
</block>
|
||||
</value>
|
||||
</block>
|
||||
</value>
|
||||
<next>
|
||||
<block type="unittest_assertequals" inline="false">
|
||||
<value name="MESSAGE">
|
||||
<block type="text">
|
||||
<field name="TEXT">reverse a copy original</field>
|
||||
</block>
|
||||
</value>
|
||||
<value name="ACTUAL">
|
||||
<block type="variables_get">
|
||||
<field name="VAR">list</field>
|
||||
</block>
|
||||
</value>
|
||||
<value name="EXPECTED">
|
||||
<block type="lists_create_with" inline="true">
|
||||
<mutation items="4"></mutation>
|
||||
<value name="ADD0">
|
||||
<block type="math_number">
|
||||
<field name="NUM">8</field>
|
||||
</block>
|
||||
</value>
|
||||
<value name="ADD1">
|
||||
<block type="math_number">
|
||||
<field name="NUM">18</field>
|
||||
</block>
|
||||
</value>
|
||||
<value name="ADD2">
|
||||
<block type="math_number">
|
||||
<field name="NUM">-1</field>
|
||||
</block>
|
||||
</value>
|
||||
<value name="ADD3">
|
||||
<block type="math_number">
|
||||
<field name="NUM">64</field>
|
||||
</block>
|
||||
</value>
|
||||
</block>
|
||||
</value>
|
||||
<next>
|
||||
<block type="variables_set">
|
||||
<field name="VAR">list</field>
|
||||
<value name="VALUE">
|
||||
<block type="lists_create_empty"></block>
|
||||
</value>
|
||||
<next>
|
||||
<block type="unittest_assertequals" inline="false">
|
||||
<value name="MESSAGE">
|
||||
<block type="text">
|
||||
<field name="TEXT">empty list</field>
|
||||
</block>
|
||||
</value>
|
||||
<value name="ACTUAL">
|
||||
<block type="lists_reverse">
|
||||
<value name="LIST">
|
||||
<block type="variables_get">
|
||||
<field name="VAR">list</field>
|
||||
</block>
|
||||
</value>
|
||||
</block>
|
||||
</value>
|
||||
<value name="EXPECTED">
|
||||
<block type="lists_create_empty"></block>
|
||||
</value>
|
||||
</block>
|
||||
</next>
|
||||
</block>
|
||||
</next>
|
||||
</block>
|
||||
</next>
|
||||
</block>
|
||||
</next>
|
||||
</block>
|
||||
</statement>
|
||||
</block>
|
||||
</xml>
|
||||
@@ -681,6 +681,7 @@ h1 {
|
||||
</value>
|
||||
</block>
|
||||
<block type="lists_sort"></block>
|
||||
<block type="lists_reverse"></block>
|
||||
</category>
|
||||
<category name="Colour" colour="20">
|
||||
<block type="colour_picker"></block>
|
||||
|
||||
Reference in New Issue
Block a user