Skip to content

Commit

Permalink
Add a function version of cut
Browse files Browse the repository at this point in the history
  • Loading branch information
Kodiologist committed May 23, 2023
1 parent ef730a4 commit b2a7110
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
13 changes: 0 additions & 13 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -972,19 +972,6 @@ base names, such that ``hy.core.macros.foo`` can be called as just ``foo``.
None)
(print (f 4)) ; Prints "14" and then "None"

.. hy:macro:: (cut [coll arg1 arg2 arg3])
``cut`` compiles to a :ref:`slicing expression <slicings>`, which selects multiple elements of a sequential data structure. The first argument is the object to be sliced. The remaining arguments are optional, and understood the same way as in a Python slicing expression. ::

(setv x "abcdef")
(cut x) ; => "abcdef"
(cut x 3) ; => "abc"
(cut x 3 5) ; => "de"
(cut x -3 None) ; => "def"
(cut x 0 None 2) ; => "ace"

A ``cut`` form is a valid target for assignment (with :hy:func:`setv`, ``+=``, etc.) and for deletion (with :hy:func:`del`).

.. hy:macro:: (raise [exception :from other])
``raise`` compiles to a :py:keyword:`raise` statement, which throws an
Expand Down
31 changes: 29 additions & 2 deletions hy/pyops.hy
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ is equivalent to ``(+= count (+ n1 n2 n3)).``"
If you're looking for the Hy equivalent of Python list slicing, as in ``foo[1:3]``,
note that this is just Python's syntactic sugar for ``foo[slice(1, 3)]``, and Hy
provides its own syntactic sugar for this with a different macro, :hy:func:`cut`.
provides its own syntactic sugar for this with a different macro, :hy:func:`cut <hy.pyops.cut>`.
Note that ``.`` (:ref:`dot <dot>`) forms can also subscript. See also Hyrule's
:hy:func:`assoc <hyrule.collections.assoc>` to easily assign multiple elements of a
Expand All @@ -243,11 +243,38 @@ is equivalent to ``(+= count (+ n1 n2 n3)).``"
(setv coll (get coll k)))
coll)

(defn cut [coll / [arg1 'sentinel] [arg2 'sentinel] [arg3 'sentinel]]
#[[``cut`` compiles to a :ref:`slicing expression <slicings>`, which selects multiple
elements of a sequential data structure. The first argument is the object to be
sliced. The remaining arguments are optional, and understood the same way as in a
Python slicing expression. ::
(setv x "abcdef")
(cut x) ; => "abcdef"
(cut x 3) ; => "abc"
(cut x 3 5) ; => "de"
(cut x -3 None) ; => "def"
(cut x 0 None 2) ; => "ace"
A call to the ``cut`` macro (but not its function version in ``hy.pyops``) is a valid
target for assignment (with :hy:func:`setv`, ``+=``, etc.) and for deletion (with
:hy:func:`del`).]]

(cond
(= arg1 'sentinel)
(cut coll)
(= arg2 'sentinel)
(cut coll arg1)
(= arg3 'sentinel)
(cut coll arg1 arg2)
True
(cut coll arg1 arg2 arg3)))

(setv __all__
(list (map hy.mangle [
'+ '- '* '** '/ '// '% '@
'<< '>> '& '| '^ 'bnot
'< '> '<= '>= '= '!=
'and 'or 'not
'is 'is-not 'in 'not-in
'get])))
'get 'cut])))

0 comments on commit b2a7110

Please sign in to comment.