Circularly shifting nested vectors - clojure

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.

(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).

You can also use cycle:
(defn circle-drop [i coll]
(->> coll
cycle
(drop i)
(take (count coll))
vec))
(defn circles [coll i j]
(let [n (count coll)
i (- n i)
j (- n j)]
(->> coll
(map #(circle-drop i %))
(circle-drop j))))
(circles [[1 2 3] [4 5 6] [7 8 9]] 2 1)
;; => [[8 9 7] [2 3 1] [5 6 4]]

There's a function for that called rotate in core.matrix (as is often the case for general purpose array/matrix operations)
The second parameter to rotate lets you choose the dimension to rotate around (0 for rows, 1 for columns)
(use 'clojure.core.matrix)
(def A [[1 2 3] [4 5 6] [7 8 9]])
(rotate A 0 1)
=> [[4 5 6] [7 8 9] [1 2 3]]
(rotate A 1 1)
=> [[2 3 1] [5 6 4] [8 9 7]]

Related

Return all elements from a list

I have a function
(defn my-fn [a b & args]
[a
(for [arg args]
(into [] (butlast arg)))
b])
If I do (my-fn [1 2] [3 4] [5 6 2] [7 8 3])
It returns [[1 2] ([5 6] [7 8]) [3 4]]
I want the output to be [[1 2] [5 6] [7 8] [3 4]] but I cannot figure out how to do this
Any help would be much appreciated.
I'd into [a] all your mapped values and then conj b at the end. E.g.
(defn my-fn [a b & args]
(-> [a]
(into (map (comp vec butlast) args))
(conj b)))
and one more variant, using quote and unquote splicing:
user> (defn my-fn2 [a b & args]
`[~a ~#(map (comp vec butlast) args) ~b])
;;=> #'user/my-fn2
user> (my-fn2 [1 2] [3 4] [5 6 2] [7 8 3])
;;=> [[1 2] [5 6] [7 8] [3 4]]
Your for statement returns a list.
(defn my-fn [a b & args]
(->> [[a]
(map (comp vec butlast) args)
[b]]
(apply concat)
vec))
The final vec transform your seq into a vector.
Here is one solution:
(defn my-fn [a b & args]
(vec
(concat
[a]
(for [arg args]
(into [] (butlast arg)))
[b])))
(my-fn [1 2] [3 4] [5 6 2] [7 8 3])
=> [[1 2] [5 6] [7 8] [3 4]]
Please note that concat returns a lazy list, which for larger and/or more complicated problems can cause a StackOverflowException. The outer vec converts the lazy list in to a concrete vector. It is unnecessary for this example and you could remove it if you want.
As another note, concat expects each argument to be a list/vector, so I have wrapped a and b into 1-element vectors. The output of for is already a (lazy) list.
Because conj, cons, concat, etc can be somewhat non-intuitive in their behavior, you may wish to checkout the helper functions glue, append, prepend, & friends in the Tupelo library.
Background
Suppose we have a mixture of scalars & vectors (or lists) that we want to combine into a single vector. We want a function ??? to give us the following result:
(??? 1 2 3 [4 5 6] 7 8 9) => [1 2 3 4 5 6 7 8 9]
Clojure doesn’t have a function for this. Instead we need to wrap all of the scalars into vectors and then use glue or concat:
; can wrap individually or in groups
(glue [1 2 3] [4 5 6] [7 8 9]) => [1 2 3 4 5 6 7 8 9] ; could also use concat
(glue [1] [2] [3] [4 5 6] [7] [8] [9]) => [1 2 3 4 5 6 7 8 9] ; could also use concat
It may be inconvenient to always wrap the scalar values into vectors just to combine them with an occasional vector value. Instead, it might be more convenient to unwrap the vector values, then combine the result with other scalars. We can do that with the ->vector and unwrap functions:
(->vector 1 2 3 4 5 6 7 8 9) => [1 2 3 4 5 6 7 8 9]
(->vector 1 (unwrap [2 3 4 5 6 7 8]) 9) => [1 2 3 4 5 6 7 8 9]
It will also work recursively for nested unwrap calls:
(->vector 1 (unwrap [2 3 (unwrap [4 5 6]) 7 8]) 9) => [1 2 3 4 5 6 7 8 9]
Alternate Solution Using Python-Style Generator Functions
You could also solve it this way:
(ns tst.clj.core
(:use clj.core tupelo.test)
(:require [tupelo.core :as t] ))
(t/refer-tupelo)
(defn my-fn [a b & args]
(lazy-gen
(yield a)
(yield-all (for [arg args]
(into [] (butlast arg))))
(yield b)))
(my-fn [1 2] [3 4] [5 6 2] [7 8 3]) => ([1 2] [5 6] [7 8] [3 4])
Here lazy-gen creates a context for a "generator function", where successive values are added to the output list using the yield and yield-all functions. Note that the yield-all function is like the Clojure "unquote-splicing" operator ~# and "unwraps" its sequence into the output stream.
An even easier answer is to dive deeper into the for part of your original solution to apply the yield output tap:
(defn my-fn2 [a b & args]
(lazy-gen
(yield a)
(doseq [arg args]
(yield (butlast arg)))
(yield b)))
(my-fn2 [1 2] [3 4] [5 6 2] [7 8 3]) => ([1 2] (5 6) (7 8) [3 4])
This version of the solution follows your original logic more closely,
and avoids the accidental complexity of the for function wrapping its results into a sequence, which conflicts with the a and b values not being wrapped in a vector. We also no longer need the (into [] ...) part of the original function.
Note that we have replaced the for with doseq since:
We are no longer using the return value of the loop
And, therefore, a lazy solution would never run

What is an idiomatic way to implement double loop over a vector in Clojure?

I am new to Clojure and it's hard for me to idiomatically implement basic manipulations with data structures.
What would be an idiomatic way to implement the following code in Clojure?
l = [...]
for i in range(len(l)):
for j in range(i + 1, len(l)):
print l[i], l[j]
the simplest (but not the most FP-ish) is almost identical to your example:
(let [v [1 2 3 4 5 6 7]]
(doseq [i (range (count v))
j (range (inc i) (count v))]
(println (v i) (v j))))
and here is more functional variant to generate all these pairs (it isn't based on length or indices, but rather on the tail iteration):
(let [v [1 2 3 4 5 6 7]]
(mapcat #(map (partial vector (first %)) (rest %))
(take-while not-empty (iterate rest v))))
output:
([1 2] [1 3] [1 4] [1 5] [1 6] [1 7] [2 3] [2 4]
[2 5] [2 6] [2 7] [3 4] [3 5] [3 6] [3 7] [4 5]
[4 6] [4 7] [5 6] [5 7] [6 7])
then just use these pairs in doseq for any side effect:
(let [v [1 2 3 4 5 6 7]
pairs (fn [items-seq]
(mapcat #(map (partial vector (first %)) (rest %))
(take-while not-empty (iterate rest items-seq))))]
(doseq [[i1 i2] (pairs v)] (println i1 i2)))
update: following #dg123's answer. it is nice, but you can make it even better, using doseq's and for's features like destructuring and guards:
(let [v [1 2 3 4 5 6 7]]
(doseq [[x & xs] (iterate rest v)
:while xs
y xs]
(println "x:" x "y:" y)))
you iterate through the tails of a collection, but remember, that iterate produces an infinite coll:
user> (take 10 (iterate rest [1 2 3 4 5 6 7]))
([1 2 3 4 5 6 7] (2 3 4 5 6 7) (3 4 5 6 7)
(4 5 6 7) (5 6 7) (6 7) (7) () () ())
so you have to limit it somehow to include just not empty collections.
the destructuring form [x & xs] splits the argument to a first param and the sequence of the rest params:
user> (let [[x & xs] [1 2 3 4 5 6]]
(println x xs))
1 (2 3 4 5 6)
nil
and when the binded collection is empty, or have a single element, the xs would be nil:
user> (let [[x & xs] [1]]
(println x xs))
1 nil
nil
so you just make use of this feature, using :while guard in a list comprehension.
in the end you just construct pairs (or do some side effect in this case) for x and every item in xs
How about using map vector and iterate:
user=> (def l [1 2 3 4 5])
#'user/l
user=> (map vector l (iterate rest (drop 1 l)))
([1 (2 3 4 5)] [2 (3 4 5)] [3 (4 5)] [4 (5)] [5 ()])
which produces a lazy sequence of the value of each i index followed by all of its js.
You can then iterate over all of the pairs of values you need using for like so:
user=> (for [[i js] (map vector l (iterate rest (drop 1 l)))
j js]
[i j])
([1 2] [1 3] [1 4] [1 5] [2 3] [2 4] [2 5] [3 4] [3 5] [4 5])
Use doseq if you would like to perform IO instead of producing a lazy sequence:
user=> (doseq [[i js] (map vector l (iterate rest (drop 1 l)))
j js]
(println (str "i: " i " j: " j)))
i: 1 j: 2
i: 1 j: 3
i: 1 j: 4
i: 1 j: 5
i: 2 j: 3
i: 2 j: 4
i: 2 j: 5
i: 3 j: 4
i: 3 j: 5
i: 4 j: 5
nil

In clojure how to map over overlapping pairs?

Say I have the sequence:
[1 2 3 4 5]
And I want to map over them in the pairs:
[(1, 2), (2, 3), (3, 4), (4, 5)]
I have tried:
(map f (partition 2 [1 2 3 4]))
But this results in the sequence of pairs:
[(1, 2), (3, 4)]
How can I get the desired functionality?
By default partiton returns non-overlapping partitions, but you can supply a step argument to provide the offset at which partitions are created:
clojure.core/partition
([n coll] [n step coll] [n step pad coll])
Returns a lazy sequence of lists of n items each, at offsets step
apart. If step is not supplied, defaults to n, i.e. the partitions
do not overlap. If a pad collection is supplied, use its elements as
necessary to complete last partition upto n items. In case there are
not enough padding elements, return a partition with less than n items.
This will do what you want:
(partition 2 1 [1 2 3 4 5 6 7 8]))
; #=> ((1 2) (2 3) (3 4) (4 5) (5 6) (6 7) (7 8))
An alternative would be map both with the list you have and the rest of the list. E.g.:
(user=> (let [l [1 2 3 4 5]] (map list l (rest l)))
((1 2) (2 3) (3 4) (4 5))
I would do it as follows
; first generate data
; (that is if you really are talking about all the ints)
(map (juxt identity inc) [1 2 3 4 5])
=> ([1 2] [2 3] [3 4] [4 5] [5 6])
; inline example
(map (comp
(fn [[a b]] (println a b)) ; this would be your f
(juxt identity inc)) [1 2 3 4 5])
; using your function f
(map (comp
f
(juxt identity inc)) [1 2 3 4 5])
This also works independent of the order of numbers in your vector:
(let [a [5 4 3 2 1] b (rest a)] (map vector a b))
will yield:
([5 4] [4 3] [3 2] [2 1])

Combine sequence with itself recursively, without duplicates

This is hard to explain but easy to show. Not sure why I haven't figured it out myself, so I must be missing something in clojure that is obvious.
I need to combine a vector with itself, into a vector, and I need to combine the first element with all the remaining elements, then the 2nd element, with all the remaining elements (3rd and after).
As a short example:
[1 2 3 4 5]
I need a function to get:
[[1 2] [1 3] [1 4] [1 5] [2 3] [2 4] [2 5] [3 4] [3 5]]
If this looks like getting half the pairs for a large matrix, you are right. I only want to solve half the matrix minus the middle diagonal. This is the only part I need to be sequential(so I only solve half) and the rest I want to use the reducers library to parallelize the heavier math in the background.
Thanks in advance!
What you want is built into clojure/math.combinatorics:
https://github.com/clojure/math.combinatorics
The basic example you are looking for is on the readme, but for completeness to this answer, I'll repeat it here:
(ns example.core
(:require [clojure.math.combinatorics :as combo]))
(combo/combinations [1 2 3] 2)
;;=> ((1 2) (1 3) (2 3))
I'd just use a for :
(for [ i (range 1 6) j (range 6) :when (< i j)] [i j])
; => ([1 2] [1 3] [1 4] [1 5] [2 3] [2 4] [2 5] [3 4] [3 5] [4 5])
(def v [1 2 3 4 5])
(for [i (range (count v))
:let [vv (drop i v)]
r (rest vv)]
[(first vv) r])
=> ([1 2] [1 3] [1 4] [1 5] [2 3] [2 4] [2 5] [3 4] [3 5] [4 5])

Why does concat on vectors evaluate to a list?

Calling concat on vectors returns a list. Being a total noob I would expect that the result would also be a vector. Why the conversion to list?
Example:
user=> (concat [1 2] [3 4] [5 6])
(1 2 3 4 5 6)
; Why not: [1 2 3 4 5 6] ?
concat returns a lazy sequence.
user=> (doc concat)
-------------------------
clojure.core/concat
([] [x] [x y] [x y & zs])
Returns a lazy seq representing the concatenation of the elements in the supplied colls.
you can convert it back to a vector with into:
user=> (into [] (concat [1 2] [3 4] [5 6]))
[1 2 3 4 5 6]
into uses transients so it's pretty quick about it.