-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide implicit conversions from
scala.collection.Seq
in Scala 2.1…
…3 and up (#176)
- Loading branch information
Showing
4 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
core/shared/src/main/scala-2.12/plotly/MutableSequenceImplicitConversions.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
26 changes: 26 additions & 0 deletions
26
core/shared/src/main/scala-2.13/plotly/MutableSequenceImplicitConversions.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
|
||
} |