Haskell has this cool general workaround that lets you call map on each node in the collection, from bottom to top. It is called everywhere , and you do something like everywhere f tree and f will be called on every node in your tree.
Writing something equivalent in Scala for Traversable very simple, but Haskell also works with tuples and equivalent case classes, or, more generally, that Scala calls Product s.
You can navigate through the elements in Product using the productIterator method, but is there an easy way to put the tuple or case class back together as soon as you know what arguments to the constructor (actually, I suppose the apply method) should be?
def mapOnProduct[X](f: X -> X, prod: Product) { val newArgs = prod.productIterator.map { case x: X => f(x) case id => id }.toList [?].apply(newArgs: _*) }
What can I replace [?] So that it has a chance to work?
Thanks!
Tob
source share