Creating a method that accepts any 2D sequence and turns it into an array [Array [_]] in Scala

As indicated in the header, I want to have a method that can be applied to any argument of type Array[Array[_]]either Seq[Array[_]]or Array[Seq[_]]or or Seq[Seq[_]]. The argument must be turned into a 2D array ( Array[Array[_]]), thus changing the type of collections involved.

I have the following signature, which apparently accepts any such combination, but I cannot build arrays.

  def apply[A: Manifest, S[_] <: Seq[_], U <% S[S[A]]](components: U): CombinationIterator[A] = {
    new CombinationIterator(Array(components.map((s: S[A]) => s.toArray)))
  }

The class CombinationIteratortakes as an argument Array[Array[T]]. I get the following error:

error: could not find implicit value for evidence parameter of type ClassManifest[A]
new CombinationIterator(Array(components.map((s: S[A]) => s.toArray)))

For completeness, here is the constructor; perhaps this is important because it Ais required Manifest.

class CombinationIterator[A: Manifest](components: Array[Array[A]]) extends Iterator[Array[A]]

failed REPL session

The following works for Array[Seq[_]], but not for Seq[Array[_]]:

scala> def f[T:Manifest](s: Seq[Seq[T]]) = s.map(_.toArray).toArray
f: [T](s: Seq[Seq[T]])(implicit evidence$1: Manifest[T])Array[Array[T]]

scala> f(Array(Seq(1,2),Seq(3,4)))
res22: Array[Array[Int]] = Array(Array(1, 2), Array(3, 4))

scala> f(Seq(Array(1,2),Array(3,4)))
<console>:9: error: type mismatch;
 found   : Seq[Array[Int]]
 required: Seq[Seq[?]]
       f(Seq(Array(1,2),Array(3,4)))
            ^

() REPL didierd

scala> def f[T: Manifest, ST <% Seq[T]](s: Seq[ST]) = s.map(_.toArray).toArray
f: [T, ST](s: Seq[ST])(implicit evidence$1: Manifest[T], implicit evidence$2: (ST) => Seq[T])Array[Array[T]]

scala> f(Seq(Seq(1)))
<console>:9: error: No implicit view available from Seq[Int] => Seq[T].
       f(Seq(Seq(1)))
        ^

,

. , . .

, implicits -.

object CombinationIterator {
  case class AArray[T](aa: Array[Array[T]])
  implicit def seqseq2AA[T: Manifest](ss: Seq[Seq[T]]) = AArray(ss.map(_.toArray).toArray)
  implicit def seqarray2AA[T: Manifest](sa: Seq[Array[T]]) = AArray(sa.toArray)

  def apply[T: Manifest](components : AArray[T]): CombinationIterator[T] = {
    new CombinationIterator(components.aa)
  }
}

, . . . , , - - "" , , Vector.

+5
2

, , - , , , Seq, . , :

:

def ss2aa[A,B[_],C[_]](c: C[B[A]])(
  implicit b2seq: B[A] => Seq[A], c2seq: C[B[A]] => Seq[B[A]], ma: ClassManifest[A]
) = c2seq(c).map(b => b2seq(b).toArray).toArray

, :

implicit def seq2array[A: ClassManifest](sa: Seq[A]) = sa.toArray
def ss2aa[A,B[_],C[_]](c: C[B[A]])(
  implicit b2arr: B[A] => Array[A], c2arr: C[B[A]] => Array[B[A]], ma: ClassManifest[A]
) = c2arr(c).map(b2arr)

, , , , :

, -, . Array Seq Either:

implicit def ss2leftleft[A](ssa: Seq[Seq[A]]) = Left(Left(ssa))
implicit def sa2leftright[A](saa: Seq[Array[A]]) = Left(Right(saa))
implicit def as2rightleft[A](asa: Array[Seq[A]]) = Right(Left(asa))
implicit def aa2rightright[A](aaa: Array[Array[A]]) = Right(Right(aaa))
def ss2aa[A: Manifest](
  x: Either[Either[Seq[Seq[A]],Seq[Array[A]]],Either[Array[Seq[A]],Array[Array[A]]]]
) = x match {
  case Left(Left(y)) => y.map(_.toArray).toArray
  case Left(Right(y)) => y.toArray
  case Right(Left(y)) => y.map(_.toArray)
  case Right(Right(y)) => y
}

, , , , . , , Either.

- Miles Sabin. ; , , , :

object Example {
  // Type union system from Miles Sabin (with non-Unicode names)
  type Not[A] = A => Nothing
  type Union[A,B] = Not[Not[A] with Not[B]]
  type Id[A] = Not[Not[A]]

  def ss2aa[A,B[_],C[_]](b: C[B[A]])(
    implicit ev: (Id[B[A]] <:< Union[Seq[A],Array[A]]),
    ev2: (Id[C[B[_]]] <:< Union[Seq[B[_]],Array[B[_]]]),
    ma: ClassManifest[A],
    mssa: ClassManifest[Seq[Seq[A]]],
    msaa: ClassManifest[Seq[Array[A]]],
    masa: ClassManifest[Array[Seq[A]]],
    mf: ClassManifest[C[B[A]]]
  ) = {
    if (mf <:< mssa) b.asInstanceOf[Seq[Seq[A]]].map(_.toArray).toArray
    else if (mf <:< masa) b.asInstanceOf[Array[Seq[A]]].map(_.toArray)
    else if (mf <:< msaa) b.asInstanceOf[Seq[Array[A]]].toArray
    else b.asInstanceOf[Array[Array[A]]]
  }
}

, , .

+3

, Seq, Seq ( JVM, , ).

, , scala . , . :

def apply[A : Manifest, SA  <% Seq[A]](components: Seq[SA])
  • Seq. , S: Seq [_] .
  • , , , , Seq- , Seq . , .
  • Seq , Seq. Seq [Seq [A]] . Array , S <% Seq [A], .

, : _ * Array(components.map((s: S[A]) => s.toArray): _*). , Seq .

F , Array Seq. :

def f[T: Manifest, ST <% Seq[T]](s: Seq[ST])

: . , scala .

, . , Seq. , - .

class NestedSeqToNestedArray[A : Manifest] {
   def on[SA <% Seq[A]](s: Seq[SA]) = s.map(_.toArray).toArray
}
object NestedSeqToNestedArray {
  def apply[A : Manifest] = new NestedSeqToNestedArray[A]
}

NestedSeqToNestedArray[Int].on(Seq(Array(1)))
res11: Array[Array[Int]] = Array(Array(1))

"on" (, , apply then), NestedSeqToNestedArray, , .

, , , ?

def f[A : Manifest](s: Seq[Seq[A]]) = s.map(_.toArray).toArray
def g[A : Manifest](s: Seq[Array[A]]) = s.map(_.clone).toArray
+2

All Articles