For comparison's sake, here's a version made with higher order functions.
HOF based functions are usually considered more idiomatic
Clojure<http://www.ehrdclj.org/files/27-March-2013-cgrand-YAGNI.pdf>than
(loop-)recur ones.
However, I've found that performance wise loop-recur ones often win.
(defn length-index
[coll target-length]
(->>
coll
(map-indexed (fn [idx, word] [idx (count word)]))
(reductions (fn [[acc_idx acc_len] [idx len]] [idx (+ acc_len len)])
[0 0] )
(filterv (fn [[idx cum_len]] (>= cum_len target-length)) )
ffirst))
On Tuesday, July 9, 2013 11:18:29 PM UTC+2, Denis Papathanasiou wrote:
>
> Hi, and thanks for your reply.
>
> On Tuesday, July 9, 2013 4:41:36 PM UTC-4, puzzler wrote
>>
>>
>> What you're looking for is:
>> (defn get-length-match [my-list target-length]
>> (loop [my-list my-list, counted-length 0, ind -1]
>> ...))
>>
>> In your recur, you can now omit target-length, but the rest of the code
>> stays the same.
>>
>
> So I should use loop together with recur?
>
> Does the recur at that point recursively call the function, or does it
> refer to the loop?
>
> Based on the docs, I think it's the latter, but it's not entirely clear.
>
> Also, in clojure is loop preferable to iteration by recursion as I had it
> originally?
>
--
--
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.