From 8fac147b5c2dc3fadd7693cbf51376b79bbbc0a7 Mon Sep 17 00:00:00 2001 From: Janko Thyson Date: Sun, 3 Jan 2021 21:04:52 +0100 Subject: [PATCH] Merging structural changes and unnamed lists Hi! This is my first pull request here, so hope I'm doing everything correctly. Hope the comments are sufficient to explain my thinking, please reach out if something should be unclear. - Added functionality to merge structural changes (named to unnamed list) and unnamed lists based on UID convention - Added unit tests for new merge functionality (probably will have to add these in a separate PR) --- R/merge.R | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/R/merge.R b/R/merge.R index 1bb2fa1..b2496a4 100644 --- a/R/merge.R +++ b/R/merge.R @@ -41,7 +41,48 @@ merge_lists <- function (base_list, overlay_list, recursive = TRUE) { overlay_list[which(names(overlay_list) %in% name)]) } } + + # Handle merging of structural changes or unnamed lists + merged_list <- if (all(is.null(names(overlay_list)))) { + # This characteristic implies that additional processing steps need to + # take place + if (!all(is.null(names(merged_list)))) { + # This implies a structural change from named list to unnamed list and + # thus the new content should be returned all together + overlay_list + } else { + # This is implies no structural break, but an unnamed list needs to be + # merged with another unnamed list. As this is a non-trivial task with + # lots of possible solutions, the current merging mechanism is a + # simple solution that expects the first element of the sublists to + # act as a unique identifier + + # Merge lists but with 'overlay_list' taking the lead + merged_list <- append(overlay_list, merged_list) + # Extract UIDs + uids <- extract_uids(merged_list) + # Drop duplicated UIDs and sort based on UIDs + merged_list[!duplicated(uids)][order(unique(uids))] + } + } else { + # Regular result if not special merging needed to take place + merged_list + } + merged_list } } +# Helper function for merging unnamed lists +extract_uids <- function(x) { + index <- lapply(x, function(.x) { + uid <- .x[[1]] + if (!is.list(uid)) { + uid + } else { + extract_uids(uid[[1]]) + } + }) + + unlist(index) +}