On Nov 21, 3:10 am, Jonathan Amsterdam <
jbamster...@gmail.com> wrote:
> > Could you provide concrete examples...
>
> I'll do better than that; I'll provide an actual example from my job,
> which is in mortgage finance (yes, I know, shoot me now).
>
> Mortgage interest rates vary across two dimensions: the term of the
> mortgage (15 years, 30 years, etc.) and the entity issuing the
> mortgage (one of those quasi-governmental things like Freddie Mac that
> we all so love to hate now, or the so-called "Jumbo" rate for loans
> that don't meet those agencies' criteria). At startup I load vectors
> of historical interest rates, and I retrieve them based on those two
> criteria. In the actual C++ code, the key type of the map is
> pair<Term, Agency>.
But the C++ map is based on a sorted binary tree not a hash table, so
it's not a very good analogy.
> I don't see how I'd represent this is with a Go
> map, short of creating a string or int represented a combined Term and
> Agency.
I don't see how you'd do that either, but if I had that problem I
might try something like the following:
package main
import "os";
type year_to_rate map[int] float64;
type agency_to_year map[string] year_to_rate;
type agency_year_rate struct {
agency string;
year int;
rate float64;
};
var stuff = [...] agency_year_rate {
agency_year_rate{"Bailey", 1947, 0.01},
agency_year_rate{"Potter", 1947, 10.0},
agency_year_rate{"Bailey", 1946, 0.02},
agency_year_rate{"Potter", 1946, 20.0},
};
func fill_in_data () (*agency_to_year) {
aty := make (agency_to_year);
for _, item := range stuff {
if _, ok := aty[
item.agency]; !ok {
aty[
item.agency] = make (year_to_rate);
}
var ytra year_to_rate = aty[
item.agency];
ytra[item.year] = item.rate;
}
return & aty;
}
func (aty *agency_to_year) look_up_rate (agency string, year int)
float64 {
ytr, ok := (*aty)[agency];
if (! ok) {
println (agency, " not found");
os.Exit (1);
}
rate, ok := ytr[year];
if (! ok) {
println (year, " not found");
os.Exit (1);
}
return rate;
}
func main () {
aty := fill_in_data ();
println (aty.look_up_rate ("Bailey", 1947));
}
Maybe a Go-Ru can do better.
> > > b. Write my own hashtable package. This is unfortunate, since it's
> > > unlikely to be as good as the built-in one.
>
> > But, since you are saying that there is no built-in hash function to
> > go from type A to type B, how can your hashtable package not be as
> > good as something which doesn't exist?
>
> Very clever! Yes, well, what I mean is that under the hood, I'm
> guessing that there is a pretty general hashtable implementation that
> could, with a little tweaking, work on any type that could be hashed
> and compared for equality.
The string thing certainly seems to be a hashtable since the strings
come out in a random order.
> On the assumption that the Go implementation has a good hash function
> on strings, I want to use it as part of my hash function on my own
> type:
>
> type MyKey struct { s string, i int }
>
> function (k *MyKey) Hash() int { return hash.hashString(k.s) +
> k.i }
Re-hashing using a hash output doesn't look like a good way to me. But
I'm probably wrong.