我正在努力理解TraverseScalaz 中trait中的以下函数定义:
def traverse[F[_] : Applicative, A, B](f: A => F[B], t: T[A]): F[T[B]]
我不明白的部分是F[_] : Applicative.
现在,让我们看看是什么Applicative:
trait Applicative[Z[_]] extends Pointed[Z] with Apply[Z] {
override def fmap[A, B](fa: Z[A], f: A => B): Z[B] = this(pure(f), fa)
override def apply[A, B](f: Z[A => B], a: Z[A]): Z[B] = liftA2(f, a, (_:A => B)(_: A))
def liftA2[A, B, C](a: Z[A], b: Z[B], f: (A, B) => C): Z[C] = apply(fmap(a, f.curried), b)
}
Run Code Online (Sandbox Code Playgroud)
在这里,为了 …
我想隐式地将函数转换A => B为List[A] => List[B].
我写了以下隐含的定义:
implicit def lift[A, B](f: A => B): List[A] => List[B] = ...
Run Code Online (Sandbox Code Playgroud)
不幸的是,当我编写以下代码时,隐式不会被应用:
val plusOne: (List[Int]) => List[Int] = (x: Int) => (x + 1)
Run Code Online (Sandbox Code Playgroud)
如果我用显式时间注释函数,它工作正常.
为什么?我该如何解决?
UPDATE.似乎问题是匿名函数特有的.相比:
@Test
def localLiftingGenerics {
implicit def anyPairToList[X, Y](x: (X, Y)): List[X] => List[Y] = throw new UnsupportedOperationException
val v: List[String] => List[Int] = ("abc", 239)
}
@Test
def localLiftingFuns {
implicit def fun2ListFun[X, Y](f: X => Y): List[X] => List[Y] = throw …Run Code Online (Sandbox Code Playgroud) 在Scala 2.10.4中,这编译:
trait Foo[-U,T]{
type Contra = U
}
Run Code Online (Sandbox Code Playgroud)
但是在2.11.0中同样失败了:
逆变型U出现在类型为反对特征Foo的类型U的不变位置[-U,T] {类型Contra = U}
有可用的解决方法吗?试图将Scala库移植到2.11并且需要逆变类型以获得编译器拾取的大量隐式defs(即使U不变量似乎不是一个选项).
谢谢
有没有办法约束一个方法,只有两种类型被证明不相等才有意义?
trait Something[A, B] {
// I can only be called if type A is the same as type B
def ifEqual(implicit ev: A =:= B)
// Now I cannot be called if type A is proven to be the same as type B
def ifNotEqual(implicit ev: A ??? B)
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试一些相当复杂的类型级计算.在那里,我有一些类型的标签(比如A,B和C),和功能对他们的工作,这是由与路径依赖的结果类型隐目击者表示:
class A
class B
class C
trait F1[T] { type result }
trait F2[T] { type result }
implicit object f1OfA extends F1[A] { type result = B }
implicit object f2OfB extends F2[B] { type result = C }
trait Composed[T] { type result }
Run Code Online (Sandbox Code Playgroud)
在计算过程中,当"实现"时Composed,我需要利用这样一个事实:在给定上述代码的情况下,原则上可以转换A为C(在这个例子中,我只需要组合,但实际上还有更多的事情涉及) .
但是,我不知道如何表达构图,因为我总是受到限制的限制,暗示不是传递性的; 以下代码失败,显示"隐式未找到":
implicit def composed1[X](implicit f2DotF1OfX: F2[F1[X]]): Composed[X] =
new Composed[X] { type result = f2DotF1OfX.result }
implicitly[Composed[C]]
Run Code Online (Sandbox Code Playgroud)
我最初尝试写的内容如下: …
type-systems scala path-dependent-type implicits type-level-computation
我的Spark代码充满了这样的代码
object Transformations {
def selectI(df:DataFrame) : DataFrame = {
// needed to use $ to generate ColumnName
import df.sparkSession.implicits._
df.select($"i")
}
}
Run Code Online (Sandbox Code Playgroud)
或者
object Transformations {
def selectI(df:DataFrame)(implicit spark:SparkSession) : DataFrame = {
// needed to use $ to generate ColumnName
import sparkSession.implicits._
df.select($"i")
}
}
Run Code Online (Sandbox Code Playgroud)
我真的不明白为什么我们需要一个实例SparkSession来导入这些隐式转换。我想做这样的事情:
object Transformations {
import org.apache.spark.sql.SQLImplicits._ // does not work
def selectI(df:DataFrame) : DataFrame = {
df.select($"i")
}
}
Run Code Online (Sandbox Code Playgroud)
有解决这个问题的优雅方法吗?我使用implicits的并不限于$而且Encoders,.toDF()等等。
我问自己什么是视图绑定相当于
(implicit conv: String => A)
Run Code Online (Sandbox Code Playgroud)
我的第一次尝试是简单地声明类型参数A,如下所示:
[String <% A]
Run Code Online (Sandbox Code Playgroud)
但Scala编译器抱怨"未找到:类型A".
有什么建议?
关于隐式类的东西,混淆reduce().在隐式类内部时,编译器会在reduce()第二个参数上抱怨.但是当相同的代码在非隐式方法中时,它编译并正常工作.
我对隐含类缺少什么?
object ImpliCurri {
implicit class MySeq[Int](val l: Seq[Int]) {
//not compiling
final def mapSum(f:Int=>Int):Int = {
l.map(x=>f(x)).reduce(_+_)
//compile error on reduce: Type mismatch. Expected String, fount Int
}
}
// works fine
def mySum(l:Seq[Int], f:Int=>Int):Int = {
l.map(x=>f(x)).reduce(_+_)
// compiles and works no issues
}
}
Run Code Online (Sandbox Code Playgroud) 该方法doesNotCompile仅接受仅包含Label[A]条目的HLists .有一个Mapper可以将Label [A]转换为String(准确地说:) Const[String]#?.但是,当我应用映射器时,返回类型为ev1.Out.我知道这实际上只是一个只有字符串的HList,但我怎么能说服编译器呢?
import shapeless._
import shapeless.poly._
import shapeless.ops.hlist._
import shapeless.UnaryTCConstraint._
object Util {
case class Label[A](name: String, value: A)
object GetLabelName extends (Label ~> Const[String]#?) {
def apply[A](label: Label[A]) = label.name
}
}
object Main {
import Util._
def bar(l: List[String]) = ???
def compiles = {
val names = "a" :: "b" :: HNil
bar(names.toList)
}
// A is an HList whose members are all Label[_]
def doesNotCompile[A <: HList : *->*[Label]#?](labels: …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) implicits ×10
scala ×10
shapeless ×3
apache-spark ×1
currying ×1
function ×1
scalaz ×1
type-systems ×1
typeclass ×1
types ×1
variance ×1
view-bound ×1