在 Scala 元组上使用函数组合器?
“map”保留元素的数量,因此在元组上使用它似乎是明智的。
到目前为止我的尝试:
scala> (3,4).map(_*2)
error: value map is not a member of (Int, Int)
(3,4).map(_*2)
^
scala> (3,4).productIterator.map(_*2)
error: value * is not a member of Any
(3,4).productIterator.map(_*2)
^
scala> (3,4).productIterator.map(_.asInstanceOf[Int]*2)
res4: Iterator[Int] = non-empty iterator
scala> (3,4).productIterator.map(_.asInstanceOf[Int]*2).toList
res5: List[Int] = List(6, 8)
它看起来很痛苦......而且我什至还没有开始尝试将它转换回元组。
我做错了吗?图书馆可以改进吗?
'map' preserves the number of elements, so using it on a Tuple seems sensible.
My attempts so far:
scala> (3,4).map(_*2)
error: value map is not a member of (Int, Int)
(3,4).map(_*2)
^
scala> (3,4).productIterator.map(_*2)
error: value * is not a member of Any
(3,4).productIterator.map(_*2)
^
scala> (3,4).productIterator.map(_.asInstanceOf[Int]*2)
res4: Iterator[Int] = non-empty iterator
scala> (3,4).productIterator.map(_.asInstanceOf[Int]*2).toList
res5: List[Int] = List(6, 8)
It looks quite painful... And I haven't even begun to try to convert it back to a tuple.
Am I doing it wrong? Could the library be improved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一般来说,元组的元素类型不相同,所以map没有意义。不过,您可以定义一个函数来处理特殊情况:
您可以使用 Pimp My Library 模式将其调用为
p.map(_ * 2)
。更新
即使元素的类型不同,
Tuple2[A, B]
也是一个 Bifunctor,可以使用bimap
操作进行映射。更新2
http://gist.github.com/454818
In general, the element types of a tuple aren't the same, so map doesn't make sense. You can define a function to handle the special case, though:
You could use the Pimp My Library pattern to call this as
p.map(_ * 2)
.UPDATE
Even when the types of the elements are not the same,
Tuple2[A, B]
is a Bifunctor, which can be mapped with thebimap
operation.UPDATE 2
http://gist.github.com/454818
shapeless 支持通过中间
HList
表示映射和折叠元组,示例 REPL 会话,
当元组的元素具有不同类型时,您可以使用具有特定于类型的情况的多态函数进行映射,
更新
作为无形状2.0.0-M1 直接支持元组映射。上面的例子现在看起来像这样,
shapeless Supports mapping and folding over tuples via an intermediary
HList
representation,Sample REPL session,
Where the elements of the tuple are of different types you can map with a polymorphic function with type-specific cases,
Update
As of shapeless 2.0.0-M1 mapping over tuples is supported directly. The above examples now look like this,
map 函数获取
A =>; B
并返回F[B]
。正如 Retronym 所写,Tuple2[A, B] 是一个 Bifunctor,因此您可以在 scalaz 或 cats 中查找 bimap 函数。
bimap 是一个映射元组两侧的函数:
因为 Tuple[A, B] 包含 2 个值,并且只能映射一个值(按照惯例,右侧值),因此您可以为左侧返回相同的值并使用正确的
函数来映射元组的正确值。
map function gets an
A => B
and returnsF[B]
.As retronym wrote Tuple2[A, B] is a Bifunctor, so you can look for the bimap function in scalaz or cats.
bimap is a function that maps both sides of the tuple:
Because Tuple[A, B] holds 2 values and only one value can be mapped (by convention the right value), you can just return the same value for the left side and use the right
function to map over the right value of the tuple.