         ; Sequence of existing lesson groups
         moduleInfoExistent
         (filter
-          (fn [modInfo]
-            (not (nil? (get-in ModulesMap
-                               [(:moduleCode modInfo) "lessons"
-                                (:lessonType modInfo)
-                                (:lessonGroup modInfo)]))))
+          #(module-has-lesson-group? (:moduleCode %1) (:lessonType %1)
+                                     (:lessonGroup %1))
           ; convert matchArrays to maps
           (map (fn [matchArray]
                  {:moduleCode  (get-module-code-from-match-array

  [moduleCode lessonType lessonLabel bgColorCssClass isActuallySelected?
   & {:keys [ModulesMap]
      :or   {ModulesMap (get-ModulesMap)}}]
  (let [moduleName          (get-in ModulesMap [moduleCode "name"])
        modulesMapLessonSeq (get-in ModulesMap [moduleCode "lessons" lessonType
                                                lessonLabel])]
    (if (and moduleName modulesMapLessonSeq)
        (let [augLessonInfoSeq
              (doall (map (fn [modulesMapLesson]
                            (add-module-lesson! moduleCode moduleName
                                                           lessonType

  []
  (aget js/window "ModulesMap"))

(defn- get-module-name-from-module-code
  "Given a module code, returns the name of the module"
  [moduleCode]
  (let [modulesMap (get-ModulesMap)]
    (get-in modulesMap [moduleCode "name"])))

(defn- get-module-lesson-groups-map
  "Retrieves the map of lesson group strings to vector of lessons"
  [moduleCode lessonType]
  (let [modulesMap (get-ModulesMap)]
    (get-in modulesMap [moduleCode "lessons" lessonType])))

(defn- module-lesson-type-has-multiple-choices?
  "Given a module code and a long form lesson type, determines if there are
   multiple choices of lesson groups for that lesson type (eg. multiple
   tutorial choices for a module)."
  [moduleCode lessonType]
  (let [lessonGroupsMap (get-module-lesson-groups-map moduleCode lessonType)]
    (> (count lessonGroupsMap) 1)))

(defn- get-all-lesson-types-for-module
  "Returns a sequence of strings, where each string is a long form lesson type
   for a module."
  [moduleCode]
  (let [modulesMap (get-ModulesMap)]
    (keys (get-in modulesMap [moduleCode "lessons"]))))

(defn- get-all-lesson-group-strings
  "Given a module and a lesson type, returns a sequence of all its lesson
   group strings."
  [moduleCode lessonType]
  (let [lessonGroupsMap (get-module-lesson-groups-map moduleCode lessonType)]
    (keys lessonGroupsMap)))

(defn- get-first-lesson-group-for-module-lesson-type
  "Given a module and a lesson type, retrieves the 'first' lesson group string
   for that module.
   This is used for selecting a random lesson group for a lesson type of a
   module."
  [moduleCode lessonType]
  (first (get-all-lesson-group-strings moduleCode lessonType)))

(defn- module-has-lesson-group?
  "Given a module code, a lesson type, and a lesson group, determines if the
   module has such a lesson group."
  [moduleCode lessonType lessonGroup]
  (let [lessonGroupsMap (get-module-lesson-groups-map moduleCode lessonType)]
    (not (nil? (get lessonGroupsMap lessonGroup)))))

(def ^{:doc     "Width in pixels of a half hour timeslot"
       :private true
       }
