Last active
May 22, 2019 13:37
-
-
Save andreortiz82/a8dcbdc1403e2526b485f724e744f70a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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] ...)) |
(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
In a previous attempt I used
mapv
instead offor
to iterate over the data but the result was similar.[[["Apple" :apple]["Orange" :orange]["Banana" :banana]] [["Tomato" :tomato]["Carrot" :carrot] ...]]