[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Python slices in Scheme
From: |
Zelphir Kaltstahl |
Subject: |
Re: Python slices in Scheme |
Date: |
Mon, 19 Jun 2023 19:44:39 +0000 |
On 6/19/23 18:36, lloda wrote:
My library guile-newra (1) has quite general multidimensional array slicing.
The indices can be linear ranges or arbitrary integer arrays and they can have
any rank. You can also use the indexed array as write target. If all the
indices are linear ranges then the operation is done without copies. It also
has placeholders, so you can skip axes, like : and ... in numpy. guile-newra's
arrays are applicative, so you can do (thearray firstindex secondindex ...),
without special brackets.
You need wrappers to use it with Guile's native array types, so it may not be
as convenient. Or maybe you don't need to handle arbitrary rank, then it might
not be the best option. It's also far from finished, although the stuff I
mentioned above does work.
I didn't adopt Pythons -1 = end convention because in Guile arrays can have any
base index, not necessarily 0 (whether that's a good idea or not).
(1)https://lloda.github.io/guile-newra/#Slicing
<https://lloda.github.io/guile-newra/#Slicing>
https://github.com/lloda/guile-newra/ <https://github.com/lloda/guile-newra/>
https://notabug.org/lloda/guile-newra <https://notabug.org/lloda/guile-newra>
This reminds me of something I did for last year's advent of code:
https://notabug.org/ZelphirKaltstahl/advent-of-code-2022/src/931399414114380121df547e6bec4dfba35772a4/day-15/array-helpers.scm
This contains notably the following functions:
(1) array-next-index: iterate through an array of arbitrary rank, going from
index to index
(2) array-cell-ref-vec: allows partial referencing of array elements, for
example:
~~~~
(array-cell-ref-vec #2((1 2 3) (4 5 6)) #(1))
=> #1(4 5 6)
(array-cell-ref-vec #3(((1 2 3) (4 5 6)) ((1 2 3) (4 5 6))) #(1))
=> #2((1 2 3) (4 5 6))
(array-cell-ref-vec #3(((1 2 3) (4 5 6)) ((1 2 3) (4 5 6))) #(1 1))
=> #1(4 5 6)
(array-cell-ref-vec #3(((1 2 3) (4 5 6)) ((1 2 3) (4 5 6))) #(1 1 1))
=> 5
~~~~
(3) array-index-of: find the index of the first element satisfying predicate
(using array-next-index)
(4) array-indices-of: find all indices of elements satisfying a predicate (using
array-next-index)
Regards,
Zelphir
--
repositories:https://notabug.org/ZelphirKaltstahl