what do you think?
May. 13th, 2010 09:16 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
case class LazyPair[X, Y](_1: X, f: X => Y) extends Product2[X, Y] { private lazy val y = f(_1) override def toString() = "(" + _1 + "," + _2 + ")" override def _2 = y } def lazyPair[X, Y](x: X, f: X => Y): Product2[X, Y] = new LazyPair(x, f)
The idea is, we won't call the function until we actually need it; and do it only once.
Note there's no variance in type parameters. I really do not know what to do about it, since the right variance would be [-X,+Y], and it does look stupid, no? Or maybe it does not.
Since it is case class, basically that's all we need.
Scala class
Pair
is actually Tuple2
which has tons of methods. I've lately came to a conclusion that we do not need most of the methods in most of the classes - e.g. Set[T]
does not need size
... and many other methods. Only have the right stuff. Well, whatever. Just asking for your opinion.