memoization

How do you initialize variables in Ruby?

Are there any differences between the following ways of initialization of variables? @var ||= [] @var = [] if @var.nil? @var = @var || [] Please share your way initializing a variable and state the pros & cons. ...

What does the term "memoize" imply?

Comparing the terms "memoize" and "cache" and in reading Wikipedia's memoization entry, do people agree that using the term "memoize" implies The memoized result is kept in the process' memory; in other words, it isn't stored in memcached . One only "memoizes" functions, as in mathematical functions, e.g. Fibonacci, not values that may...

Explanation on "JavaScript - the Good Parts" example (section 4.15)?

Beginner in JS :) needs an explanation of code piece from Crockford's book, section 4.15: var memoizer = function (memo, fundamental) { var shell = function (n) { var result = memo[n]; if (typeof result !== 'number') { result = fundamental(shell, n); memo[n] = result; } return ...

Contiguous All-one block in a matrix

Hi, the problem is - Suppose you are given an mXn bitmap, represented by an array M[1..m,1.. n] whose entries are all 0 or 1. A all-one block is a subarray of the form M[i .. i0, j .. j0] in which every bit is equal to 1. Describe and analyze an efficient algorithm to find an all-one block in M with maximum area I am trying to make a ...

How do I generate memoized recursive functions in Clojure?

I'm trying to write a function that returns a memoized recursive function in Clojure, but I'm having trouble making the recursive function see its own memoized bindings. Is this because there is no var created? Also, why can't I use memoize on the local binding created with let? This slightly unusual Fibonacci sequence maker that starts...

Java: automatic memoization

I have a few functions in my code where it makes much sense (seems even mandatory) to use memoization. I don't want to implement that manually for every function separately. Is there some way (for example like in Python) I can just use an annotation or do something else so I get this automatically on those functions where I want it? ...

When is memoization automatic in GHC Haskell?

I can't figure out why m1 is apparently memoized while m2 is not in the following: m1 = ((filter odd [1..]) !!) m2 n = ((filter odd [1..]) !! n) m1 10000000 takes about 1.5 seconds on the first call, and a fraction of that on subsequent calls (presumably it caches the list), whereas m2 10000000 always takes the same amount of...

Rails cannot cache precalculated values across browser requests? (such as remembering n factorial results)

For example, the following code: class FoosController < ApplicationController def index if [email protected]? render :locals => {:bar => @foo} return else @foo = rand 10 render :locals => {:bar => @foo} end end end if I load localhost:3000/foos multiple times, it will show different values, and it is n...

"Caching" attributes of classes in Python

I'm writing a class in python and I have an attribute that will take a relatively long time to compute, so I only want to do it once. Also, it will not be needed by every instance of the class, so I don't want to do it by default in __init__. I'm new to Python, but not to programming. I can come up with a way to do this pretty easil...