Re: Simple FIFO cache for memoize

2013-01-23 Thread Baishampayan Ghose
Take a look at core.cache - https://github.com/clojure/core.cache ~BG

On Wed, Jan 23, 2013 at 1:11 PM, Omer Iqbal momeriqb...@gmail.com wrote:
 I've been reading a bit about the STM, and here's an implementation of a
 FIFO cache for producing a memoized version of a function. Is it correct to
 use the STM in this case, or are there any  drawbacks?

 (defn bounded-memoize
   Return a bounded memoized version of fn 'f'
that caches the last 'k' computed values
   [f k]
   (let [cache (ref {})
 values (ref clojure.lang.PersistentQueue/EMPTY)]
 (fn [ args]
   (if-let [e (find @cache args)]
 (val e)
 (let [result (apply f args)]
   (dosync
(alter values conj args)
(alter cache assoc args result)
(if ( (count @values) k)
  (let [evict (peek @values)]
(alter values pop)
(alter cache dissoc evict))
  )
result
))

 )
   ))
   )


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





-- 
Baishampayan Ghose
b.ghose at gmail.com

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




Re: Simple FIFO cache for memoize

2013-01-23 Thread Christophe Grand
and kotka.de/blog/2010/03/memoize_done_right.html has some intersting
discussion on memoization


On Wed, Jan 23, 2013 at 9:12 AM, Baishampayan Ghose b.gh...@gmail.comwrote:

 Take a look at core.cache - https://github.com/clojure/core.cache ~BG

 On Wed, Jan 23, 2013 at 1:11 PM, Omer Iqbal momeriqb...@gmail.com wrote:
  I've been reading a bit about the STM, and here's an implementation of a
  FIFO cache for producing a memoized version of a function. Is it correct
 to
  use the STM in this case, or are there any  drawbacks?
 
  (defn bounded-memoize
Return a bounded memoized version of fn 'f'
 that caches the last 'k' computed values
[f k]
(let [cache (ref {})
  values (ref clojure.lang.PersistentQueue/EMPTY)]
  (fn [ args]
(if-let [e (find @cache args)]
  (val e)
  (let [result (apply f args)]
(dosync
 (alter values conj args)
 (alter cache assoc args result)
 (if ( (count @values) k)
   (let [evict (peek @values)]
 (alter values pop)
 (alter cache dissoc evict))
   )
 result
 ))
 
  )
))
)
 
 
  --
  --
  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
 
 



 --
 Baishampayan Ghose
 b.ghose at gmail.com

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





-- 
On Clojure http://clj-me.cgrand.net/
Clojure Programming http://clojurebook.com
Training, Consulting  Contracting http://lambdanext.eu/

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




Re: Simple FIFO cache for memoize

2013-01-23 Thread David Powell
Specifically, core.memoize uses core.cache to provide more flexible
replacements for memoize:
https://github.com/clojure/core.memoize

-- 
Dave



On Wed, Jan 23, 2013 at 8:12 AM, Baishampayan Ghose b.gh...@gmail.comwrote:

 Take a look at core.cache - https://github.com/clojure/core.cache ~BG

 On Wed, Jan 23, 2013 at 1:11 PM, Omer Iqbal momeriqb...@gmail.com wrote:
  I've been reading a bit about the STM, and here's an implementation of a
  FIFO cache for producing a memoized version of a function. Is it correct
 to
  use the STM in this case, or are there any  drawbacks?
 
  (defn bounded-memoize
Return a bounded memoized version of fn 'f'
 that caches the last 'k' computed values
[f k]
(let [cache (ref {})
  values (ref clojure.lang.PersistentQueue/EMPTY)]
  (fn [ args]
(if-let [e (find @cache args)]
  (val e)
  (let [result (apply f args)]
(dosync
 (alter values conj args)
 (alter cache assoc args result)
 (if ( (count @values) k)
   (let [evict (peek @values)]
 (alter values pop)
 (alter cache dissoc evict))
   )
 result
 ))
 
  )
))
)
 
 
  --
  --
  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
 
 



 --
 Baishampayan Ghose
 b.ghose at gmail.com

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




Re: Simple FIFO cache for memoize

2013-01-23 Thread AtKaaZ
now I'm confused, which one is the right memoize to use?
and is that true about dosync? the nesting property of dosync: a nested
transaction merges with the surrounding one. or did it change in the past
almost 3 years since?


