Just for fun, a version without loop, :
(def my-list '("abc" "x00b" "nanana" "zoot"))
(defn length-match [my-list target-length]
(first
(keep-indexed
(fn [idx len] (when (>= len target-length) idx))
(reductions + (map count my-list)))))
(length-match my-list 10)
; => 2
or the strictly equivalent threaded version:
(def my-list '("abc" "x00b" "nanana" "zoot"))
(defn length-match [my-list target-length]
(->> my-list
(map count)
(reductions +)
(keep-indexed (fn [idx len] (when (> len target-length) idx)))
first))
(length-match my-list 10)
2
HTH
--
Laurent
2013/7/9 Mark Engelberg <[email protected]>:
> On Tue, Jul 9, 2013 at 1:22 PM, Denis Papathanasiou
> <[email protected]> wrote:
>>
>> (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).
>
>
> 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.
>
>
> --
> --
> 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.
>
>
--
--
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.