Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memoize a Double function in Haskell

I have a function

slow :: Double -> Double

which gets called very often (hundreds of millions of times), but only gets called on about a thousand discrete values. This seems like an excellent candidate for memoization, but I can't figure out how to memoize a function of a Double.

The standard technique of making a list doesn't work, since it's not an integral type. I looked at Data.MemoCombinators, but it doesn't natively support Doubles. There was a bits function for handling more data types, but Double isn't an instance of Data.Bits.

Is there an elegant way to memoize `slow?

like image 750
rprospero Avatar asked Jan 20 '26 11:01

rprospero


1 Answers

You could always use ugly-memo. The internals are impure, but it's fast and does what you need (except if the argument is NaN).

like image 79
augustss Avatar answered Jan 22 '26 07:01

augustss



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!