From 1c23556ef45df23ad14a366ec723700aea78d361 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Thu, 11 Feb 2021 11:45:28 +0800 Subject: [PATCH] add zipper param --- src/util/_impl/_zip_impl.scad | 10 ++++++---- src/util/zip.scad | 4 ++-- test/util/test_zip.scad | 4 ++++ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/util/_impl/_zip_impl.scad b/src/util/_impl/_zip_impl.scad index fbcd5462..08504d21 100644 --- a/src/util/_impl/_zip_impl.scad +++ b/src/util/_impl/_zip_impl.scad @@ -1,5 +1,7 @@ -function _zipAll_sub(lists, list_to, elem_to, i = 0) = - i > elem_to ? [] : - concat([[for(j = [0:list_to]) lists[j][i]]], _zipAll_sub(lists, list_to, elem_to, i + 1)); +_identity = function(elems) elems; -function _zipAll(lists) = _zipAll_sub(lists, len(lists) - 1, len(lists[0]) - 1); \ No newline at end of file +function _zipAll_sub(lts, list_to, elem_to, zipper, i = 0) = + i > elem_to ? [] : + concat([zipper([for(j = [0:list_to]) lts[j][i]])], _zipAll_sub(lts, list_to, elem_to, zipper, i + 1)); + +function _zipAll(lts, zipper = _identity) = _zipAll_sub(lts, len(lts) - 1, len(lts[0]) - 1, zipper); \ No newline at end of file diff --git a/src/util/zip.scad b/src/util/zip.scad index a5255457..64ed2753 100644 --- a/src/util/zip.scad +++ b/src/util/zip.scad @@ -4,10 +4,10 @@ * @copyright Justin Lin, 2020 * @license https://opensource.org/licenses/lgpl-3.0.html * -* @see https://openhome.cc/eGossip/OpenSCAD/lib2x-zip.html +* @see https://openhome.cc/eGossip/OpenSCAD/lib3x-zip.html * **/ use <_impl/_zip_impl.scad>; -function zip(lts) = _zipAll(lts); \ No newline at end of file +function zip(lts, zipper) = is_function(zipper) ? _zipAll(lts, zipper) : _zipAll(lts); \ No newline at end of file diff --git a/test/util/test_zip.scad b/test/util/test_zip.scad index 1cb6da56..0bb20b22 100644 --- a/test/util/test_zip.scad +++ b/test/util/test_zip.scad @@ -1,4 +1,5 @@ use ; +use ; module test_zip() { echo("==== test_zip ===="); @@ -8,6 +9,9 @@ module test_zip() { zs = [2.5, 7.5, 12.4]; assert(zip([xs, ys, zs]) == [[10, 5, 2.5], [20, 15, 7.5], [30, 25, 12.4]]); + + sum_up = function(elems) sum(elems); + assert(zip([xs, ys, zs], sum_up) == [17.5, 42.5, 67.4]); } test_zip(); \ No newline at end of file