Search code examples
clojureconditional-statements

is there a way (or conditional statement in Clojure) to go through all different conditions even if one of the conditions is false?


I would like to evaluate all the conditions in this block but i can't find any conditional statement that does this without pulling out when one condition is false. If there's a way outside using conditional statements please let me know also.

Kindly help. Tnx.

(when
     ;;when "number" is found in 'a' execute the nextline
      (clojure.string/includes? (.toString a) "number")
        (println (hash-map :num {:test 'number?, :data (flatten num)}))
     ;;when "vector" is found in 'b' execute the nextline
      (clojure.string/includes? (.toString b) "vector")
        (println (hash-map :vec {:test 'vector?, :data vec}))
     ;;when "string" is found in 'c' execute the nextline
      (clojure.string/includes? (.toString c) "symbol")
        (println (hash-map :sym {:test 'symbol?, :data (flatten sym)}) )
     )

Solution

  • Just write it as a sequence of 3 when statements.

    (do   ; it doesn't hurt to wrap in a `do`, but this is usually not needed
    
      ;when "number" is found in 'a' execute the nextline
      (when (clojure.string/includes? (.toString a) "number")
        (println (hash-map :num {:test 'number?, :data (flatten num)})))
    
      ;when "vector" is found in 'b' execute the nextline
      (when (clojure.string/includes? (.toString b) "vector")
        (println (hash-map :vec {:test 'vector?, :data vec})))
    
      ;when "string" is found in 'c' execute the nextline
      (when (clojure.string/includes? (.toString c) "symbol")
        (println (hash-map :sym {:test 'symbol?, :data (flatten sym)}))))
    

    As the comment says, wrapping everything in a do is possible but probably not needed.