superruzafa / visual-scala-reference

Visual Scala Reference
https://superruzafa.github.io/visual-scala-reference
269 stars 28 forks source link

Correct mapConserve description and signature. #8

Closed exoego closed 5 years ago

exoego commented 5 years ago

I think "preserving its parameterized type" is not accurate description, simplification aside.

val list  = List(Some("a"), Some("b")) // List[Some[String]]
list.mapConserve(identity)             // List[Some[String]] preserved
list.mapConserve(s => Option(s.get))   // List[Option[String]] changed

Above example shows mapConserve can accept A => B as long as B is a super type of A . IIUC, a characteristic of mapConserve is that it will return the collection instance itself when all mapped elements eq to original.

And, A and B should be a sub type of AnyRef, not Any. So the signature also needs correction. https://www.scala-lang.org/api/2.13.0/scala/collection/immutable/List.html#mapConserve[B%3E:A%3C:AnyRef](f:A=%3EB):List[B] I will simplify this to def mapConserve[B] if it is too much detail for new readers.

superruzafa commented 5 years ago

I think that the use of Upper/Lower type bounds, even when accurated, can be confused for new-comers.

I need to find an acceptable way to express the behavior of mapConserve, both in the description and the diagram.

exoego commented 5 years ago

I think that the use of Upper/Lower type bounds, even when accurated, can be confused for new-comers.

Understood.

I need to find an acceptable way to express the behavior of mapConserve, both in the description and the diagram.

Awesome !! Closing this in favor of #12.