"Jim - FooBar();" <jimpil1...@gmail.com> writes:

> Ok we've established that accessing records fields is much faster than
> using regular maps...

Really?

> what will happen though if we create a memoized fn that simply returns
> the map?

Well, if you memoize a fn of no args, it'll always return the same
value.  If that's really what you want, I'd rather use

  (def the-map (expr-calculating-the-map))

If it needs to be a function for some reason, then I'd use

  (def gimme-the-map (constantly (expr-calculating-the-map)))

But I don't think the above applies to your usecase.  Can it be that the
function constructs the returned map by inspecting the value of some
ref/atom or the current-phase-of-moon or so?  In that case, you can't
memoize the fn.  Example:

--8<---------------cut here---------------start------------->8---
user> (def gimme-random (let [r (java.util.Random.)]
                             (fn [] (.nextInt r))))
#'user/gimme-random
user> (gimme-random)
696947893
user> (gimme-random)
-1591730151
user> (gimme-random)
459131481
user> (def memoized-gimme-random (memoize gimme-random))
#'user/memoized-gimme-random
user> (memoized-gimme-random)
-1840720068
user> (memoized-gimme-random)
-1840720068
user> (memoized-gimme-random)
-1840720068
--8<---------------cut here---------------end--------------->8---

Bye,
Tassilo

-- 
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

Reply via email to