Why not just (reduce func v l)?

On Sat, Sep 14, 2013 at 8:12 AM, Bogdan Opanchuk <manti...@gmail.com> wrote:

> Hello,
>
> Consider the following problem:
>
> (def v [1 2 3])
> (def l '(4 5 6))
>
> ; we need to apply in sequence
> ; `v <- (func v e)`,
> ; where `e` goes over elements of the list `l`
> (def new-v (apply-from-list func v l))
>
> For example, if `(def func conj)`, then the value of `new-v` is `[1 2 3 4
> 5 6]`. Is there some function (or a simple combination of functions) from
> the standard library that does that? It seems to me that it could be done
> with something like `->`, but a function, not a macro. At the moment I have
> the following ugly implementation:
>
> (defn apply-from-list
>   [init-res func init-l]
>   (if (empty? init-l)
>     init-res
>     (loop [res init-res
>            l init-l]
>       (let [first-elem (first l)
>             rest-elems (rest l)
>             res (func res first-elem)]
>         (if (empty? rest-elems)
>           res
>           (recur res rest-elems))))))
>
> --
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> 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 clojure+unsubscr...@googlegroups.com.
> 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 clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to