Clojure: map map - clojure

I would like to use the map function on map. But I can't get it to work.
A toy example:
(map map [+ - *] [1 2 3] [4 5 6] [7 8 9])
I expect a result like (12 15 18) but all I get is an error.
Thanks.

As an alternative to already existing answers, you could replace the outer map with a list comprehension, which is more readable than a nested map IMHO:
user=> (defn fun [ops & args]
#_=> (for [op ops]
#_=> (apply map op args)))
#'user/fun
user=> (fun [+ - *] [1 2 3] [4 5 6] [7 8 9])
((12 15 18) (-10 -11 -12) (28 80 162))

If you want to map each of the operator separately over the lists, then use
((fn [ops & args] (map #(apply map %1 args) ops)) [+ - *] [1 2 3] [4 5 6] [7 8 9])
or if you are willing to reorder arguments
(map #(map %1 [1 2 3] [4 5 6] [7 8 9]) [+ - *])
Both give the result of ((12 15 18) (-10 -11 -12) (28 80 162))

You can use juxt:
(apply map list (map (juxt + - *) [1 2 3] [4 5 6] [7 8 9]))
Which will result in: ((12 15 18) (-10 -11 -12) (28 80 162))

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

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

Reshaping nested vectors

Given a nested vector A, which is the 3 x 4 matrix
[[1 4 7 10] [2 5 8 11] [3 6 9 12]]
Transform A such that the nested vector (matrix) is now 2 x 6.
The output would look like
[[1 3 5 7 9 11] [2 4 6 8 10 12]]
As of now I am stuck on the beginning implementation of this idea.
You might want to look into core.matrix:
;; using [net.mikera/core.matrix "0.18.0"] as a dependency
(require '[clojure.core.matrix :as matrix])
(-> [[1 4 7 10] [2 5 8 11] [3 6 9 12]]
(matrix/transpose)
(matrix/reshape [6 2])
(matrix/transpose))
;= [[1 3 5 7 9 11] [2 4 6 8 10 12]]
this function will reshape m to be composed of subvectors with the desired shape
(defn reshape [m & shape]
(reduce (fn [vecs dim]
(reduce #(conj %1 (subvec vecs %2 (+ dim %2)))
[] (range 0 (count vecs) dim)))
(vec (flatten m)) (reverse shape)))
example:
(reshape [1 [2 3 4] 5 6 7 8] 2 2) => [[[1 2] [3 4]] [[5 6] [7 8]]]

How do I pass var args to an anonymous function in Clojure?

Is it possible to do var args for an anonymous function in Clojure?
For example, how do I turn:
(#(reduce + (map * %1 %2 %3)) [1 2 3] [4 5 6] [7 8 9])
into something like,
(#(reduce + (map * [& args])) [1 2 3] [4 5 6] [7 8 9])
This solves the problem:
user> ((fn[& args] (reduce + (apply map * args))) [1 2 3] [4 5 6] [7 8 9])
270
or
user> (#(reduce + (apply map * %&)) [1 2 3] [4 5 6] [7 8 9])
270
Use the apply function
I'm not aware of a way to do this with the #(...) syntax, but here is your example using fn
((fn [& args] (reduce + (apply map * args))) [1 2 3] [4 5 6] [7 8 9])
You can use %& to get the rest argument in the #(...) form, resulting in
(#(reduce + (apply map * %&)) [1 2 3] [4 5 6] [7 8 9])

Distributing list items to variables in clojure

When I pass this function
(into []
(map #(+ %1 %2)
[1 2]
[5 6]))
I get this result: [6 8]
What should I do to get this: [6 7 7 8] while keeping this #(+ %1 %2) ?
Seems like map isn't the right function in this case.
Use for when you want a Cartesian product:
user=> (for [x [1 2] y [5 6]]
#_=> (+ x y))
(6 7 7 8)
for is one option as Alex answer shows. map can also be used (with mapcat) as shown below:
user=> (mapcat #(map (partial + %1) [5 6]) [1 2])
(6 7 7 8)