- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是使用 Shapeless 的新手,我正在尝试使用 Shapeless 来自动生成类型类并折叠 HList。
我的目标是渲染 HList
如 (a, b, c, d)
使用 scalaz.Show 的类型类实现
我的第一步是使用以下代码在 REPL 中进行试验
import shapeless._
import shapeless.ops.hlist._
object prettyPrint extends Poly2 {
implicit def defaultCase[A] = at((a:A, z:String)=>s", ${a.toString}$z")
}
def print[H, T<:HList](f: H :: T)(implicit folder:RightFolder.Aux[H :: T, String, prettyPrint.type, String]) = {
f.foldRight("")(prettyPrint)
}
val f = 1::'a::2::'b::HNil
val res = s"(${f.head}${print(f.tail)})" // Results res: String = (1, 'a, 2, 'b)
LabelledTypeClassCompanion[...]
的实现中实现了以下方法.不幸的是,这段代码无法编译,因为编译器提示缺少隐式,即使我不知道 REPL 中的代码和下面的代码之间有什么区别。我的问题是下面代码中的问题是什么,我该如何解决?
def showFold[H, T<:HList](f: H::T)(implicit folder:RightFolder.Aux[ H::T, String, prettyPrint.type, String]) = {
f.foldRight("")(prettyPrint)
}
override def product[H, T <: HList](name: String, ch: ScalazShow[H], ct: ScalazShow[T]): ScalazShow[H :: T] = {
new ScalazShow[H :: T] {
override def shows(ft: (H :: T)): String = {
showFold(ft) // This does not compile
}
}
}
package com.fpinscala.ninetynine
import shapeless._
import shapeless.ops.hlist.RightFolder
import scalaz.{Show => ScalazShow}
object prettyPrint extends Poly2 {
implicit def defaultCase[A]:this.Case.Aux[A, String, String] = at[A, String]{
(a,z) => s", $a$z"
}
}
object ShowImpl extends LabelledTypeClassCompanion[ScalazShow] {
implicit def symbolShow : ScalazShow[Symbol] = new ScalazShow[Symbol] {
override def shows(f: Symbol): String = f.toString()
}
implicit def intShow : ScalazShow[Int] = new ScalazShow[Int] {
override def shows(f: Int): String = f.toString
}
override val typeClass: LabelledTypeClass[ScalazShow] = new LabelledTypeClass[ScalazShow] {
override def coproduct[L, R <: Coproduct](name: String, cl: => ScalazShow[L], cr: => ScalazShow[R]): ScalazShow[L :+: R] = new ScalazShow[L :+: R] {
override def shows(lr: (L :+: R)): String = lr match {
case Inl(l) => cl.shows(l)
case Inr(r) => cr.shows(r)
}
}
override def emptyCoproduct: ScalazShow[CNil] = new ScalazShow[CNil] {
override def shows(f: CNil): String = ""
}
def showFold[H, T<:HList](f: H::T)(implicit folder:RightFolder.Aux[ H::T, String, prettyPrint.type, String]) = {
f.foldRight("")(prettyPrint)
}
override def product[H, T <: HList](name: String, ch: ScalazShow[H], ct: ScalazShow[T]): ScalazShow[H :: T] = {
new ScalazShow[H :: T] {
override def shows(ft: (H :: T)): String = {
showFold(ft) // This does not compile
}
}
}
override def project[F, G](instance: => ScalazShow[G], to: (F) => G, from: (G) => F): ScalazShow[F] = new ScalazShow[F] {
override def shows(f: F): String = instance.shows(to(f))
}
override def emptyProduct: ScalazShow[HNil] = new ScalazShow[HNil] {
override def shows(f: HNil): String = ""
}
}
}
最佳答案
您可以将类型类约束视为将有关类型的某些信息从具体上下文传送到通用上下文(通过调用堆栈向后移动)的一种方式。在这种情况下,您实际上确实需要 RightFolder
例如,如果你想以这种方式编写你的实现,但是在 LabelledTypeClass
中的方法签名不允许你携带这些信息,所以你很不走运(基本的想法是可能的,但你只需要一个稍微不同的方法)。
更新
我刚刚意识到我稍微误读了您的问题——因为您使用的是 TypeClass
type class 我假设您想要 case 类和密封特征层次结构以及 hlists 和 coproducts 的实例。我的回答为您提供了所有这些(就像 TypeClass
一样),因此您可以这样写:
scala> (123 :: "abc" :: HNil).shows
res2: String = (123, abc)
genericShow
定义。
Show
打印一个值两次。我们可以这样做:
scala> import scalaz._, Scalaz._
import scalaz._
import Scalaz._
scala> val x: Int = 123
x: Int = 123
scala> s"${ x.shows }${ x.shows }"
res0: String = 123123
x
有一个具体类型,当我们调用
.shows
时在上面,编译器会尝试找到
Show
的实例。对于那种具体类型。 Scalaz 提供了一个
Show[Int]
,所以一切正常,我们得到了我们想要的结果。
def toStringTwice[X](x: X): String = s"${ x.shows }${ x.shows }"
<console>:18: error: value shows is not a member of type parameter X
def toStringTwice[X](x: X): String = s"${ x.shows }${ x.shows }"
^
X
有一个
Show
例如,因为它对
X
一无所知.你可以写一堆重载的具体方法:
scala> def toStringTwice(x: String): String = s"${ x.shows }${ x.shows }"
toStringTwice: (x: String)String
scala> def toStringTwice(x: Int): String = s"${ x.shows }${ x.shows }"
toStringTwice: (x: Int)String
...
Show
实例,您可以通过向编译器提供所需的信息来抽象它们:
scala> def toStringTwice[X: Show](x: X): String = s"${ x.shows }${ x.shows }"
toStringTwice: [X](x: X)(implicit evidence$1: scalaz.Show[X])String
Int
调用它,或任何其他带有
Show
的东西实例:
scala> toStringTwice(123)
res2: String = 123123
def toStringFourTimes[X](x: X): String = s"${ toStringTwice(x) * 2 }"
scala> def toStringFourTimes[X: Show](x: X): String = s"${ toStringTwice(x) * 2 }"
toStringFourTimes: [X](x: X)(implicit evidence$1: scalaz.Show[X])String
Show
一直约束,直到你得到一个具体的类型。您只能使用
toStringTwice
有两种方式:在具有
Show
的具体类型上实例,或具有
Show
的泛型类型约束。
LabelledTypeClass
的一个很好的用例。 ,因为所需的实例并不真正适合构建
TypeClass
的实例的方式。类型类支持。你可能会这样做,但我真的不想尝试。
prettyPrint
的方式也存在问题有效——它实际上并没有使用
Show
A
的实例(甚至没有一个可以使用),而是调用可怕的通用
toString
.
import scalaz.Show, scalaz.Scalaz._
import shapeless._
import shapeless.ops.coproduct.Folder
import shapeless.ops.hlist.RightReducer
object prettyPrint2 extends Poly2 {
implicit def defaultCase[A: Show]: Case.Aux[A, String, String] =
at[A, String]((a, z) => s"$a, $z")
}
object prettyPrint extends Poly1 {
implicit def defaultCase[A: Show]: Case.Aux[A, String] = at[A](_.shows)
}
implicit def hlistShow[L <: HList](implicit
reducer: RightReducer.Aux[L, prettyPrint2.type, String]
): Show[L] = Show.shows(l => "(" + l.reduceRight(prettyPrint2) + ")")
implicit def coproductShow[C <: Coproduct](implicit
folder: Folder.Aux[prettyPrint.type, C, String]
): Show[C] = Show.shows(_.fold(prettyPrint))
implicit def genericShow[A, R](implicit
gen: Generic.Aux[A, R],
reprShow: Show[R]
): Show[A] = reprShow.contramap(gen.to)
scala> Foo(123, "abc").shows
res0: String = (123, abc)
scala> (Foo(123, "abc"): Base).shows
res1: String = (123, abc)
关于scala - HList.foldRight 在类型类的实现中使用时如何查找隐式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43690425/
我试图找到在庞大的代码库中创建 NaN 的位置。是否有一些编译器标志或我可以用来在 NaN 上 panic 的东西,这样我就可以找到它在哪一行? 最佳答案 没有编译器标志。你能做的最好的事情就是把你的
A类 class ClassA { @Autowired class ClassB; } 类配置: @Configuration class TestConfi
我是一名统计学研究生,经常使用 R。我熟悉其他编程环境中的 OOP。我什至在各种定义用于存储数据的新类的统计包中看到了它的使用。 在我研究生生涯的这个阶段,我通常会为一些类作业编写一些算法——一些接收
我想要两个不同的网络摄像头视频输出,一个是普通的网络摄像头镜头,另一个是它的“镜像”版本。 cv2可以吗? import time, cv2 video=cv2.VideoCapture(0) a=0
我创建了一个可以通过两种方式过滤的图库。一个通过单击按钮,另一个通过搜索过滤器。过滤器工作完美,除了当 div 隐藏在过滤器上时,其余显示的 div 不会彼此相邻 float 。 这是过滤前的样子:
我们作为一个 4 人团队工作,我们的项目部署在 openshift我们使用 git 存储库 进行提交、推送和 pull 。当有人提交更多更改时,其他人必须 pull 它以在我们的系统中进行更新。但是从
我正在尝试扩展自动完成功能,以便在选择某个项目时显示辅助标签。例如,给定显示项目的自动完成功能,项目名称将显示在包含代码的输入框旁边的 span 标记中。 查看自动完成源代码,我发现过滤值的下拉列表是
我有一个包含歌曲、艺术家和专辑实体的核心数据。 歌曲有可选的一对一关系艺术家到艺术家实体和专辑到专辑实体这两个实体都与 Song 实体具有反向关系。 相册有可选的一对一关系艺术家到艺术家实体和可选的一
XmlSerializer正在调用 IList.Add()在我的课上,我不明白为什么。 我有一个自定义类(层次结构中的几个类之一),其中包含我使用 XmlSerializer 与 XML 相互转换的数
我们在 Web 应用程序中定义了此事件,它创建了一个名为 timelineEventClicked 的自定义触发器 canvas.addEventListener('click', function
有大量资源可用于使用 Swift(可达性)检查有效的 Internet 连接,以及在进行 API 调用时检查 httpResponse 的 statusCode 的方法,但是检查和处理这些的“正确”方
谁能告诉我是否可以在 Controller 规范中 stub params[] 值,以便 Controller 接受 stub 值作为 View 中的实际 params[] 值。 例如,我的观点有一个
我的问题是没有在 UserControl 中连接 DependencyProperties。这不是问题。当我将 UserControl 中的按钮绑定(bind)到 UserControl 的 Depe
如何#define 路径 L"C:\Windows\System32\taskmgr.exe"来处理宽字符 #define TASK_MGR "C:\\Windows\\System32\\taskm
我正在尝试使用 Jasmine 和 Sion 编写单元测试,但是在使用 RequireJs 加载模块时我很难找到以下等效项: sinon.stub(window, "MyItemView"); 使用
我有一个包含三个 div 的示例页面,如下所示: 当浏览器大小达到 md 点并且第二个 div 高于第一个 div 时,第三个 div 开始在第一个的右侧
我在 C++ 端有 CString cs,在 C# 端有 IntPtr ip,它通过编码(marshal)处理机制包含 cs 的值。 然后,我只需将需要的字符串作为 Marshal.PtrToStri
我是一名优秀的程序员,十分优秀!