Skip to main content
`for` is nicer than `map` with a lambda
Source Link
amalloy
  • 675
  • 4
  • 11

You are right to want to avoid creating the nested structure to begin with. I imagine you read my answer in the question you linked (the second one). As there, here the solution is to use mapcat instead of map. And as I also say in the comments there, while mapcat-indexed does not exist, you can just pass an extra (range) argument to get numbering.

(defn visible-nodes [tree]
  (mapcat (fn [idx ele]
            (map #(cons idx %)
               for [node (cons []
                         (when (:expanded ele)
                           (visible-nodes (:children ele))))
              (cons idx node))
          (range), tree))

You are right to want to avoid creating the nested structure to begin with. I imagine you read my answer in the question you linked (the second one). As there, here the solution is to use mapcat instead of map. And as I also say in the comments there, while mapcat-indexed does not exist, you can just pass an extra (range) argument to get numbering.

(defn visible-nodes [tree]
  (mapcat (fn [idx ele]
            (map #(cons idx %)
                 (cons []
                       (when (:expanded ele)
                         (visible-nodes (:children ele))))))
          (range), tree))

You are right to want to avoid creating the nested structure to begin with. I imagine you read my answer in the question you linked (the second one). As there, here the solution is to use mapcat instead of map. And as I also say in the comments there, while mapcat-indexed does not exist, you can just pass an extra (range) argument to get numbering.

(defn visible-nodes [tree]
  (mapcat (fn [idx ele]
            (for [node (cons []
                         (when (:expanded ele)
                           (visible-nodes (:children ele))))
              (cons idx node))
          (range), tree))
added 47 characters in body
Source Link
amalloy
  • 675
  • 4
  • 11

You are right to want to avoid creating the nested structure to begin with. I imagine you read my answer in the question you linked (the second one). As there, here the solution is to use mapcat instead of map. There's noAnd as I also say in the comments there, while mapcat-indexed does not exist, but you can just pass an extra (range) argument to get numbering.

(defn visible-nodes [tree]
  (mapcat (fn [idx ele]
            (map #(cons idx %)
                 (cons []
                       (when (:expanded ele)
                         (visible-nodes (:children ele))))))
          (range), tree))

You are right to want to avoid creating the nested structure to begin with. I imagine you read my answer in the question you linked (the second one). As there, here the solution is to use mapcat instead of map. There's no mapcat-indexed, but you can just pass an extra (range) argument to get numbering.

(defn visible-nodes [tree]
  (mapcat (fn [idx ele]
            (map #(cons idx %)
                 (cons []
                       (when (:expanded ele)
                         (visible-nodes (:children ele))))))
          (range), tree))

You are right to want to avoid creating the nested structure to begin with. I imagine you read my answer in the question you linked (the second one). As there, here the solution is to use mapcat instead of map. And as I also say in the comments there, while mapcat-indexed does not exist, you can just pass an extra (range) argument to get numbering.

(defn visible-nodes [tree]
  (mapcat (fn [idx ele]
            (map #(cons idx %)
                 (cons []
                       (when (:expanded ele)
                         (visible-nodes (:children ele))))))
          (range), tree))
added 125 characters in body
Source Link
amalloy
  • 675
  • 4
  • 11

JustYou are right to want to avoid creating the nested structure to begin with. I imagine you read my answer in the question you linked (the second one). As there, here the solution is to use mapcat instead of map to avoid producing the nested structure to begin with. There's no mapcat-indexed, but you can just pass an extra (range) argument to get numbering.

(defn visible-nodes [tree]
  (mapcat (fn [idx ele]
            (map #(cons idx %)
                 (cons []
                       (when (:expanded ele)
                         (visible-nodes (:children ele))))))
          (range), tree))

Just use mapcat instead of map to avoid producing the nested structure to begin with. There's no mapcat-indexed, but you can just pass an extra (range) argument to get numbering.

(defn visible-nodes [tree]
  (mapcat (fn [idx ele]
            (map #(cons idx %)
                 (cons []
                       (when (:expanded ele)
                         (visible-nodes (:children ele))))))
          (range), tree))

You are right to want to avoid creating the nested structure to begin with. I imagine you read my answer in the question you linked (the second one). As there, here the solution is to use mapcat instead of map. There's no mapcat-indexed, but you can just pass an extra (range) argument to get numbering.

(defn visible-nodes [tree]
  (mapcat (fn [idx ele]
            (map #(cons idx %)
                 (cons []
                       (when (:expanded ele)
                         (visible-nodes (:children ele))))))
          (range), tree))
Source Link
amalloy
  • 675
  • 4
  • 11
Loading