Skip to content

Commit

Permalink
Provide implicit conversions from scala.collection.Seq in Scala 2.1…
Browse files Browse the repository at this point in the history
…3 and up (#176)
  • Loading branch information
tmccarthy authored Apr 19, 2020
1 parent 8c4727e commit a9ea8c3
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package plotly

trait MutableSequenceImplicitConversions {
// Unneccessary in Scala 2.12, since the `Seq` alias refers to the supertype for mutable and immutable sequences
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package plotly

import plotly.Sequence.{DateTimes, Doubles, NestedDoubles, NestedInts, Strings}
import plotly.element.LocalDateTime
import scala.collection.{Seq => BaseScalaSeq}

trait MutableSequenceImplicitConversions {

implicit def fromMutableDoubleSeq(s: BaseScalaSeq[Double]): Sequence =
Doubles(s.toSeq)
implicit def fromMutableFloatSeq(s: BaseScalaSeq[Float]): Sequence =
Doubles(s.map(_.toDouble).toSeq)
implicit def fromMutableIntSeq(s: BaseScalaSeq[Int]): Sequence =
Doubles(s.map(_.toDouble).toSeq)
implicit def fromMutableLongSeq(s: BaseScalaSeq[Long]): Sequence =
Doubles(s.map(_.toDouble).toSeq)
implicit def fromMutableNestedDoubleSeq(s: BaseScalaSeq[BaseScalaSeq[Double]]): Sequence =
NestedDoubles(s.map(_.toSeq).toSeq)
implicit def fromMutableNestedIntSeq(s: BaseScalaSeq[BaseScalaSeq[Int]]): Sequence =
NestedInts(s.map(_.toSeq).toSeq)
implicit def fromMutableStringSeq(s: BaseScalaSeq[String]): Sequence =
Strings(s.toSeq)
implicit def fromMutableDateTimes(seq: BaseScalaSeq[LocalDateTime]): Sequence =
DateTimes(seq.toSeq)

}
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/plotly/Sequence.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import plotly.element.LocalDateTime

sealed abstract class Sequence extends Product with Serializable

object Sequence {
object Sequence extends MutableSequenceImplicitConversions {
final case class Doubles(seq: Seq[Double]) extends Sequence
final case class NestedDoubles(seq: Seq[Seq[Double]]) extends Sequence
final case class NestedInts(seq: Seq[Seq[Int]]) extends Sequence
Expand Down
31 changes: 31 additions & 0 deletions tests/src/test/scala/plotly/SequenceTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package plotly

import org.scalatest.FlatSpec

import scala.collection.mutable.ArrayBuffer

class SequenceTests extends FlatSpec {

"The implicit sequence conversion" should "convert a List to a Sequence" in {
assert((List(1, 2, 3): Sequence) === Sequence.Doubles(List(1d, 2d, 3d)))
}

it should "convert a mutable ArrayBuffer to a Sequence" in {
assert((ArrayBuffer(1, 2, 3): Sequence) === Sequence.Doubles(List(1d, 2d, 3d)))
}

it should "convert a nested mutable ArrayBuffer to a Sequence" in {
val mutableNestedDoubles: ArrayBuffer[ArrayBuffer[Double]] = ArrayBuffer(
ArrayBuffer(1d, 2d),
ArrayBuffer(3d, 4d),
)

val nestedDoublesList: List[List[Double]] = List(
List(1d, 2d),
List(3d, 4d),
)

assert((mutableNestedDoubles: Sequence) === Sequence.NestedDoubles(nestedDoublesList))
}

}

0 comments on commit a9ea8c3

Please sign in to comment.