Skip to content

Recommended syntax for slicing arrays #2186

Answered by allison-casey
josephtoles asked this question in Q&A
Discussion options

You must be logged in to vote

cut was changed to have the same syntax as slice which is what python's : sugar compiles down to. So arr[5:] actually compiles down to arr[slice(5, None)]. Now that cut is equivalent all the following are identical.

(setv x [1 2 3 4 5 6 7])
(get x (slice 5 None))
;; => [6 7]
(cut x 5 None)
;; => [6 7]
x[5:]
# => [6, 7]
x[slice(5, None)]
# => [6, 7]

If you need more complex slicing mechanics (or more "Pythonic"), and you're willing to use the master branch Hy version, you can check out Hyrule's #: and ncut macros. Specifically ncut for you since it allows the sugared n-dimensional slicing that numpy and pandas use frequently

(get x #: 5:)
;; => [6 7]
(ncut x 5:)
;; => [6 7]

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by allison-casey
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants