Hi, I'm new to clojure and I'd like to know if this is the best/idiomatic
way of solving this particular task:
I have a list of strings of variable length, and I want to find the
position or index number of the list whose cumulative size from the head of
the list matches a given target number.
For example, if this is the list:
(def my-list '("abc" "x00b" "nanana" "zoot"))
Then the index for a cumulative target length of 10 is 2, since:
(count (nth my-list 0)) -> 3 : cumulative size = 3
(count (nth my-list 1)) -> 4 : cumulative size = 7
(count (nth my-list 2)) -> 6 : cumulative size = 13 <-- 10 is less than 13
Here's how I wrote the function:
(defn get-length-match [my-list target-length counted-length ind]
(let [current-len (count (first my-list))]
(if (>= counted-length target-length)
ind
(recur (rest my-list) target-length (+ counted-length current-len)
(inc ind)))))
Ideally, I'd want the counted-length and ind parameters to be optional,
defaulting to 0 and -1 respectively, but I wasn't sure if clojure allows
for it (trying to define defaults for those in the function gave me a
compiler error).
So while the function works as intended, I wanted to find out if it could
be improved or made more idiomatic?
--
--
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
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.