43

'map' は要素の数を保持するため、Tuple で使用することは理にかなっているようです。

これまでの私の試み:

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)

それはかなり苦痛に見えます...そして、私はそれをタプルに戻そうとさえしていません。
私はそれを間違っていますか?ライブラリを改善できますか?

4

3 に答える 3

35

一般に、タプルの要素の型は同じではないため、マップは意味がありません。ただし、特殊なケースを処理する関数を定義できます。

scala> def map[A, B](as: (A, A))(f: A => B) = 
     as match { case (a1, a2) => (f(a1), f(a2)) } 
map: [A,B](as: (A, A))(f: (A) => B)(B, B)

scala> val p = (1, 2)    
p: (Int, Int) = (1,2)

scala> map(p){ _ * 2 }
res1: (Int, Int) = (2,4)

Pimp My Library パターンを使用して、これを として呼び出すことができますp.map(_ * 2)

アップデート

要素の型が同じでない場合でも、操作でマッピングできるBifanctorTuple2[A, B]です。bimap

scala> import scalaz._
import scalaz._

scala> import Scalaz._
import Scalaz._

scala> val f = (_: Int) * 2
f: (Int) => Int = <function1>

scala> val g = (_: String) * 2
g: (String) => String = <function1>

scala> f <-: (1, "1") :-> g
res12: (Int, String) = (2,11)

更新 2

http://gist.github.com/454818

于 2010-02-26T07:11:31.790 に答える
29

shapeless中間HList表現によるタプルのマッピングとフォールディングをサポートします。

サンプルREPLセッション、

scala> import shapeless._ ; import Tuples._
import shapeless._
import Tuples._

scala> object double extends (Int -> Int) (_*2)
defined module double

scala> (3, 4).hlisted.map(double).tupled
res0: (Int, Int) = (6,8)

タプルの要素が異なる型である場合、型固有のケースを持つ多相関数でマッピングできます。

scala> object frob extends Poly1 {
     |   implicit def caseInt     = at[Int](_*2)
     |   implicit def caseString  = at[String]("!"+_+"!")
     |   implicit def caseBoolean = at[Boolean](!_)
     | }
defined module frob

scala> (23, "foo", false, "bar", 13).hlisted.map(frob).tupled
res1: (Int, String, Boolean, String, Int) = (46,!foo!,true,!bar!,26)

アップデート

shapeless 2.0.0-M1以降、タプルに対するマッピングが直接サポートされています。上記の例は次のようになります。

scala> import shapeless._, poly._, syntax.std.tuple._
import shapeless._
import poly._
import syntax.std.tuple._

scala> object double extends (Int -> Int) (_*2)
defined module double

scala> (3, 4) map double
res0: (Int, Int) = (6,8)

scala> object frob extends Poly1 {
     |   implicit def caseInt     = at[Int](_*2)
     |   implicit def caseString  = at[String]("!"+_+"!")
     |   implicit def caseBoolean = at[Boolean](!_)
     | }
defined module frob

scala> (23, "foo", false, "bar", 13) map frob
res1: (Int, String, Boolean, String, Int) = (46,!foo!,true,!bar!,26)
于 2012-05-07T18:56:00.597 に答える
1

map 関数は を取得してA => Bを返しますF[B]

def map[A, B](f: A => B) : F[B]

retronym が書いたように Tuple2[A, B] は Bifunctor であるため、scalaz や cat で bimap 関数を探すことができます。
bimap は、タプルの両側をマップする関数です。

def bimap[A, B, C, D](fa: A => C, fb: B => D): Tuple2[C, D]

Tuple[A, B] は 2 つの値を保持し、1 つの値のみをマップできるため (慣例により正しい値)、左側に同じ値を返し、right 関数を使用してタプルの右側の値をマップすることができます。 .

(3, 4).bimap(identity, _ * 2)
于 2018-12-14T19:23:32.493 に答える