Skip to content

Commit 566d307

Browse files
authored
Add Python Slicing documentation entry (#1244)
Hi, Could you please consider a patch to add documentation for Python slicing? This has come up a few times in Discussions, and without guidance, it might take users a while to understand how slicing is achieved in Basilisp. Thanks Co-authored-by: ikappaki <[email protected]>
1 parent 0fead05 commit 566d307

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

docs/pyinterop.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,27 @@ Type hints may be applied to :lpy:form:`def` names, function arguments and retur
283283
Return annotations are combined as by :external:py:obj:`typing.Union`, so ``typing.Union[str, str] == str``.
284284
The annotations for individual arity arguments are preserved in their compiled form, but they are challenging to access programmatically.
285285

286+
.. _python_slicing:
287+
288+
Python Slicing
289+
--------------
290+
291+
Python slicing lets you extract parts of a sequence (like a list or string) using the syntax ``sequence[start:end:step]``:
292+
293+
.. code-block:: python
294+
295+
coll = [-3, -2, -1, 0, 1, 2, 3]
296+
coll[1:5:2]
297+
# => [-2, 0]
298+
299+
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:
300+
301+
.. code-block:: clojure
302+
303+
(def coll #py [-3 -2 -1 0 1 2 3])
304+
(aget coll (python/slice 1 5 2))
305+
;; => #py [-2 0]
306+
286307
.. _python_iterators:
287308

288309
Python Iterators

0 commit comments

Comments
 (0)