Optimized cleanup_path() and list_rotate()

This commit is contained in:
Garth Minette 2021-03-20 01:37:46 -07:00
parent d69242f848
commit 84bd2088e5
2 changed files with 12 additions and 3 deletions

View file

@ -432,7 +432,14 @@ function reverse(x) =
function list_rotate(list,n=1) = function list_rotate(list,n=1) =
assert(is_list(list)||is_string(list), "Invalid list or string.") assert(is_list(list)||is_string(list), "Invalid list or string.")
assert(is_finite(n), "Invalid number") assert(is_finite(n), "Invalid number")
let (elems = select(list,n,n+len(list)-1)) let (
ll = len(list),
n = ((n % ll) + ll) % ll,
elems = [
for (i=[n:1:ll-1]) list[i],
for (i=[0:1:n-1]) list[i]
]
)
is_string(list)? str_join(elems) : elems; is_string(list)? str_join(elems) : elems;

View file

@ -62,7 +62,8 @@ function is_closed_path(path, eps=EPSILON) = approx(path[0], path[len(path)-1],
// close_path(path); // close_path(path);
// Description: // Description:
// If a path's last point does not coincide with its first point, closes the path so it does. // If a path's last point does not coincide with its first point, closes the path so it does.
function close_path(path, eps=EPSILON) = is_closed_path(path,eps=eps)? path : concat(path,[path[0]]); function close_path(path, eps=EPSILON) =
is_closed_path(path,eps=eps)? path : concat(path,[path[0]]);
// Function: cleanup_path() // Function: cleanup_path()
@ -70,7 +71,8 @@ function close_path(path, eps=EPSILON) = is_closed_path(path,eps=eps)? path : co
// cleanup_path(path); // cleanup_path(path);
// Description: // Description:
// If a path's last point coincides with its first point, deletes the last point in the path. // If a path's last point coincides with its first point, deletes the last point in the path.
function cleanup_path(path, eps=EPSILON) = is_closed_path(path,eps=eps)? select(path,0,-2) : path; function cleanup_path(path, eps=EPSILON) =
is_closed_path(path,eps=eps)? [for (i=[0:1:len(path)-2]) path[i]] : path;
// Function: path_subselect() // Function: path_subselect()