Clojure:将嵌套矢量格式应用于展平矢量

Clojure: Apply a nested vector format to a flattened vector

例如,我有以下嵌套向量:

[[[0.582198689235419 -0.34713183143727 0.4685311493624731]
  [-0.38928013774079284 -0.5901700383677557 -0.37573234072157]
  [0.6716356761877877 -0.19645167952721243 -0.5700686091940252]]
 [[0.0027162308840597005 -0.4483592764429284 -0.4766278022217257 -0.2724018313051576]
  [-0.2765881229144672 -0.8030656496255356 -0.16159395457031567 -0.27432324260043084]
  [-0.6154630466545907 -0.60573539482247 0.4417814561800192 -0.5559788990464504]
  [0.6194560094536031 -0.3663074359460578 -0.5704311251195602 0.7194827876969362]]]

我有以下扁平向量:

(0.5366343712173423
 -0.816449781850872
 -0.16066485785704843
 0.9816561233924161
 -0.09646744313584676
 -0.2619662625757997
 -0.9946004265996822
 -0.14096299956754854
 0.579260850612288
 -0.827601452607939
 -0.24934665032374648
 -0.42272393175707873
 0.11239245249400165
 -0.29878238708035043
 -0.61522274672097
 0.8298721730401472
 0.5016214138116059
 0.11633537727916243
 -0.0631891708267196
 -0.26569217599364303
 0.20900664784109668
 0.2005869506108401
 -0.2658279978034501
 0.3383997403318165
 -0.09353513546647907)

我希望将展平向量转换为嵌套向量,该向量遵循与上述嵌套向量相同的结构。 Clojure 中是否有核心函数或执行此操作的库?我对如何解决这个问题有一些想法,但所有这些似乎都非常非常低效,而且这个操作将用于大向量。

非常感谢您。

这是一个有趣的问题,因为这是我认为使用拉链使事情变得更容易而不是更难的极少数情况之一。这个想法是只对嵌套向量进行向量压缩,以表示所需的结构,并在其上重复调用 zip/next ;每当我们到达一个叶子节点时,我们将其值替换为输入序列中的下一个节点。

请注意,这假设嵌套结构中的项数与展平列表中的项数完全相同;如果不是这种情况,您可能会遇到某种错误,谁知道呢。

(require '[clojure.zip :as z])

(defn replace-values [structure values]
  (loop [z (z/vector-zip structure)
         values (seq values)]
    (cond (not values) (z/root z)
          (z/branch? z) (recur (z/next z) values)
          :else (recur (-> z
                           (z/replace (first values))
                           (z/next))
                       (next values)))))

user> (replace-values '[[[0.582198689235419 -0.34713183143727 0.4685311493624731]
                         [-0.38928013774079284 -0.5901700383677557 -0.37573234072157]
                         [0.6716356761877877 -0.19645167952721243 -0.5700686091940252]]
                        [[0.0027162308840597005 -0.4483592764429284 -0.4766278022217257 -0.2724018313051576]
                         [-0.2765881229144672 -0.8030656496255356 -0.16159395457031567 -0.27432324260043084]
                         [-0.6154630466545907 -0.60573539482247 0.4417814561800192 -0.5559788990464504]
                         [0.6194560094536031 -0.3663074359460578 -0.5704311251195602 0.7194827876969362]]]
                      '(0.5366343712173423
                        -0.816449781850872
                        -0.16066485785704843
                        0.9816561233924161
                        -0.09646744313584676
                        -0.2619662625757997
                        -0.9946004265996822
                        -0.14096299956754854
                        0.579260850612288
                        -0.827601452607939
                        -0.24934665032374648
                        -0.42272393175707873
                        0.11239245249400165
                        -0.29878238708035043
                        -0.61522274672097
                        0.8298721730401472
                        0.5016214138116059
                        0.11633537727916243
                        -0.0631891708267196
                        -0.26569217599364303
                        0.20900664784109668
                        0.2005869506108401
                        -0.2658279978034501
                        0.3383997403318165
                        -0.09353513546647907))

[[[0.5366343712173423 -0.816449781850872 -0.16066485785704843]
  [0.9816561233924161 -0.09646744313584676 -0.2619662625757997]
  [-0.9946004265996822 -0.14096299956754854 0.579260850612288]]
 [[-0.827601452607939 -0.24934665032374648 -0.42272393175707873 0.11239245249400165]
  [-0.29878238708035043 -0.61522274672097 0.8298721730401472 0.5016214138116059]
  [0.11633537727916243 -0.0631891708267196 -0.26569217599364303 0.20900664784109668]
  [0.2005869506108401 -0.2658279978034501 0.3383997403318165 -0.09353513546647907]]]