You are already doing exactly the right thing by having a temporary
variable.

To be precise a code fragment like:


(let [expensive-answer (some-fn x y z)
      final-result { :k1 (f1 expensive-answer)
                     :k2 (f2 expensive-answer)
                     :k3 (f3 expensive-answer) } ]
  final-result)


is already purely functional and very appropriate.  In fact, there is even
a name for a closely-related technique:
https://refactoring.com/catalog/extractVariable.html

Alan


On Sat, May 13, 2017 at 1:26 PM, Kevin Kleinfelter <
kleinfelter.gro...@gmail.com> wrote:

> How would one convert the following procedural logic into
> functional/Clojure?
>     x = expensive-calculation
>     return (f1(x), f2(x), f3(x))
>
> I'm sure I could force it by using a var to save the intermediate value,
> but that's still procedural.  It seems like the pattern of reusing an
> expensive result in multiple function calls without recalculating it must
> have a functional approach, but I'm not finding it.
>
> What I really want to do is to return:
>     {:key1 f1(f0(x)), :key2 f2(f0(x)), :key3 f3(f0(x))}
>
> without paying the price of executing f0 multiple times.
>
> Tnx.
>
> --
> 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/d/optout.
>

-- 
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/d/optout.

Reply via email to