How do I filter nil values from a vector in Clojure while keeping the order intact?
- User
How do I filter nil values from a vector in Clojure while keeping the order intact?
- Assistant
To filter out
nilvalues from a vector in Clojure while maintaining the order of the remaining elements, you can use thefilterfunction, which is a common tool in Clojure for selecting items from a collection that meet a particular criterion. In this case, the criterion is that the elements are notnil.Here is a step-by-step guide on how to do this:
-
Use the
filterfunction: Thefilterfunction accepts two arguments: a predicate function and a collection. The predicate function should returntruefor elements you want to keep andfalsefor those you want to discard.
-
- User
(defn dropdown-button-menu "Standard dropdown menu button. Takes a nested config map.
Requires an options map passed with: :id - String, used for key generation :items - A vector of maps that contain key-value pairs of options for each item. Regular items usually contain :label and :on-click. Any custom items can be passed through the :custom key. Example use case: {:custom {:divider true}} to put a non-clickable divider into the list. See the link below for more details.
- Assistant
- User
It's supported by the following code:
(def ^:private dropdown-menu (ra/adapt-react-class rs/DropdownMenu)) (def ^:private dropdown-button (ra/adapt-react-class rs/ButtonDropdown)) (def ^:private dropdown-toggle (ra/adapt-react-class rs/DropdownToggle)) (def ^:private dropdown-item (ra/adapt-react-class rs/DropdownItem))
(defn- dropdown-item-interpreter "Checks if the item passed is a map, then reformats it to appear as an item in a dropdown list." [{:keys [label custom] :as config}] (if (map? config) (let [config (-> config (dissoc :custom) ;; pass any custom configuration directly into the config (merge custom))] [dropdown-item config label]) (throw-exception (str "Unexpected item passed through dropdown-item-interpreter: " config))))
(def default-styles-dropdown-button {:label "Actions" :button-options {:id (str "dropdown-button") ;; If there are multiple dropdowns with the same ID this leads to problems. You should set :id manually. :direction "down"} :toggle-options {:key "dropdown-toggle" :color "primary" :caret true} :menu-options {:key "dropdown-menu" :class "dropdown-menu-right"} ;; These items are here to show examples of the structure. ;; :items [{:label "Clickable item" ;; :disabled false ;; :on-click #(dispatch [:dispatch params]} ;; {:divider true} ;; {:label [:div.d-flex ;; "Disabled item " ;; [:i.fas.fa-question-circle.fa-md.ml-2]] ;; :disabled true ;; :on-click #(dispatch [:dispatch]} ;; {:label "Item with other effect" ;; :on-click (swap! atom function params)}] })
- Assistant
- User
- Assistant