Search code examples
clojure

Clojure - function that returns a vector of vector


I want to write a function that when you pass in a vector as an argument, it returns the iterations of the vector ie

  • If you pass in [1], it returns [1]
  • If you pass in [1 2], it returns [[1 2] [2 1]]
  • If you pass in [1 2 3], it returns [[1 2 3] [2 3 1] [3 1 2]] etc

Any help would be much appreciated


Solution

  • it could look something like that:

    user> (defn its [items]
            (let [c (count items)]
              (if (<= c 1)
                items
                (->> items
                     cycle
                     (partition c 1)
                     (take c)
                     (mapv vec)))))
    #'user/its
    
    user> (its [])
    ;;=> []
    
    user> (its [1 2])
    ;;=> [[1 2] [2 1]]
    
    user> (its [1 2 3])
    ;;=> [[1 2 3] [2 3 1] [3 1 2]]