Skip to content

Commit

Permalink
Merge pull request #166 from schneiderlin/patch-1
Browse files Browse the repository at this point in the history
fix slice function nil check, and docstring discrepancy
  • Loading branch information
genmeblog authored Sep 3, 2024
2 parents 0012ee2 + 8d140cc commit 4f81d3a
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/tablecloth/column/api/column.clj
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,26 @@
number, it is treated as an index from the end of the column. The
`:start` and `:end` keywords can be used to represent the start and
end of the column, respectively.
The `step` parameter determines the increment between each index in the
slice. It defaults to 1 if not provided.
Examples:
(def column [1 2 3 4 5])
(slice column 1 3) ;=> [2 3]
(slice column 2) ;=> [3 4 5]
(slice column -3 -1) ;=> [3 4 5]
(slice column :start 2) ;=> [1 2 3 4 5]
(slice column 2 :end) ;=> [3 4 5]
(slice column -2 :end) ;=> [4 5]"
(def c (column [1 2 3 4 5]))
(slice c 1 3) ;=> [2 3 4]
(slice c 2) ;=> [3 4 5]
(slice c -3 -1) ;=> [3 4 5]
(slice c :start 2) ;=> [1 2 3]
(slice c 2 :end) ;=> [3 4 5]
(slice c -2 :end) ;=> [4 5]
(slice c 0 :end 2) ;=> [1 3 5]"
([col from]
(slice col from :end))
([col from to]
(slice col from to 1))
([col from to step]
(let [len (count col)
from (or (when-not (or (= from :start) (nil? from)) from) 0)
to (or (when-not (or (= to :end) (nil? :end)) to) (dec len))]
to (or (when-not (or (= to :end) (nil? to)) to) (dec len))]
(col/select col (range (if (neg? from) (+ len from) from)
(inc (if (neg? to) (+ len to) to))
step)))))
Expand Down

0 comments on commit 4f81d3a

Please sign in to comment.