Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions almond/src/main/scala/plotly/Almond.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import java.lang.{Boolean => JBoolean, Double => JDouble, Integer => JInt}

import almond.interpreter.api.{DisplayData, OutputHandler}

import scala.collection.immutable.Seq
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep relying on the default Seq. That makes the API slightly different in 2.12 and 2.13, but that these APIs accept Seq(…) in both cases, which is not the case in 2.12 if we require scala.collection.immutable.Seq explicitly.

import scala.util.Random
import plotly.element._
import plotly.layout._
Expand Down
27 changes: 27 additions & 0 deletions core/shared/src/main/scala/plotly/Sequence.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package plotly

import plotly.element.LocalDateTime

import scala.collection.{Seq => BaseScalaSeq}
import scala.collection.immutable.Seq

sealed abstract class Sequence extends Product with Serializable

object Sequence {
Expand All @@ -27,4 +30,28 @@ object Sequence {
Strings(s)
implicit def fromDateTimes(seq: Seq[LocalDateTime]): Sequence =
DateTimes(seq)

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

private def makeImmutableSeq[A](mutableSeq: BaseScalaSeq[A]): Seq[A] =
mutableSeq match {
case asImmutableSeq: Seq[A] => asImmutableSeq
case _ => mutableSeq.toVector
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is basically equivalent to the .toSeq method in Scala 2.13. Since we are cross-compiling with Scala 2.12, we require a dedicated implementation here.


}
1 change: 1 addition & 0 deletions core/shared/src/main/scala/plotly/Trace.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package plotly

import scala.collection.immutable.Seq
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, let's keep the default Seq, even though it's slightly different in 2.12 and 2.13, here and in all scala.collection.immutable.Seq added below.

import scala.language.implicitConversions

import java.lang.{ Boolean => JBoolean, Double => JDouble }
Expand Down
2 changes: 2 additions & 0 deletions core/shared/src/main/scala/plotly/element/ColorScale.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package plotly.element

import dataclass.data
import scala.collection.immutable.Seq

sealed abstract class ColorScale extends Product with Serializable

Expand Down
2 changes: 2 additions & 0 deletions core/shared/src/main/scala/plotly/element/Error.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package plotly
package element

import scala.collection.immutable.Seq

import dataclass.data

import java.lang.{ Boolean => JBoolean, Double => JDouble }
Expand Down
4 changes: 3 additions & 1 deletion core/shared/src/main/scala/plotly/element/HoverInfo.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package plotly.element

import scala.collection.immutable.Seq

sealed abstract class HoverInfo extends Product with Serializable {
def label: String
}
Expand All @@ -10,7 +12,7 @@ object HoverInfo {
def none: HoverInfo = None
def skip: HoverInfo = Skip
def apply(elements: Element*): HoverInfo =
Combination(elements)
Combination(Seq(elements: _*))


sealed abstract class Element(override val label: String) extends HoverInfo
Expand Down
2 changes: 2 additions & 0 deletions core/shared/src/main/scala/plotly/element/OneOrSeq.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package plotly.element

import scala.collection.immutable.Seq

sealed abstract class OneOrSeq[T] extends Product with Serializable

object OneOrSeq {
Expand Down
1 change: 1 addition & 0 deletions core/shared/src/main/scala/plotly/layout/Layout.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import java.lang.{ Integer => JInt, Double => JDouble, Boolean => JBoolean }

import plotly.element._
import dataclass.data
import scala.collection.immutable.Seq

@data class Layout(
title: Option[String],
Expand Down
2 changes: 2 additions & 0 deletions demo/src/main/scala/plotly/demo/DemoChart.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package plotly.demo
import plotly.Trace
import plotly.layout.Layout

import scala.collection.immutable.Seq

trait DemoChart {
def plotlyDocUrl: String
def id: String
Expand Down
1 change: 1 addition & 0 deletions joda-time/src/main/scala/plotly/Joda.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package plotly

import scala.collection.immutable.Seq
import org.joda.time._
import plotly.Sequence.DateTimes

Expand Down
1 change: 1 addition & 0 deletions render/js/src/main/scala/plotly/Plotly.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import plotly.element.Color
import plotly.internals.BetterPrinter
import plotly.layout._

import scala.collection.immutable.Seq
import scala.scalajs.js
import scala.scalajs.js.Dynamic.{global => g}
import scala.scalajs.js.JSON
Expand Down
1 change: 1 addition & 0 deletions render/jvm/src/main/scala/plotly/Plotly.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import argonaut.{Json, PrettyParams}
import plotly.internals.{BetterPrinter, Properties}

import scala.annotation.tailrec
import scala.collection.immutable.Seq

object Plotly {

Expand Down
1 change: 1 addition & 0 deletions render/shared/src/main/scala/plotly/Enumerate.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package plotly

import scala.collection.immutable.Seq
import shapeless.{ :+:, ::, CNil, Coproduct, Generic, HList, HNil, Inl, Inr, Strict }

sealed abstract class Enumerate[T] {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package plotly.internals

import argonaut.{DecodeJson, EncodeJson}
import scala.collection.immutable.Seq

trait ArgonautCodecsExtra {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import argonaut.ArgonautShapeless._
import argonaut.derive._
import shapeless._

import scala.collection.immutable.Seq
import scala.util.Try
import plotly.element._
import plotly.layout._
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))
}

}