如何将 HList 转换为 HList 的 HList,如下面的代码片段所示。
import shapeless._
import Nat._
case class A(i: Int)
case class B(str: String)
case class C(i: Int, str: String)
type Input = A :: B :: C :: HNil
val in: Input = A(1) :: B("b") :: C(2, "c") :: HNil
type X = A :: HNil
val x: X = A(1) :: HNil
type Y = A :: B :: HNil // could also be B :: HNil
val y: Y = A(1) :: …Run Code Online (Sandbox Code Playgroud) import shapeless._
trait Something[T <: Something[T, R], R] {}
class Test[T <: Something[T, R], R, T1 <: Something[T1, _] <:!< T](t: T, t1: T1) {}
Run Code Online (Sandbox Code Playgroud)
但我得到:
type arguments [T1,?] do not conform to trait Something's type parameter bounds [T <: Something[T,R],R]
Run Code Online (Sandbox Code Playgroud)
这是有道理的,除非我希望它能起作用:
class Test1[T <: Something[T, R], R, T1 <: Something[T1, R1] <:!< T, R1](t: T, t1: T1)
Run Code Online (Sandbox Code Playgroud)
但它要求在 上具有相同的界限T1 <: Something[T, R]。
我想说的是,这个类需要 4 个类型参数,每对描述 的不同后代Something[T <: Something[T, R], R]。简单地说,强制执行T != T1。
这样做的正确方法是什么?
我是新手,没有变形,并试图通过这样做来学习它.我想创建一个非常小的库,它可以将String的集合(第一步是Sized集合)转换为不同类型的HList.
基本上我想要实现的目标:
import shapeless._
import nat._
import BigQueryParser._
val s: Sized[IndexedSeq[String], nat._3] = Sized("Testing", "2.0", "1")
BigQueryParser[Sized[IndexedSeq[String], nat._3], String :: BigDecimal :: BigInt :: HNil].parse(s)
Run Code Online (Sandbox Code Playgroud)
我的非工作实现在这里https://gist.github.com/taojang/f6a9352dbc618039e3a3
我在https://github.com/milessabin/shapeless/blob/master/examples/src/main/scala/shapeless/examples/csv.scala之后实现了它
我的代码没有编译,编译器抱怨以下错误:
[error] /somepath/some-file.scala: could not find implicit value for parameter st: exmaple.BigQueryParser[shapeless.Sized[IndexedSeq[String],shapeless.nat._3],shapeless.::[String,shapeless.::[BigDecimal,shapeless.::[BigInt,shapeless.HNil]]]]
[error] BigQueryParser[Sized[IndexedSeq[String], nat._3], String :: BigDecimal :: BigInt :: HNil].parse(s)
Run Code Online (Sandbox Code Playgroud) 我正在尝试解码一些真正糟糕的 JSON。每个对象的类型信息都在标记为type、 ie"type": "event"等的字段中编码。我使用Circe进行 JSON 编码/解码。该库使用类型类,其中相关的类型类是def apply(c: HCursor): Decoder.Result[A]. 问题是任何解码器都对类型不变,A. 这是一个具体的例子
sealed trait MotherEvent {
val id: UUID
val timestamp: DateTime
}
implicit val decodeJson: Decoder[MotherEvent] = new Decoder[MotherEvent] {
def apply(c: HCursor) = {
c.downField("type").focus match {
case Some(x) => x.asString match {
case Some(string) if string == "flight" => FlightEvent.decodeJson(c)
case Some(string) if string == "hotel" => // etc
// like a bunch of these
case None => …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Scala 中实现以下目标,这似乎超出了我的“通用”技能:
我有 2 个通用类:
class A[T]
class B[T]
Run Code Online (Sandbox Code Playgroud)
我想将一些 As 映射到一些 B:
val m = Map[A, B]
Run Code Online (Sandbox Code Playgroud)
现在不能编译,因为 A 和 B 是通用的,所以
val m = Map[A[_], B[_]]
Run Code Online (Sandbox Code Playgroud)
我希望能够存储任意类型 T 的 A/B 对。但是我只想添加键和值的泛型类型相同的对。所以我可以做
m updated(new A[String], new B[String])
Run Code Online (Sandbox Code Playgroud)
但不是
m updated(new A[String], new B[Int])
Run Code Online (Sandbox Code Playgroud)
我希望编译器意识到这一点,所以我可以做
val a = new A[String]
val b = new A[String]
val m = Map(a -> b)
val b: B[String] = m(a) //
Run Code Online (Sandbox Code Playgroud)
我在想像无形的图书馆会有所帮助吗?
我正在尝试编写一个 Spark 连接器来从 RabbitMQ 消息队列中提取 AVRO 消息。解码 AVRO 消息时,仅在 Spark 中运行时才会出现 NoSuchMethodError 错误。
我无法在 Spark 之外准确地重现 Spark 代码,但我相信这两个示例非常相似。我认为这是重现相同场景的最小代码。
我删除了所有连接参数,因为信息是私有的,而且连接似乎不是问题。
火花代码:
package simpleexample
import org.apache.spark.SparkConf
import org.apache.spark.streaming.rabbitmq.distributed.RabbitMQDistributedKey
import org.apache.spark.streaming.rabbitmq.models.ExchangeAndRouting
import org.apache.spark.streaming.rabbitmq.RabbitMQUtils
import org.apache.spark.streaming.{Seconds, StreamingContext}
import org.apache.spark.storage.StorageLevel
import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}
import com.sksamuel.avro4s._
import java.io.{ByteArrayInputStream, ByteArrayOutputStream}
import com.rabbitmq.client.QueueingConsumer.Delivery
import java.util.HashMap
case class AttributeTuple(attrName: String, attrValue: String)
// AVRO Schema for Events
case class DeviceEvent(
tenantName: String,
groupName: String,
subgroupName: String,
eventType: String,
eventSource: String,
deviceTypeName: String,
deviceId: Int,
timestamp: Long,
attribute: AttributeTuple
) …Run Code Online (Sandbox Code Playgroud) 我对无形完全陌生。我从列表中创建了一个案例类,如下所示:
val list = Seq(Some(1), Some(1.0), ...)
val y =
list
.toHList[Option[Int]::Option[Double]::Option[Int]::Option[Double]::Option[Double]::Option[Double]::Option[Double]::Option[Double]::Option[Double]::Option[Double]::Option[Double]::Option[Double]::Option[Double]::Option[Double]::HNil]
val z = y.get.tupled
val aa = YieldVariables.tupled(z)
Run Code Online (Sandbox Code Playgroud)
它运行良好,但我想知道是否有办法不在部件中写入所有这些类型toHList[Here]。
所以我想知道是否存在类似list.toHList[find the type yourself]或list.getTypesForHlist或MyCaseClass.getTypesForHlist这样的结果Option[Int]::Option[Double]...。
我看过Travis Brown 提出的很酷的解决方案,它允许以通用方式在彼此之间转换案例类。我试图用它来转换HList为 acase class但没有设法让它工作。这是我的尝试:
import shapeless._, ops.hlist.Align
import syntax.std.tuple._
object Shplss extends App {
class SameFieldsConverter[T] {
def apply[S, SR <: HList, TR <: HList](s: S)(implicit
genS: LabelledGeneric.Aux[S, SR],
genT: LabelledGeneric.Aux[T, TR],
align: Align[SR, TR]
) = genT.from(align(genS.to(s)))
}
def convertTo[T] = new SameFieldsConverter[T]
type SomeType = Int :: Int :: String :: Boolean :: Int :: Int :: HNil
final case class SomeProductType(f1: Int, f2: Int, f3: String, f4: Boolean, f5: Int, …Run Code Online (Sandbox Code Playgroud) 我可以通过谷歌找到使用Typeable演员的所有例子都包含for表达式.这对我有用,但我想知道这是否合适.我对于无形的全新:这是我在lib中的第一次导入.
import shapeless.Typeable._
val blarg: Future[Any] = (worker ? ListOfLongsPlx(foo)) // I know Any === Try[List[Long]]
blarg.map {
_.cast[Try[List[Long]]] match {
case Some(Success(xs)) => xs
case Some(Failure(f)) => /* reporting the failure or just default: */ ; List()
case None => /*reporting bad cast just a default: */ List()
}
}
Run Code Online (Sandbox Code Playgroud)
我应该期待这种模式的问题吗?
要清楚,这是通过我的测试.
Shapeless允许多态函数中的特定于类型的情况.是否有某种方法可以与类型成员实现相同,并获得道德等同
type Foo[T] = T match {
case Int => ...
case List[A] => ...
}
Run Code Online (Sandbox Code Playgroud)
?