Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Right" way to deal with multiple return values in clojure where order matters

Tags:

vector

clojure

I'm using hiccup to generate some checkboxes followed by labels. The original code looked like this:

(check-box "check1")
(label "check1" "Check1") 
(check-box "check2")
(label "check2" "Check2")
(check-box "check1")
(label "check3" "Check3")
(check-box "check4")
(label "check4" "Check4")

Which, after a lot of tinkering, I got down to the following line:

(map
    (fn [x]
        (map
            #(get (vector
                (check-box x)
                (label x (capitalize x)))%)
            [0 1]))
    ["check1" "check2" "check3" "check4"])

Which works, but I feel I'm not doing it in a very lisp-like/optimized/correct/etc. way - especially returning a vector just to scoop out those values with get. Is there a better way to do this?

like image 212
whunut Avatar asked Dec 04 '22 03:12

whunut


1 Answers

Perhaps destructuring is what you need?

(defn multi-return-fn []
  [:1 :2 :3])

(let [[x y z] (multi-return-fn)]
    (println x)
    (println y)
    (println z))

Destructuring also works in fn arguments, loop, for loops, etc.

like image 177
Timothy Baldridge Avatar answered Apr 07 '23 04:04

Timothy Baldridge