Skip to content

Instantly share code, notes, and snippets.

@Hendekagon
Created October 18, 2019 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hendekagon/29c61ff5e6d49925fd0858ba74c7f88b to your computer and use it in GitHub Desktop.
Save Hendekagon/29c61ff5e6d49925fd0858ba74c7f88b to your computer and use it in GitHub Desktop.
reverse destructuring
(defn put [f x] (let [is-str? (char? (first x)) sx (apply str x)] (cond (map? f) (into {} (map vec (partition 2 (if (even? (count x)) x (conj x nil))))) (vector? f) (if is-str? (conj f sx) (into f x)) (list? f) (if is-str? (cons sx f) (concat f x)) (set? f) (if is-str? #{sx} (into f x)))))
(defn structure [s f] (mapv (fn [b a] (if (empty? b) (put b a) (let [r (structure a b)] (if (vector? b) (vec r) r)))) f (partition-all (int (/ (count s) (count f))) s)))
(structure (range 32) [[[] #{}] [#{} [] []]])
=>
[[[0 1 2 3 4 5 6 7] #{15 13 12 11 9 14 10 8}]
[#{20 17 19 16 18} [21 22 23 24 25] [26 27 28 29 30]]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment