first a quesiton about idiomatic code:
;; provided by book Fogus & Houser, Joy of Clojure
(defn index [coll]
(cond
(map? coll) (seq coll)
(set? coll) (map vector coll coll)
:else (map vector (iterate inc 0) coll)))
;; book gives this as idiomatic code
(defn pos [needle coll]
(for [[i val] (index coll)
:when (= needle val)] i))
;; usage
(let [coll [:0 :1 :2 :3 :4 :5 :4]]
(pos :4 coll)) ;-> (4 6)
;; how can i make this alternate version better, and why provide :when, given
filter?
;; is it possible to get the destructuring elegance above without `for` ?
;; is this sequencing property of for (operate on individual values that will
end up in a seq)
;; the reason `for` exists? is not not possible to do have nice syntax wtihout
for (or seq-monad)?
(defn pos2 [needle coll]
(let [pairs (filter (fn [[i val]] (= val needle))
(index coll))]
(for [[i val] pairs] i))) ; can't destructure nicely without for, or
seq-monad, i think
meta question: where is the best place to ask noob questions like this, and
also more long-form discussion questions? stackoverflow frowns on questions
like this. i could slam in a couple of these a day, that doesn't seem
appropriate here.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en