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

eg:.

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

It's easy to write this, but is there a built-in way to do this?

+4
source share
1 answer

There is no way to do this in a single function call if you want slices to return as vectors. In two calls it is still not possible if you really want the fragments (created by subvec ); otherwise you could use

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

to get new regular vectors. Without mapv vec you would get a seqs section.

+9
source

Source: https://habr.com/ru/post/1481526/


All Articles