From f1382cb7f405171b3d42d15e8dd73340e5199a7d Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Thu, 11 Feb 2021 11:50:32 +0800 Subject: [PATCH] update to 3.0 --- README.md | 2 +- docs/{lib2x-zip.md => lib3x-zip.md} | 8 ++++++-- src/experimental/note.md | 1 + src/util/_impl/_zip_impl.scad | 6 +++--- src/util/zip.scad | 2 +- 5 files changed, 12 insertions(+), 7 deletions(-) rename docs/{lib2x-zip.md => lib3x-zip.md} (64%) diff --git a/README.md b/README.md index 41a28cda..b08b1973 100644 --- a/README.md +++ b/README.md @@ -150,7 +150,7 @@ These examples incubate dotSCAD and dotSCAD refactors these examples. See [examp - [util/sub_str](https://openhome.cc/eGossip/OpenSCAD/lib3x-sub_str.html) - [util/split_str](https://openhome.cc/eGossip/OpenSCAD/lib3x-split_str.html) - [util/sum](https://openhome.cc/eGossip/OpenSCAD/lib3x-sum.html) -- [util/zip](https://openhome.cc/eGossip/OpenSCAD/lib2x-zip.html) +- [util/zip](https://openhome.cc/eGossip/OpenSCAD/lib3x-zip.html) ### Matrix - [matrix/m_determinant](https://openhome.cc/eGossip/OpenSCAD/lib2x-m_determinant.html) diff --git a/docs/lib2x-zip.md b/docs/lib3x-zip.md similarity index 64% rename from docs/lib2x-zip.md rename to docs/lib3x-zip.md index ac11c33f..782acea5 100644 --- a/docs/lib2x-zip.md +++ b/docs/lib3x-zip.md @@ -6,7 +6,8 @@ Make a list that aggregates elements from each of the lists. Returns a list of l ## Parameters -- `lts` : a list of lists. +- `lts` : A list of lists. +- `head` : Rather than listing the elements, the elements are combined using the function. **Since:** 3.0 ## Examples @@ -16,4 +17,7 @@ Make a list that aggregates elements from each of the lists. Returns a list of l ys = [5, 15, 25]; zs = [2.5, 7.5, 12.4]; - assert(zip([xs, ys, zs]) == [[10, 5, 2.5], [20, 15, 7.5], [30, 25, 12.4]]); \ No newline at end of file + 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]); \ No newline at end of file diff --git a/src/experimental/note.md b/src/experimental/note.md index 66be6bd2..21d85406 100644 --- a/src/experimental/note.md +++ b/src/experimental/note.md @@ -8,6 +8,7 @@ dotSCAD 3.0 Dev - `util/sort`: `by` accepts a function literal. - `util/bsearch`: only supports `sorted` and `target` parameters. I view it as a new function. - `util/dedup`: add the `eq` parameter. +- `util/zip`: add the `head` parameter. New modules/functions diff --git a/src/util/_impl/_zip_impl.scad b/src/util/_impl/_zip_impl.scad index 08504d21..753f9f4c 100644 --- a/src/util/_impl/_zip_impl.scad +++ b/src/util/_impl/_zip_impl.scad @@ -1,7 +1,7 @@ _identity = function(elems) elems; -function _zipAll_sub(lts, list_to, elem_to, zipper, i = 0) = +function _zipAll_sub(lts, list_to, elem_to, head, 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)); + concat([head([for(j = [0:list_to]) lts[j][i]])], _zipAll_sub(lts, list_to, elem_to, head, i + 1)); -function _zipAll(lts, zipper = _identity) = _zipAll_sub(lts, len(lts) - 1, len(lts[0]) - 1, zipper); \ No newline at end of file +function _zipAll(lts, head = _identity) = _zipAll_sub(lts, len(lts) - 1, len(lts[0]) - 1, head); \ No newline at end of file diff --git a/src/util/zip.scad b/src/util/zip.scad index 64ed2753..c91040ff 100644 --- a/src/util/zip.scad +++ b/src/util/zip.scad @@ -10,4 +10,4 @@ use <_impl/_zip_impl.scad>; -function zip(lts, zipper) = is_function(zipper) ? _zipAll(lts, zipper) : _zipAll(lts); \ No newline at end of file +function zip(lts, head) = is_function(head) ? _zipAll(lts, head) : _zipAll(lts); \ No newline at end of file