Skip to content

Added aslice macro #1249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Added support for proxies (#425)
* Added a `:slots` meta flag for `deftype` to disable creation of `__slots__` on created types (#1241)
* Added support for f-strings (#922)
* Added the `aslice` macro to facilitate the use of Python style `array[start:stop:step]` slicing in Basilisp (#1248)

### Changed
* Removed implicit support for single-use iterables in sequences, and introduced `iterator-seq` to expliciltly handle them (#1192)
Expand Down
19 changes: 17 additions & 2 deletions docs/pyinterop.rst
Original file line number Diff line number Diff line change
Expand Up @@ -288,15 +288,30 @@ Type hints may be applied to :lpy:form:`def` names, function arguments and retur
Python Slicing
--------------

Python slicing lets you extract parts of a sequence (like a list or string) using the syntax ``sequence[start:end:step]``:
Python slicing lets you extract parts of a sequence (like a list or string) using the syntax ``sequence[start:stop:step]``:

.. code-block:: python

coll = [-3, -2, -1, 0, 1, 2, 3]
coll[1:5:2]
# => [-2, 0]

Basilisp does not support slicing syntax directly, but it can be achieved using the :external:py:obj:`slice` operator combined with the :lpy:fn:`basilisp.core/aget` function:
Basilisp provides the :lpy:fn:`basilisp.core/aslice` macro to facilitate this syntax:

.. code-block:: clojure

(def coll #py [-3 -2 -1 0 1 2 3])
(aslice coll 3)
;; => #py [-3 -2 -1]

(aslice coll nil -3)
;; => #py [-3 -2 -1 0]

(aslice coll 1 5 2)
;; => #py [-2 0]


This macro is just a wrapper around Python's :external:py:obj:`slice` operator combined with the :lpy:fn:`basilisp.core/aget` function:

.. code-block:: clojure

Expand Down
13 changes: 12 additions & 1 deletion src/basilisp/core.lpy
Original file line number Diff line number Diff line change
Expand Up @@ -1949,7 +1949,7 @@
(python/len array))

(defmacro amap
"Map ``expr`` over the Python list `array`, returning a new Python list with the
"Map ``expr`` over the Python list ``array``, returning a new Python list with the
result.

This macro initially binds the symbol named by ``ret`` to a clone of ``array``\\. On
Expand Down Expand Up @@ -1977,6 +1977,17 @@
(recur ~expr (inc ~idx))
~ret))))

(defmacro aslice
"Get a portion of the Python list ``array`` using a
Python :external:py:obj:`slice` instance with a single ``stop``
value, or a combination of ``start`` and ``stop`` and optional
``step`` values.
Comment on lines +1981 to +1984
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we should add any documentation about the fact that nil is equivalent to omission in Python for any of these values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, I've tried to mention nil means omit in the next paragraph, but please be free to suggest a better wording.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. Thank you!


Equivalent to Python's ``array[start:stop:step]`` extended indexing
syntax. Use ``nil`` when omitting any of these fields."
[array & stop-or-start-stop-step]
`(aget ~array (python/slice ~@stop-or-start-stop-step) ))

(defn ^:inline booleans
"Dummy cast to a Python list of booleans.

Expand Down
13 changes: 13 additions & 0 deletions tests/basilisp/test_core_macros.lpy
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,19 @@
(let [l #py [1 2 3]]
(is (= 6 (areduce l idx ret 0 (+ ret (aget l idx)))))))

(deftest aslice-test
(let [l #py [-3 -2 -1 0 1 2 3]]
(is (= #py [-3 -2 -1 0] (aslice l 4)))
(is (= #py [-3 -2 -1 0 1 2 3] (aslice l nil)))
(is (= #py [-3 -2 -1 0 1] (aslice l -2)))
(is (= #py [-1 0] (aslice l 2 4)))
(is (= #py [-3 -2 -1 0] (aslice l nil 4)))
(is (= #py [1 2 3] (aslice l 4 nil)))
(is (= #py [1 2] (aslice l 4 -1)))
(is (= #py [-2 0 2] (aslice l 1 6 2)))
(is (= #py [3 1 -1] (aslice l 6 1 -2)))
(is (= #py [3 1 -1 -3] (aslice l nil nil -2)))))

(defmacro case-with-seq
[]
`(case :val
Expand Down
Loading