From b956329bc609507aee9d898863853b8b58ed8fa0 Mon Sep 17 00:00:00 2001 From: EnumEnv Date: Sat, 28 Sep 2024 22:08:36 +0200 Subject: [PATCH 1/3] New DeepClone code snippet I've made a bit more documented of a code snippet to help scripters navigate their way through it, considering not everyone viewing would be experienced. This take on the deep clone snippet also gives a faster result, meaning the code is ran faster and you get your value returned faster. OLD: **0.0000051** NEW: **0.0000034** The difference may be small but definitely in some cases noticeable and useful. This is due to the usage of the generalized iterator. Also simplifying the code with ternary operators. But, I believe my take on this new snippet would definitely be faster, and also educate other scripters better, even spiking their curiosity in code simplification if not familiar with ternary operators. All in short, I believe It's a great take. --- content/en-us/luau/tables.md | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/content/en-us/luau/tables.md b/content/en-us/luau/tables.md index 79d4c4a99..44b09952c 100644 --- a/content/en-us/luau/tables.md +++ b/content/en-us/luau/tables.md @@ -291,14 +291,21 @@ local clone = table.clone(original) To copy a more complex table with nested tables inside it, you'll need to use a recursive function similar to the following: ```lua -local function deepCopy(original) +-- The function used for deep copying a table. +local function deepCopy(original) + -- Define the new table for the copy local copy = {} - for k, v in pairs(original) do - if type(v) == "table" then - v = deepCopy(v) - end - copy[k] = v + + -- Loop through the original table to clone + for key, value in original do + -- If the type of the value is a table, + -- then deep copy it to the key (index). + -- Else (or) the type isn't a table, + -- assign the default value to the index instead. + copy[key] = type(value) == "table" and MY_deepCopy(value) or value end + + -- Return the finalized copy of the deep cloned table return copy end ``` From a353497ec416c9c75fd6c11f924005ff4895075f Mon Sep 17 00:00:00 2001 From: EnumEnv Date: Sat, 5 Oct 2024 08:23:07 +0200 Subject: [PATCH 2/3] Fixed recursive --- content/en-us/luau/tables.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/en-us/luau/tables.md b/content/en-us/luau/tables.md index 44b09952c..53d8da0b2 100644 --- a/content/en-us/luau/tables.md +++ b/content/en-us/luau/tables.md @@ -302,7 +302,7 @@ local function deepCopy(original) -- then deep copy it to the key (index). -- Else (or) the type isn't a table, -- assign the default value to the index instead. - copy[key] = type(value) == "table" and MY_deepCopy(value) or value + copy[key] = type(value) == "table" and deepCopy(value) or value end -- Return the finalized copy of the deep cloned table From b0c182d7db1d28e5a25a195fea9d5727ac0969c2 Mon Sep 17 00:00:00 2001 From: IgnisRBX <43388550+IgnisRBX@users.noreply.github.com> Date: Mon, 7 Oct 2024 05:21:36 -1000 Subject: [PATCH 3/3] Apply suggestions from code review --- content/en-us/luau/tables.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/content/en-us/luau/tables.md b/content/en-us/luau/tables.md index 53d8da0b2..5d79a1c25 100644 --- a/content/en-us/luau/tables.md +++ b/content/en-us/luau/tables.md @@ -291,17 +291,15 @@ local clone = table.clone(original) To copy a more complex table with nested tables inside it, you'll need to use a recursive function similar to the following: ```lua --- The function used for deep copying a table. +-- The function used for deep copying a table local function deepCopy(original) -- Define the new table for the copy local copy = {} -- Loop through the original table to clone for key, value in original do - -- If the type of the value is a table, - -- then deep copy it to the key (index). - -- Else (or) the type isn't a table, - -- assign the default value to the index instead. + -- If the type of the value is a table, deep copy it to the key (index) + -- Else (or) the type isn't a table, assign the default value to the index instead copy[key] = type(value) == "table" and deepCopy(value) or value end