# Create a structure vector with duplicates
core1 <- o_glycan_core_1()
core2 <- n_glycan_core()
structures <- c(core1, core2, core1) # core1 appears twice
# Map a function that counts vertices - only computed twice, not three times
smap_int(structures, igraph::vcount)
# Map a function that returns logical
smap_lgl(structures, function(g) igraph::vcount(g) > 5)
# Use purrr-style lambda functions
smap_int(structures, ~ igraph::vcount(.x))
smap_lgl(structures, ~ igraph::vcount(.x) > 5)
# Map a function that modifies structure (must return igraph)
add_vertex_names <- function(g) {
if (!("name" %in% igraph::vertex_attr_names(g))) {
igraph::set_vertex_attr(g, "name", value = paste0("v", seq_len(igraph::vcount(g))))
} else {
g
}
}
smap_structure(structures, add_vertex_names)
Run the code above in your browser using DataLab