juan_gandhi: (Default)
[personal profile] juan_gandhi
  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.

Profile

juan_gandhi: (Default)
Juan-Carlos Gandhi

June 2025

S M T W T F S
1 2345 6 7
891011121314
15161718192021
22232425262728
2930     

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jun. 9th, 2025 09:59 am
Powered by Dreamwidth Studios