Skip to content

Instantly share code, notes, and snippets.

@raspasov
Last active April 12, 2021 10:55
Show Gist options
  • Save raspasov/0521706c3f963a9209152bfb2becd91c to your computer and use it in GitHub Desktop.
Save raspasov/0521706c3f963a9209152bfb2becd91c to your computer and use it in GitHub Desktop.
A solution to a poorly defined "problem" :)
(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