Skip to content

Commit 1d28875

Browse files
Map on tuples added
1 parent c457581 commit 1d28875

File tree

5 files changed

+45
-8
lines changed

5 files changed

+45
-8
lines changed

library/src/scala/Tuple.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ sealed trait Tuple extends Any {
3535

3636
inline def zip[This >: this.type <: Tuple, T2 <: Tuple](t2: T2): Zip[This, T2] =
3737
DynamicTuple.dynamicZip(this, t2)
38+
39+
inline def map[F[_]](f: [t] => t => F[t]): Map[this.type, F] =
40+
DynamicTuple.dynamicMap(this, f)
3841
}
3942

4043
object Tuple {

library/src/scala/runtime/DynamicTuple.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package scala.runtime
22

3-
import scala.Tuple.{ Concat, Size, Head, Tail, Elem, Zip }
3+
import scala.Tuple.{ Concat, Size, Head, Tail, Elem, Zip, Map }
44

55
object DynamicTuple {
66
inline val MaxSpecialized = 22
@@ -257,6 +257,10 @@ object DynamicTuple {
257257
).asInstanceOf[Zip[This, T2]]
258258
}
259259

260+
def dynamicMap[This <: Tuple, F[_]](self: This, f: [t] => t => F[t]): Map[This, F] =
261+
Tuple.fromArray(self.asInstanceOf[Product].productIterator.map(f(_)).toArray)
262+
.asInstanceOf[Map[This, F]]
263+
260264
def consIterator(head: Any, tail: Tuple): Iterator[Any] =
261265
Iterator.single(head) ++ tail.asInstanceOf[Product].productIterator
262266

tests/neg/tuple-ops.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
val a: (1, 2, 3) = (1, 2, 3)
2+
val b: (4, 5, 6) = (4, 5, 6)
3+
val c: (7, 8) = (7, 8)
4+
val d: Unit = ()
5+
val e: (1, "foo", 10.1) = (1, "foo", 10.1)
6+
7+
// Zip
8+
val r1: ((1, 4), (2, 5), (6, 6)) = a.zip(b) // error
9+
val r2: ((1, 7), (1, 8)) = a.zip(c) // error
10+
val r3: ((2, 1), (8, 2)) = c.zip(a) // error
11+
12+
// Map
13+
case class Foo[X](x: X)
14+
15+
val r6: (Int, Int, String) = a.map[[t] =>> Int]([t] => x: t => x match { // error
16+
case x: Int => x * x
17+
case _ => ???
18+
})
19+
20+
val r7: ((1, Foo[1]), (2), (3, Foo[3])) =
21+
a.map[[t] =>> (t, Foo[t])]( [t] => x: t => (x, Foo(x)) ) // error

tests/run/tuple-ops.check

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
((7,1),(8,2))
44
()
55
()
6+
(1,4,9)
7+
((1,Foo(1)),(2,Foo(2)),(3,Foo(3)))

tests/run/tuple-ops.scala

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@ val b: (4, 5, 6) = (4, 5, 6)
33
val c: (7, 8) = (7, 8)
44
val d: Unit = ()
55

6+
// Zip
67
val r1: ((1, 4), (2, 5), (3, 6)) = a.zip(b)
78
val r2: ((1, 7), (2, 8)) = a.zip(c)
89
val r3: ((7, 1), (8, 2)) = c.zip(a)
910
val r4: Unit = d.zip(a)
1011
val r5: Unit = a.zip(d)
1112

12-
@main def Test = List(
13-
r1,
14-
r2,
15-
r3,
16-
r4,
17-
r5)
18-
.foreach(println)
13+
// Map
14+
case class Foo[X](x: X)
15+
16+
val r6: (Int, Int, Int) = a.map[[t] =>> Int]([t] => x: t => x match {
17+
case x: Int => x * x
18+
case _ => ???
19+
})
20+
21+
val r7: ((1, Foo[1]), (2, Foo[2]), (3, Foo[3])) =
22+
a.map[[t] =>> (t, Foo[t])]( [t] => x: t => (x, Foo(x)) )
23+
24+
@main def Test =
25+
List(r1, r2, r3, r4, r5, r6, r7).foreach(println)

0 commit comments

Comments
 (0)