使用无形和教程中的示例:
import shapeless._
import syntax.std.tuple._
import poly._
object ShapelessPlay extends App{
val t = ((1,"a"),'c')
println(t flatMap identity)
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
could not find implicit value for parameter mapper: shapeless.ops.tuple.FlatMapper[((Int, String), Char),shapeless.poly.identity.type]
Run Code Online (Sandbox Code Playgroud)
println(t flatMap身份)
我错过了什么?^
我有这个辅助功能:
def findByType[T: ClassTag](xs: Seq[Any]) =
xs.find(classTag[T].runtimeClass.isInstance).map(_.asInstanceOf[T])
Run Code Online (Sandbox Code Playgroud)
我目前正在使用这样的:
val foo = findByType[Foo](xs)
val bar = findByType[Bar](xs)
val baz = findByType[Baz](xs)
Run Code Online (Sandbox Code Playgroud)
但是,这里有一些重复; 我想要的是什么(伪代码):
val List(foo, bar, baz) = List(Foo, Bar, Baz).map(t => findByType[t](xs))
Run Code Online (Sandbox Code Playgroud)
我有什么选择?我可以保持它的原样,但是如果有一些简单的东西可以干这个,我很乐意听到它.
我正在寻找抽象来组成类型类并避免样板代码:
sealed trait MyTypeClass[T]{
def add(t:T, mystuff:Something)
}
object MyTypeClass {
implicit def tupled[A,B](implicit adder1: MyTypeClass [A],adder2: MyTypeClass [B]): MyTypeClass [(A,B)] = new MyTypeClass [(A, B)] {
override def add(t: (A, B), mystuff: Something): Unit = {
val (a,b) = t
adder1 add a
adder2 add b
}
}
}
Run Code Online (Sandbox Code Playgroud)
有免费的样板吗?也许在无形?
情况:
事件的流(RxScala),我们使用tumblingBuffer()进行批处理,然后构建完整的调试历史记录.最终我想要所有值的(Seq [T],Seq [T]),所以我创建了以下函数作为foldLeft的累加器:
def tupleConcat[S](a: (Seq[S], Seq[S]), b: (Seq[S], Seq[S])) = (a._1 ++ b._1, a._2 ++ b._2)
Run Code Online (Sandbox Code Playgroud)
现在,这对我来说衬托一堆警钟的看了Runar&保罗在Scala的函数式编程之后,因为这看起来极像两个半群实例的MAP2,但我还是有点停留在如何正确地概括它.到目前为止,我认为它可能看起来像:
def tupleConcatSG[S](a: (S,S), b: (S,S))(implicit s: Semigroup[S]) = (a._1 |+| b._1, a._2 |+| b._2)
Run Code Online (Sandbox Code Playgroud)
(但是我必须从我能收集到的东西中将我的Seq推广到IndexedSeq).
为了进一步推广到任何Applicative,我想我需要一个元组实例,它可能来自Shapeless?还是我错过了一些明显的东西?
编辑:我还要补充一点,我试图避免压缩和解压缩,基于偏见的性能问题,但也许我不应该担心的是......(每个tumblingBuffer的价值(SEQ,SEQ)将是〜15000长,决赛(Seq,Seq)应该是数百万).
我正在尝试创建模仿无形类型的自定义类型类.它看起来像这样:
trait View[Record] {
type Result <: HList
def apply(r: Record): Result
}
object View extends LowPriorityLiftFunction1{
type Aux[Record, L <: HList] = View[Record] {type Result = L}
implicit def atView[Record: View] = at[Record](implicitly[View[Record]].apply)
}
Run Code Online (Sandbox Code Playgroud)
假设我提供它的功能如下:
object toHView extends ->( (_:Int) + 1)
implicit def provideView[Record, L <: HList]
(implicit generic: Generic.Aux[Record, L],
mapper: Mapper[toHView.type, L])
: View.Aux[Record, mapper.Out] =
new View[Record] {
type Result = mapper.Out
def apply(r: Record) = mapper(generic.to(r))
}
Run Code Online (Sandbox Code Playgroud)
所以,如果我们定义:
case class Viewable(x: Int, y: Int, …Run Code Online (Sandbox Code Playgroud) 给定一个HList Label[A](String)我想将它映射到HList LabelWithValue[A](Label[A], A),其中实际值来自a Map[String, Any].在下面的示例中,我只是在方法中定义了值的映射,只是想象值来自数据库.
下面的工作,但它是非常veckery hacky因为它使用全局变量.相反,我想Map[String, Any]进入GetLabelWithValue.我没有找到方法,因为调用者getValues隐式创建了一个Mapper,并且此时值的映射尚不存在.我自己尝试创建一个Mapper,但我的类型级编程技能还不够好.
import shapeless._
import shapeless.poly._
import shapeless.ops.hlist._
object Main extends App {
case class Label[A](name: String)
case class LabelWithValue[A](label: Label[A], value: A)
// TODO: avoid the horrible global state - pass in the Map as a parameter
var horribleGlobalState: Map[String, Any] = _
object GetLabelWithValue extends (Label ~> LabelWithValue) {
def apply[A](label: Label[A]) =
LabelWithValue(label, horribleGlobalState.get(label.name).asInstanceOf[A])
}
val label1 = Label[Int]("a")
val label2 …Run Code Online (Sandbox Code Playgroud) 我正在使用 Shapeless 2.2.5。我尝试使用下面的代码将元组转换为 HList。
import shapeless._
import syntax.std.product._
(23, "foo", 2.0, true).productElements
Run Code Online (Sandbox Code Playgroud)
但我收到编译错误。
[error] /scala/testScala/src/test/scala/lombok/shapeless/TestTuple2HList.scala:12: could not find implicit value for parameter gen: shapeless.Generic[(Int, String, Double, Boolean)]
[error] (23, "foo", 2.0, true).productElements
Run Code Online (Sandbox Code Playgroud)
https://github.com/milessabin/shapeless/blob/master/core/src/test/scala/shapeless/conversions.scala 中的测试转换 .scala
没有为 Generic[(Int,String,Double,Boolean)] 提供隐式值。
我错过了一些进口吗?
在此先感谢您的帮助!
盛
我学习不成形,目前我试图创建一个具有以下功能:给定一个类型的HList它返回HList的NoneS,与Option相应的给定类型的HList类型.
例如:
create[String :: Int :: HNil] // returns None[String] :: None[Int] :: HNil
Run Code Online (Sandbox Code Playgroud)
所以逻辑如下:
def create[A <: HList] {
type HT = ??? //somehow getting Head type
type TT = ??? //somehow getting Tail type
// if HT is HNil HNil else Option.empty[HT] :: create[TT]
}
Run Code Online (Sandbox Code Playgroud)
看起来像HT和TT可以提供IsHCons
def createHList[L <: HList](implicit ihc: IsHCons[L]): HList = {
type HT = ihc.H
type TT = ihc.T …Run Code Online (Sandbox Code Playgroud) 假设我有一个没有参数的方法.如何确定类型参数的长度?
def func[T <: HList]: Nat = {
// some magic
}
Run Code Online (Sandbox Code Playgroud) 使用Generic#to,我可以得到一个HList代表case class:
import shapeless._
case class F(x: Int, y: String)
scala> Generic[F].to( F(1, "foo") )
res1: shapeless.::[Int,shapeless.::[String,shapeless.HNil]] =
1 :: foo :: HNil
Run Code Online (Sandbox Code Playgroud)
但是,我想得到以下表示:
("x", 1) :: ("y", "foo") :: HNil
换句话说,而不是只是F实例的字段的值,我想获得字段名,即x和y,以及.
我怎样才能得到这种表述?