Search code examples
clojureclojure-contrib

Circularly shifting nested vectors


Given a nested vector A

[[1 2 3] [4 5 6] [7 8 9]]

my goal is to circularly shift rows and columns.

If I first consider a single row shift I'd expect

[[7 8 9] [1 2 3] [4 5 6]]

where the 3rd row maps to the first in this case.

This is implemented by the code

(defn circles [x i j]
     (swap-rows x i j))

with inputs

(circles [[1 2 3] [4 5 6] [7 8 9]] 0 1)

However, I am unsure how to go further and shift columns. Ideally, I would like to add to the function circles and be able to either shift rows or columns. Although I'm not sure if it's easiest to just have two distinct functions for each shift choice.


Solution

  • (defn circles [xs i j]
      (letfn [(shift [v n]
                (let [n (- (count v) n)]
                  (vec (concat (subvec v n) (subvec v 0 n)))))]
        (let [ys (map #(shift % i) xs)
              j  (- (count xs) j)]
          (vec (concat (drop j ys) (take j ys))))))
    

    Example:

    (circles [[1 2 3] [4 5 6] [7 8 9]] 1 1)
    ;= [[9 7 8] [3 1 2] [6 4 5]]
    

    Depending on how often you expect to perform this operation, the sizes of the input vectors and the shifts to be applied, using core.rrb-vector could make sense. clojure.core.rrb-vector/catvec is the relevant function (you could also use clojure.core.rrb-vector/subvec for slicing, but actually here it's fine to use the regular subvec from clojure.core, as catvec will perform its own conversion).