Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a built-in way to get each slice of a given length from a vector in Clojure?

Tags:

clojure

E.g.:

(each-slice 3 [1 2 3 4 5])
; => [[1 2 3] [2 3 4] [3 4 5]]

It would not be hard to write it, but is there a built-in way to do it?

like image 489
bfontaine Avatar asked Jan 14 '23 08:01

bfontaine


1 Answers

There's no way to do it in a single function call if you want the slices returned as vectors. In two calls, it's still impossible if you really want slices (as created by subvec); otherwise you could use

(mapv vec (partition 3 1 [1 2 3 4 5]))

to get new regular vectors. Without the mapv vec, you'd get a seq of seqs.

like image 184
Michał Marczyk Avatar answered Jan 31 '23 06:01

Michał Marczyk