On Wed, Jan 23, 2013 at 12:08 PM, David Powell djpow...@djpowell.netwrote:


 Specifically, core.memoize uses core.cache to provide more flexible
 replacements for memoize:
 https://github.com/clojure/core.memoize

 --
 Dave



 On Wed, Jan 23, 2013 at 8:12 AM, Baishampayan Ghose b.gh...@gmail.comwrote:

 Take a look at core.cache - https://github.com/clojure/core.cache ~BG

 On Wed, Jan 23, 2013 at 1:11 PM, Omer Iqbal momeriqb...@gmail.com
 wrote:
  I've been reading a bit about the STM, and here's an implementation of a
  FIFO cache for producing a memoized version of a function. Is it
 correct to
  use the STM in this case, or are there any  drawbacks?
 
  (defn bounded-memoize
Return a bounded memoized version of fn 'f'
 that caches the last 'k' computed values
[f k]
(let [cache (ref {})
  values (ref clojure.lang.PersistentQueue/EMPTY)]
  (fn [ args]
(if-let [e (find @cache args)]
  (val e)
  (let [result (apply f args)]
(dosync
 (alter values conj args)
 (alter cache assoc args result)
 (if ( (count @values) k)
   (let [evict (peek @values)]
 (alter values pop)
 (alter cache dissoc evict))
   )
 result
 ))
 
  )
))
)
 
 
  --
  --
  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
 
 



 --
 Baishampayan Ghose
 b.ghose at gmail.com

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






-- 
I may be wrong or incomplete.
Please express any corrections / additions,
they are encouraged and appreciated.
At least one entity is bound to be transformed if you do ;)

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




Re: Simple FIFO cache for memoize

2013-01-23 Thread Michael Fogus
 now I'm confused, which one is the right memoize to use?

I'm not sure exactly what you mean, but if you mean which backing
cache to use the answer depends on your needs.  The core.cache wiki
has discussion about the advantages/disadvantages of using one type or
another.  You can find the discussion under each cache's detailed
information linked on the Using page --
https://github.com/clojure/core.cache/wiki/Using

 and is that true about dosync? the nesting property of dosync: a nested
 transaction merges with the surrounding one. or did it change in the past
 almost 3 years since?

It's always been that way.
:F

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




Re: Simple FIFO cache for memoize

2013-01-23 Thread Michael Fogus
Absolutely essential reading.

On Wed, Jan 23, 2013 at 6:02 AM, Christophe Grand christo...@cgrand.net wrote:
 and kotka.de/blog/2010/03/memoize_done_right.html has some intersting
 discussion on memoization


 On Wed, Jan 23, 2013 at 9:12 AM, Baishampayan Ghose b.gh...@gmail.com
 wrote:

 Take a look at core.cache - https://github.com/clojure/core.cache ~BG

 On Wed, Jan 23, 2013 at 1:11 PM, Omer Iqbal momeriqb...@gmail.com wrote:
  I've been reading a bit about the STM, and here's an implementation of a
  FIFO cache for producing a memoized version of a function. Is it correct
  to
  use the STM in this case, or are there any  drawbacks?
 
  (defn bounded-memoize
Return a bounded memoized version of fn 'f'
 that caches the last 'k' computed values
[f k]
(let [cache (ref {})
  values (ref clojure.lang.PersistentQueue/EMPTY)]
  (fn [ args]
(if-let [e (find @cache args)]
  (val e)
  (let [result (apply f args)]
(dosync
 (alter values conj args)
 (alter cache assoc args result)
 (if ( (count @values) k)
   (let [evict (peek @values)]
 (alter values pop)
 (alter cache dissoc evict))
   )
 result
 ))
 
  )
))
)
 
 
  --
  --
  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
 
 



 --
 Baishampayan Ghose
 b.ghose at gmail.com

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





 --
 On Clojure http://clj-me.cgrand.net/
 Clojure Programming http://clojurebook.com
 Training, Consulting  Contracting http://lambdanext.eu/

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





-- 
-- http://blog.fogus.me
-- http://github.com/fogus
--

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




Simple FIFO cache for memoize

2013-01-22 Thread Omer Iqbal
I've been reading a bit about the STM, and here's an implementation of a 
FIFO cache for producing a memoized version of a function. Is it correct to 
use the STM in this case, or are there any  drawbacks?
 
(defn bounded-memoize
  Return a bounded memoized version of fn 'f'
   that caches the last 'k' computed values
  [f k]
  (let [cache (ref {})
values (ref clojure.lang.PersistentQueue/EMPTY)]
(fn [ args]
  (if-let [e (find @cache args)]
(val e)
(let [result (apply f args)]
  (dosync
   (alter values conj args)
   (alter cache assoc args result)
   (if ( (count @values) k)
 (let [evict (peek @values)]
   (alter values pop)
   (alter cache dissoc evict))
 )
   result
   ))

)
  ))
  )


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