Last active
April 12, 2021 10:55
-
-
Save raspasov/0521706c3f963a9209152bfb2becd91c to your computer and use it in GitHub Desktop.
A solution to a poorly defined "problem" :)
This file contains 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 sections | |
;convert to Clojure data | |
(js->clj | |
#js [ | |
#js { | |
"title" "Getting started", | |
"reset_lesson_position" false, | |
"lessons" [ | |
{"name" "Welcome"}, | |
{"name" "Installation"} | |
] | |
}, | |
#js { | |
"title" "Basic operator", | |
"reset_lesson_position" false, | |
"lessons" [ | |
{"name" "Addition / Subtraction"}, | |
{"name" "Multiplication / Division"} | |
] | |
}, | |
#js { | |
"title" "Advanced topics", | |
"reset_lesson_position" true, | |
"lessons" [ | |
{"name" "Mutability"}, | |
{"name" "Immutability"} | |
] | |
} | |
] | |
:keywordize-keys true)) | |
;"Solution" | |
(let [section-cnt (atom 0) | |
lesson-cnt (atom 0)] | |
(mapv | |
(fn [{:keys [lessons reset_lesson_position] :as section-m}] | |
(when (true? reset_lesson_position) | |
(reset! lesson-cnt 0)) | |
(assoc section-m | |
:position (swap! section-cnt inc) | |
:lessons (mapv #(assoc % :position (swap! lesson-cnt inc)) lessons))) | |
sections)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment