scalapuzzlers / scalapuzzlers.github.com

Github Pages behind scalapuzzlers.com
www.scalapuzzlers.com
161 stars 53 forks source link

puzzler suggestion #114

Closed MasseGuillaume closed 10 years ago

MasseGuillaume commented 10 years ago

it comes with a good explaination http://stackoverflow.com/questions/25372561/how-does-orelse-work-on-partialfunctions

demobox commented 10 years ago

Thanks, @MasseGuillaume! Would you be interested in putting together a puzzler around this (hint, hint)..?

MasseGuillaume commented 10 years ago

oh and Array(1) == Array(1) would be my favorite :-)

demobox commented 10 years ago

oh and Array(1) == Array(1) would be my favorite :-)

It's a classic, indeed...what would you suggest as the four possible solutions?

demobox commented 10 years ago

I was thinking of ways in which you could make the comparison slightly less "direct", e.g.:

def eqSeq[T](a: Seq[T], b: Seq[T]) = a == b

But this actually works as expected due to array wrapping:

scala> println(eqSeq(List(1), List(1)))
true

scala> println(eqSeq(Array(1), Array(1)))
true

Any thoughts on that..?

MasseGuillaume commented 10 years ago

yeah, but adding level of indirection can make it worse:

case class Model(a: Array[Int])
Set(Model(Array(1)), Model(Array(2))).contains(Model(Array(1))) // false

or

Array(1).hashCode // 574628410
Array(1).hashCode // 737168815
Array(1).hashCode // 2106652649
demobox commented 10 years ago

@MasseGuillaume Continue the discussion about the first suggested puzzler at #115? ;-)