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

时间:2016-05-11 20:01:19

标签: vector clojure nested structure flatten

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

[[[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或库中有核心功能吗?我有一些关于如何解决这个问题的想法,但是所有这些看起来都非常低效,而且这个操作将用于大向量。

非常感谢你。

1 个答案:

答案 0 :(得分:8)

这是一个很有趣的问题,因为这是我认为使用拉链使事情更容易而不是更难的次数之一。想法是只做一个嵌套向量的vector-zip,来表示所需的结构,然后重复调用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]]]