GitCrush
GitCrush

Reputation: 113

Create 2-dimensional vectors from 1-dimensionals

Although my question seems to be concerned with a rather trivial task, I was not yet able to sucessfully create a single 2-dimensional vector

[[1 2] [3 4]]

fom two individual vectors,

(def a [1 2]) and (def b [3 4]).

Leaving the functions conj and cons aside, I ran into the problem that vec or into-array both expect single input values. Another workaround would be pre-filling a two dimenstional vector

(vec (replicate 2 (vec (replicate 2 nil))))

but I it is still a complicated option.

Upvotes: 0

Views: 87

Answers (2)

There are several ways to approach this:

  1. As @MartinPůda says, use vector: (vector a b) => [[1 2] [3 4]])
  2. Use conj: (conj [] a b) => [[1 2] [3 4]]
  3. Use a vector literal: [a b] => [[1 2] [3 4]]

If Clojure has a problem it is an excess of riches. :-)

Upvotes: 4

Martin Půda
Martin Půda

Reputation: 7568

Just use vector:

(def a [1 2])
(def b [3 4])
(vector a b) => [[1 2] [3 4]]
[a b] => [[1 2] [3 4]]

Upvotes: 4

Related Questions