Skip to content

Instantly share code, notes, and snippets.

@andreortiz82
Last active May 22, 2019 13:37
Show Gist options
  • Save andreortiz82/a8dcbdc1403e2526b485f724e744f70a to your computer and use it in GitHub Desktop.
Save andreortiz82/a8dcbdc1403e2526b485f724e744f70a to your computer and use it in GitHub Desktop.
(def some-data
[{:id :fruits
:title "Fruits"
:types [{:id :apple :title "Apple"}
{:id :orange :title "Orange"}
{:id :banana :title "Banana"}]}
{:id :vegetables
:title "Vegetables"
:types [{:id :tomato :title "Tomato"}
{:id :carrot :title "Carrot"}
{:id :banana :title "Cucumber"}]}
{:id :meats
:title "Meats"
:types [{:id :beef :title "Beef"}
{:id :chicken :title "Chicken"}
{:id :pork :title "Pork"}]}])
; Desired shape
; [["Apple" :apple]["Orange" :orange]["Banana" :banana]["Tomato" :tomato]["Carrot" :carrot] ...]
;; Attempt
(def reshape-some-data
(for [group nav-data
:let [sections (:sections group)]]
(for [sec sections
:let [id (:id sec)
title (:title sec)]]
[title id])))
; Current shape - NO GOOD!
; ((["Apple" :apple]["Orange" :orange]["Banana" :banana]) (["Tomato" :tomato]["Carrot" :carrot] ...))
@andreortiz82
Copy link
Author

In a previous attempt I used mapv instead of for to iterate over the data but the result was similar.
[[["Apple" :apple]["Orange" :orange]["Banana" :banana]] [["Tomato" :tomato]["Carrot" :carrot] ...]]

@tmcneill-reifyhealth
Copy link

tmcneill-reifyhealth commented May 22, 2019

(mapcat (fn [m] (map (fn [n] [(:title n) (:id n)]) (:types m))) some-data)
or

(def reshape-some-data
  (apply concat (for [group some-data
                      :let [sections (:types group)]]
                  (for [sec sections
                        :let [id (:id sec)
                              title (:title sec)]]
                    [title id]))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment